Skip to content

Support cargo new/init --force #6708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/bin/cargo/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub fn cli() -> App {
subcommand("init")
.about("Create a new cargo package in an existing directory")
.arg(Arg::with_name("path").default_value("."))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
}

Expand Down
9 changes: 1 addition & 8 deletions src/bin/cargo/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ pub fn cli() -> App {
subcommand("new")
.about("Create a new cargo package at <path>")
.arg(Arg::with_name("path").required(true))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
}

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let opts = args.new_options(config)?;

ops::new(&opts, config)?;
let path = args.value_of("path").unwrap();
let package_name = if let Some(name) = args.value_of("name") {
name
} else {
path
};
let package_name = ops::new(&opts, config)?;
config.shell().status(
"Created",
format!("{} `{}` package", opts.kind, package_name),
Expand Down
11 changes: 7 additions & 4 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct NewOptions {
pub path: PathBuf,
pub name: Option<String>,
pub edition: Option<String>,
pub force: bool,
pub registry: Option<String>,
}

Expand Down Expand Up @@ -81,6 +82,7 @@ impl NewOptions {
path: PathBuf,
name: Option<String>,
edition: Option<String>,
force: bool,
registry: Option<String>,
) -> CargoResult<NewOptions> {
let kind = match (bin, lib) {
Expand All @@ -96,6 +98,7 @@ impl NewOptions {
path,
name,
edition,
force,
registry,
};
Ok(opts)
Expand Down Expand Up @@ -299,9 +302,9 @@ fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformatio
}
}

pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
pub fn new<'a>(opts: &'a NewOptions, config: &Config) -> CargoResult<&'a str> {
let path = &opts.path;
if fs::metadata(path).is_ok() {
if fs::metadata(path).is_ok() && !opts.force {
failure::bail!(
"destination `{}` already exists\n\n\
Use `cargo init` to initialize the directory",
Expand Down Expand Up @@ -329,13 +332,13 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
path.display()
)
})?;
Ok(())
Ok(name)
}

pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
let path = &opts.path;

if fs::metadata(&path.join("Cargo.toml")).is_ok() {
if fs::metadata(&path.join("Cargo.toml")).is_ok() && !opts.force {
failure::bail!("`cargo init` cannot be run on existing Cargo packages")
}

Expand Down
3 changes: 3 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub trait AppExt: Sized {
)
.value_name("NAME"),
)
._arg(opt("force", "Force initialising over existing files").short("f"))
._arg(opt("registry", "Registry to use").value_name("REGISTRY"))
}

fn arg_index(self) -> Self {
Expand Down Expand Up @@ -380,6 +382,7 @@ pub trait ArgMatchesExt {
self.value_of_path("path", config).unwrap(),
self._value_of("name").map(|s| s.to_string()),
self._value_of("edition").map(|s| s.to_string()),
self._is_present("force"),
self.registry(config)?,
)
}
Expand Down
74 changes: 30 additions & 44 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use crate::support::{paths, Execs};
fn cargo_process(s: &str) -> Execs {
let mut execs = support::cargo_process(s);
execs.cwd(&paths::root()).env("HOME", &paths::home());
execs.env("USER", "foo");
execs
}

