Skip to content
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

cargo new/init: Conditionalize adding Cargo.lock to gitignore #2390

Merged
Merged
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
7 changes: 6 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct MkOptions<'a> {
path: &'a Path,
name: &'a str,
source_files: Vec<SourceFileInformation>,
bin: bool,
}

impl Decodable for VersionControl {
Expand Down Expand Up @@ -222,6 +223,7 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
path: &path,
name: name,
source_files: vec![plan_new_source_file(opts.bin, name.to_string())],
bin: opts.bin,
};

mk(config, &mkopts).chain_error(|| {
Expand Down Expand Up @@ -282,6 +284,7 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
version_control: version_control,
path: &path,
name: name,
bin: src_paths_types.iter().any(|x|x.bin),
source_files: src_paths_types,
};

Expand Down Expand Up @@ -315,7 +318,9 @@ fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
let cfg = try!(global_config(config));
let mut ignore = "target\n".to_string();
let in_existing_vcs_repo = existing_vcs_repo(path.parent().unwrap(), config.cwd());
ignore.push_str("Cargo.lock\n");
if !opts.bin {
ignore.push_str("Cargo.lock\n");
}

let vcs = match (opts.version_control, cfg.version_control, in_existing_vcs_repo) {
(None, None, false) => VersionControl::Git,
Expand Down
61 changes: 61 additions & 0 deletions tests/test_cargo_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,67 @@ test!(gitignore_appended_not_replaced {
assert!(contents.contains(r#"qqqqqq"#));
});

test!(cargo_lock_gitignored_if_lib1 {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join(".gitignore"), existing_file());

let mut contents = String::new();
File::open(&paths::root().join(".gitignore")).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(r#"Cargo.lock"#));
});

test!(cargo_lock_gitignored_if_lib2 {
fs::create_dir(&paths::root().join(".git")).unwrap();

File::create(&paths::root().join("lib.rs")).unwrap().write_all(br#""#).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join(".gitignore"), existing_file());

let mut contents = String::new();
File::open(&paths::root().join(".gitignore")).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(r#"Cargo.lock"#));
});

test!(cargo_lock_not_gitignored_if_bin1 {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
.arg("--bin")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join(".gitignore"), existing_file());

let mut contents = String::new();
File::open(&paths::root().join(".gitignore")).unwrap().read_to_string(&mut contents).unwrap();
assert!(!contents.contains(r#"Cargo.lock"#));
});

test!(cargo_lock_not_gitignored_if_bin2 {
fs::create_dir(&paths::root().join(".git")).unwrap();

File::create(&paths::root().join("main.rs")).unwrap().write_all(br#""#).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join(".gitignore"), existing_file());

let mut contents = String::new();
File::open(&paths::root().join(".gitignore")).unwrap().read_to_string(&mut contents).unwrap();
assert!(!contents.contains(r#"Cargo.lock"#));
});

test!(with_argument {
assert_that(cargo_process("init").arg("foo").arg("--vcs").arg("none")
.env("USER", "foo"),
Expand Down