From 3e8cdd7ce0f0da2494e07233519a1fc13df45277 Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Fri, 8 Nov 2019 09:59:33 +1100 Subject: [PATCH 1/3] Only include "already existing ..." comment in gitignore on conflict This comment was previously confusing when there were no existing items that conflict. --- src/cargo/ops/cargo_new.rs | 12 ++++++++---- tests/testsuite/init.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index e7c3facb031..393ddd6d93e 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -487,10 +487,14 @@ impl IgnoreList { _ => &self.ignore, }; - let mut out = "\n\n#Added by cargo\n\ - #\n\ - #already existing elements are commented out\n\n" - .to_string(); + let mut out = "\n\n#Added by cargo\n".to_string(); + if ignore_items + .iter() + .any(|item| existing_items.contains(item)) + { + out.push_str("#\n#already existing elements are commented out\n"); + } + out.push('\n'); for item in ignore_items { if existing_items.contains(item) { diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index e0e6620be60..7132fabd4ae 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -99,6 +99,33 @@ fn simple_git_ignore_exists() { cargo_process("build").cwd(&paths::root().join("foo")).run(); } +#[cargo_test] +fn git_ignore_exists_no_conflicting_entries() { + // write a .gitignore file with one entry + fs::create_dir_all(paths::root().join("foo")).unwrap(); + fs::write(paths::root().join("foo/.gitignore"), "**/some.file").unwrap(); + + cargo_process("init --lib foo --edition 2015") + .env("USER", "foo") + .run(); + + let fp = paths::root().join("foo/.gitignore"); + let mut contents = String::new(); + File::open(&fp) + .unwrap() + .read_to_string(&mut contents) + .unwrap(); + assert_eq!( + contents, + "**/some.file\n\n\ + #Added by cargo\n\ + \n\ + /target\n\ + **/*.rs.bk\n\ + Cargo.lock\n", + ); +} + #[cargo_test] fn both_lib_and_bin() { cargo_process("init --lib --bin") From a6db5b0797a9325631a093c6f4de600411d826cb Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Fri, 8 Nov 2019 10:00:29 +1100 Subject: [PATCH 2/3] Fix comment --- tests/testsuite/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 7132fabd4ae..4eaae95cf9a 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -59,7 +59,7 @@ fn simple_bin() { #[cargo_test] fn simple_git_ignore_exists() { - // write a .gitignore file with one entry + // write a .gitignore file with two entries fs::create_dir_all(paths::root().join("foo")).unwrap(); fs::write( paths::root().join("foo/.gitignore"), From c42f2eeb0e7807de2c56e3cd6932f1ef6cdb3312 Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Fri, 8 Nov 2019 10:00:57 +1100 Subject: [PATCH 3/3] Improve gitignore comment wording --- src/cargo/ops/cargo_new.rs | 2 +- tests/testsuite/init.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 393ddd6d93e..0f7709622ca 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -492,7 +492,7 @@ impl IgnoreList { .iter() .any(|item| existing_items.contains(item)) { - out.push_str("#\n#already existing elements are commented out\n"); + out.push_str("#\n#already existing elements were commented out\n"); } out.push('\n'); diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 4eaae95cf9a..6fef7b7b269 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -89,7 +89,7 @@ fn simple_git_ignore_exists() { **/some.file\n\n\ #Added by cargo\n\ #\n\ - #already existing elements are commented out\n\ + #already existing elements were commented out\n\ \n\ #/target\n\ **/*.rs.bk\n\