#[test]
fn simple_lib() {
cargo_process("init --lib --vcs none --edition 2015")
.env("USER", "foo")
.with_stderr("[CREATED] library package")
.run();

Expand All @@ -30,7 +30,6 @@ fn simple_bin() {
let path = paths::root().join("foo");
fs::create_dir(&path).unwrap();
cargo_process("init --bin --vcs none --edition 2015")
.env("USER", "foo")
.cwd(&path)
.with_stderr("[CREATED] binary (application) package")
.run();
Expand All @@ -53,9 +52,7 @@ fn simple_git_ignore_exists() {
.write("/target\n**/some.file".as_bytes())
.unwrap();

cargo_process("init --lib foo --edition 2015")
.env("USER", "foo")
.run();
cargo_process("init --lib foo --edition 2015").run();

assert!(paths::root().is_dir());
assert!(paths::root().join("foo/Cargo.toml").is_file());
Expand Down Expand Up @@ -88,7 +85,6 @@ fn simple_git_ignore_exists() {
#[test]
fn both_lib_and_bin() {
cargo_process("init --lib --bin")
.env("USER", "foo")
.with_status(101)
.with_stderr("[ERROR] can't specify both lib and binary outputs")
.run();
Expand All @@ -112,15 +108,9 @@ fn bin_already_exists(explicit: bool, rellocation: &str) {
.unwrap();

if explicit {
cargo_process("init --bin --vcs none")
.env("USER", "foo")
.cwd(&path)
.run();
cargo_process("init --bin --vcs none").cwd(&path).run();
} else {
cargo_process("init --vcs none")
.env("USER", "foo")
.cwd(&path)
.run();
cargo_process("init --vcs none").cwd(&path).run();
}

assert!(paths::root().join("foo/Cargo.toml").is_file());
Expand Down Expand Up @@ -184,7 +174,7 @@ fn confused_by_multiple_lib_files() {
.write_all(br#" fn qqq () { println!("Hello, world 3!"); }"#)
.unwrap();

cargo_process("init --vcs none").env("USER", "foo").cwd(&path).with_status(101).with_stderr(
cargo_process("init --vcs none").cwd(&path).with_status(101).with_stderr(
"[ERROR] cannot have a package with multiple libraries, found both `src/lib.rs` and `lib.rs`",
)
.run();
Expand Down Expand Up @@ -212,7 +202,6 @@ fn multibin_project_name_clash() {
.unwrap();

cargo_process("init --lib --vcs none")
.env("USER", "foo")
.cwd(&path)
.with_status(101)
.with_stderr(
Expand Down Expand Up @@ -243,10 +232,7 @@ fn lib_already_exists(rellocation: &str) {
.write_all(content)
.unwrap();

cargo_process("init --vcs none")
.env("USER", "foo")
.cwd(&path)
.run();
cargo_process("init --vcs none").cwd(&path).run();

assert!(paths::root().join("foo/Cargo.toml").is_file());
assert!(!paths::root().join("foo/src/main.rs").is_file());
Expand All @@ -272,9 +258,7 @@ fn lib_already_exists_nosrc() {

#[test]
fn simple_git() {
cargo_process("init --lib --vcs git")
.env("USER", "foo")
.run();
cargo_process("init --lib --vcs git").run();

assert!(paths::root().join("Cargo.toml").is_file());
assert!(paths::root().join("src/lib.rs").is_file());
Expand All @@ -284,21 +268,30 @@ fn simple_git() {

#[test]
fn auto_git() {
cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join("Cargo.toml").is_file());
assert!(paths::root().join("src/lib.rs").is_file());
assert!(paths::root().join(".git").is_dir());
assert!(paths::root().join(".gitignore").is_file());
}

#[test]
fn force() {
cargo_process("init").run();
cargo_process("init")
.with_status(101)
.with_stderr("[ERROR] `cargo init` cannot be run on existing Cargo packages")
.run();
cargo_process("init --force").run();
}

#[test]
fn invalid_dir_name() {
let foo = &paths::root().join("foo.bar");
fs::create_dir_all(&foo).unwrap();
cargo_process("init")
.cwd(foo.clone())
.env("USER", "foo")
.with_status(101)
.with_stderr(
"\
Expand All @@ -317,7 +310,6 @@ fn reserved_name() {
fs::create_dir_all(&test).unwrap();
cargo_process("init")
.cwd(test.clone())
.env("USER", "foo")
.with_status(101)
.with_stderr(
"\
Expand All @@ -334,7 +326,7 @@ use --name to override crate name
fn git_autodetect() {
fs::create_dir(&paths::root().join(".git")).unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join("Cargo.toml").is_file());
assert!(paths::root().join("src/lib.rs").is_file());
Expand All @@ -346,7 +338,7 @@ fn git_autodetect() {
fn mercurial_autodetect() {
fs::create_dir(&paths::root().join(".hg")).unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join("Cargo.toml").is_file());
assert!(paths::root().join("src/lib.rs").is_file());
Expand All @@ -363,7 +355,7 @@ fn gitignore_appended_not_replaced() {
.write_all(b"qqqqqq\n")
.unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join("Cargo.toml").is_file());
assert!(paths::root().join("src/lib.rs").is_file());
Expand All @@ -387,7 +379,7 @@ fn gitignore_added_newline_in_existing() {
.write_all(b"first")
.unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -403,7 +395,7 @@ fn gitignore_added_newline_in_existing() {
fn gitignore_no_newline_in_new() {
fs::create_dir(&paths::root().join(".git")).unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -424,7 +416,7 @@ fn mercurial_added_newline_in_existing() {
.write_all(b"first")
.unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join(".hgignore").is_file());

Expand All @@ -440,7 +432,7 @@ fn mercurial_added_newline_in_existing() {
fn mercurial_no_newline_in_new() {
fs::create_dir(&paths::root().join(".hg")).unwrap();

cargo_process("init --lib").env("USER", "foo").run();
cargo_process("init --lib").run();

assert!(paths::root().join(".hgignore").is_file());

Expand All @@ -456,9 +448,7 @@ fn mercurial_no_newline_in_new() {
fn cargo_lock_gitignored_if_lib1() {
fs::create_dir(&paths::root().join(".git")).unwrap();

cargo_process("init --lib --vcs git")
.env("USER", "foo")
.run();
cargo_process("init --lib --vcs git").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -479,7 +469,7 @@ fn cargo_lock_gitignored_if_lib2() {
.write_all(br#""#)
.unwrap();

cargo_process("init --vcs git").env("USER", "foo").run();
cargo_process("init --vcs git").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -495,9 +485,7 @@ fn cargo_lock_gitignored_if_lib2() {
fn cargo_lock_not_gitignored_if_bin1() {
fs::create_dir(&paths::root().join(".git")).unwrap();

cargo_process("init --vcs git --bin")
.env("USER", "foo")
.run();
cargo_process("init --vcs git --bin").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -518,7 +506,7 @@ fn cargo_lock_not_gitignored_if_bin2() {
.write_all(br#""#)
.unwrap();

cargo_process("init --vcs git").env("USER", "foo").run();
cargo_process("init --vcs git").run();

assert!(paths::root().join(".gitignore").is_file());

Expand All @@ -532,9 +520,7 @@ fn cargo_lock_not_gitignored_if_bin2() {

#[test]
fn with_argument() {
cargo_process("init foo --vcs none")
.env("USER", "foo")
.run();
cargo_process("init foo --vcs none").run();
assert!(paths::root().join("foo/Cargo.toml").is_file());
}

Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ fn existing() {
.run();
}

#[test]
fn force() {
fs::create_dir(paths::root().join("foo")).unwrap();
cargo_process("new --force foo").run();
}

#[test]
fn invalid_characters() {
cargo_process("new foo.rs")
Expand Down