Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Test 1: init a library crate with a `rustfmt.toml` config file in it.
Expect the generated source files to be formatted according to the
config file.

Test 2: same as test 1, but with missing `rustfmt`. Expect `cargo init`
to ignore the absence of `rustfmt` and generate source files
with default formatting.
  • Loading branch information
Kinrany committed Jan 27, 2020
1 parent 63cb62d commit 6baf6bc
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,44 @@ fn no_filename() {
)
.run();
}

#[cargo_test]
fn formats_source() {
fs::write(&paths::root().join("rustfmt.toml"), "tab_spaces = 2").unwrap();

cargo_process("init --lib")
.with_stderr("[CREATED] library package")
.run();

assert_eq!(
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
r#"#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#
);
}

#[cargo_test]
fn ignores_failure_to_format_source() {
cargo_process("init --lib")
.env("PATH", "") // pretend that `rustfmt` is missing
.with_stderr("[CREATED] library package")
.run();

assert_eq!(
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
r#"#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#
);
}

0 comments on commit 6baf6bc

Please sign in to comment.