Skip to content

Commit 6baf6bc

Browse files
committed
Add tests
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.
1 parent 63cb62d commit 6baf6bc

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/testsuite/init.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,44 @@ fn no_filename() {
657657
)
658658
.run();
659659
}
660+
661+
#[cargo_test]
662+
fn formats_source() {
663+
fs::write(&paths::root().join("rustfmt.toml"), "tab_spaces = 2").unwrap();
664+
665+
cargo_process("init --lib")
666+
.with_stderr("[CREATED] library package")
667+
.run();
668+
669+
assert_eq!(
670+
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
671+
r#"#[cfg(test)]
672+
mod tests {
673+
#[test]
674+
fn it_works() {
675+
assert_eq!(2 + 2, 4);
676+
}
677+
}
678+
"#
679+
);
680+
}
681+
682+
#[cargo_test]
683+
fn ignores_failure_to_format_source() {
684+
cargo_process("init --lib")
685+
.env("PATH", "") // pretend that `rustfmt` is missing
686+
.with_stderr("[CREATED] library package")
687+
.run();
688+
689+
assert_eq!(
690+
fs::read_to_string(paths::root().join("src/lib.rs")).unwrap(),
691+
r#"#[cfg(test)]
692+
mod tests {
693+
#[test]
694+
fn it_works() {
695+
assert_eq!(2 + 2, 4);
696+
}
697+
}
698+
"#
699+
);
700+
}

0 commit comments

Comments
 (0)