mirror of
https://github.com/rust-lang/cargo
synced 2024-10-01 21:43:45 +00:00
Warn if project path contains an invalid PATH env character
This commit is contained in:
parent
f5cdfa4d8b
commit
82a92a203c
|
@ -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![];
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
22
tests/testsuite/init/path_contains_separator/mod.rs
Normal file
22
tests/testsuite/init/path_contains_separator/mod.rs
Normal 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());
|
||||
}
|
|
@ -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]
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
3
tests/testsuite/init/path_contains_separator/stderr.log
Normal file
3
tests/testsuite/init/path_contains_separator/stderr.log
Normal 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
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue