Warn if project path contains an invalid PATH env character

This commit is contained in:
RyanAD 2022-10-26 15:27:46 -06:00
parent f5cdfa4d8b
commit 82a92a203c
9 changed files with 74 additions and 0 deletions

View file

@ -261,6 +261,18 @@ fn check_name(
Ok(())
}
/// Checks if the path contains any invalid PATH env characters.
fn check_path(path: &Path, shell: &mut Shell) -> CargoResult<()> {
if path.to_string_lossy().contains(&[';', ':', '"'][..]) {
shell.warn(format!(
"the path `{}` contains invalid PATH characters (usually `:`, `;`, or `\"`)\n\
It is recommended to use a different name to avoid problems.",
path.to_string_lossy()
))?;
}
Ok(())
}
fn detect_source_paths_and_types(
package_path: &Path,
package_name: &str,
@ -421,6 +433,8 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
)
}
check_path(path, &mut config.shell())?;
let is_bin = opts.kind.is_bin();
let name = get_name(path, opts)?;
@ -458,6 +472,8 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<NewProjectKind> {
anyhow::bail!("`cargo init` cannot be run on existing Cargo packages")
}
check_path(path, &mut config.shell())?;
let name = get_name(path, opts)?;
let mut src_paths_types = vec![];

View file

@ -28,6 +28,7 @@ mod mercurial_autodetect;
mod multibin_project_name_clash;
#[cfg(not(windows))]
mod no_filename;
mod path_contains_separator;
mod pijul_autodetect;
mod reserved_name;
mod simple_bin;

View file

@ -0,0 +1,7 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::prelude::*;
use cargo_test_support::{command_is_available, paths, Project};
use std::fs;
use std::process::Command;
use crate::test_root;

View file

@ -0,0 +1,22 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::prelude::*;
use cargo_test_support::Project;
use cargo_test_support::curr_dir;
#[cargo_test]
fn path_contains_separator() {
let project = Project::from_template(curr_dir!().join("in"));
let project_root = &project.root().join("test:ing");
snapbox::cmd::Command::cargo_ui()
.arg_line("init --bin --vcs none --edition 2015 --name testing")
.current_dir(project_root)
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));
assert_ui().subset_matches(curr_dir!().join("out"), project_root);
assert!(!project_root.join(".gitignore").is_file());
}

View file

@ -0,0 +1,8 @@
[package]
name = "testing"
version = "0.1.0"
edition = "2015"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View file

@ -0,0 +1,3 @@
warning: the path `[ROOT]/case/test:ing/.` contains PATH separators (usually `:` or `:`)
It is recommended to use a different name to avoid problems.
Created binary (application) package

View file

@ -529,3 +529,17 @@ Caused by:
)
.run();
}
#[cfg(unix)]
#[cargo_test]
fn path_with_invalid_character() {
cargo_process("new --name testing test:ing")
.with_stderr(
"\
[WARNING] the path `[CWD]/test:ing` contains invalid PATH characters (usually `:`, `;`, or `\"`)
It is recommended to use a different name to avoid problems.
[CREATED] binary (application) `testing` package
",
)
.run();
}