From 64f7a855d36aea86744598121d5e6a369cf5a723 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 28 Jun 2024 10:49:04 +0200 Subject: [PATCH] tests: Declare manifests and extra files in test code (#44) * tests/autolib: Declare manifests and extra files in test code This makes the tests a little easier to comprehend, since we don't have to crawl through the filesystem structure to understand the test setup. * tests: Move `prepare()` fn to `utils` module This will allow us to share this function with other tests * tests/parse: Declare manifests and extra files in test code This makes the tests a little easier to comprehend, since we don't have to crawl through the filesystem structure to understand the test setup. --- Cargo.toml | 1 + tests/autobin/Cargo.toml | 8 ---- tests/autobin/src/main.rs | 0 tests/autobuild/Cargo.toml | 3 -- tests/autobuild/build.rs | 0 tests/autolib.rs | 49 ++++++++++++++++++++++--- tests/autolib/bin/Cargo.toml | 3 -- tests/autolib/bin/src/main.rs | 1 - tests/autolib/lib_rs/Cargo.toml | 3 -- tests/autolib/lib_rs/src/lib.rs | 0 tests/autolib/name_override/Cargo.toml | 6 --- tests/autolib/name_override/src/lib.rs | 0 tests/autolib/other_override/Cargo.toml | 8 ---- tests/autolib/other_override/src/lib.rs | 0 tests/autolib/path_override/Cargo.toml | 6 --- tests/autolib/path_override/src/foo.rs | 0 tests/autolib/path_override/src/lib.rs | 0 tests/metadata/Cargo.toml | 7 ---- tests/metadata/build.rs | 2 - tests/metadata/lib.rs | 0 tests/parse.rs | 36 ++++++++++++++++-- tests/utils.rs | 17 +++++++++ 22 files changed, 95 insertions(+), 55 deletions(-) delete mode 100644 tests/autobin/Cargo.toml delete mode 100644 tests/autobin/src/main.rs delete mode 100644 tests/autobuild/Cargo.toml delete mode 100644 tests/autobuild/build.rs delete mode 100644 tests/autolib/bin/Cargo.toml delete mode 100644 tests/autolib/bin/src/main.rs delete mode 100644 tests/autolib/lib_rs/Cargo.toml delete mode 100644 tests/autolib/lib_rs/src/lib.rs delete mode 100644 tests/autolib/name_override/Cargo.toml delete mode 100644 tests/autolib/name_override/src/lib.rs delete mode 100644 tests/autolib/other_override/Cargo.toml delete mode 100644 tests/autolib/other_override/src/lib.rs delete mode 100644 tests/autolib/path_override/Cargo.toml delete mode 100644 tests/autolib/path_override/src/foo.rs delete mode 100644 tests/autolib/path_override/src/lib.rs delete mode 100644 tests/metadata/Cargo.toml delete mode 100644 tests/metadata/build.rs delete mode 100644 tests/metadata/lib.rs create mode 100644 tests/utils.rs diff --git a/Cargo.toml b/Cargo.toml index 27cc882..9d384d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ toml = { version = "0.8", features = ["preserve_order"] } [dev-dependencies] insta = "1.39.0" +tempfile = "3.10.1" diff --git a/tests/autobin/Cargo.toml b/tests/autobin/Cargo.toml deleted file mode 100644 index 98b0b44..0000000 --- a/tests/autobin/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "auto-bin" -version = "0.1.0" -publish = false -edition = "2018" - -[badges] -travis-ci = { repository = "…" } diff --git a/tests/autobin/src/main.rs b/tests/autobin/src/main.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autobuild/Cargo.toml b/tests/autobuild/Cargo.toml deleted file mode 100644 index 7815fbf..0000000 --- a/tests/autobuild/Cargo.toml +++ /dev/null @@ -1,3 +0,0 @@ -[package] -name = "buildrstest" -version = "0.2.0" diff --git a/tests/autobuild/build.rs b/tests/autobuild/build.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autolib.rs b/tests/autolib.rs index ba4023a..839993a 100644 --- a/tests/autolib.rs +++ b/tests/autolib.rs @@ -1,14 +1,24 @@ use cargo_manifest::Manifest; +mod utils; + +const BASIC_MANIFEST: &str = r#" +[package] +name = "auto-lib" +version = "0.1.0" +"#; + #[test] fn test_bin() { - let m = Manifest::from_path("tests/autolib/bin/Cargo.toml").unwrap(); + let tempdir = utils::prepare(BASIC_MANIFEST, vec!["src/main.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); assert!(m.lib.is_none()); } #[test] fn test_lib_rs() { - let m = Manifest::from_path("tests/autolib/lib_rs/Cargo.toml").unwrap(); + let tempdir = utils::prepare(BASIC_MANIFEST, vec!["src/lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); let lib = m.lib.unwrap(); assert_eq!(lib.path.as_deref(), Some("src/lib.rs")); @@ -19,7 +29,16 @@ fn test_lib_rs() { #[test] fn test_name_override() { - let m = Manifest::from_path("tests/autolib/name_override/Cargo.toml").unwrap(); + let manifest = r#" + [package] + name = "auto-lib" + version = "0.1.0" + + [lib] + name = "foo" + "#; + let tempdir = utils::prepare(manifest, vec!["src/lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); let lib = m.lib.unwrap(); assert_eq!(lib.path.as_deref(), Some("src/lib.rs")); @@ -30,7 +49,16 @@ fn test_name_override() { #[test] fn test_path_override() { - let m = Manifest::from_path("tests/autolib/path_override/Cargo.toml").unwrap(); + let manifest = r#" + [package] + name = "auto-lib" + version = "0.1.0" + + [lib] + path = "src/foo.rs" + "#; + let tempdir = utils::prepare(manifest, vec!["src/foo.rs", "src/lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); let lib = m.lib.unwrap(); assert_eq!(lib.path.as_deref(), Some("src/foo.rs")); @@ -41,7 +69,18 @@ fn test_path_override() { #[test] fn test_other_override() { - let m = Manifest::from_path("tests/autolib/other_override/Cargo.toml").unwrap(); + let manifest = r#" + [package] + name = "auto-lib" + version = "0.1.0" + edition = "2018" + + [lib] + proc-macro = true + test = false + "#; + let tempdir = utils::prepare(manifest, vec!["src/lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); let lib = m.lib.unwrap(); assert!(!lib.test); diff --git a/tests/autolib/bin/Cargo.toml b/tests/autolib/bin/Cargo.toml deleted file mode 100644 index 0a083ca..0000000 --- a/tests/autolib/bin/Cargo.toml +++ /dev/null @@ -1,3 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" diff --git a/tests/autolib/bin/src/main.rs b/tests/autolib/bin/src/main.rs deleted file mode 100644 index 8b13789..0000000 --- a/tests/autolib/bin/src/main.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tests/autolib/lib_rs/Cargo.toml b/tests/autolib/lib_rs/Cargo.toml deleted file mode 100644 index 0a083ca..0000000 --- a/tests/autolib/lib_rs/Cargo.toml +++ /dev/null @@ -1,3 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" diff --git a/tests/autolib/lib_rs/src/lib.rs b/tests/autolib/lib_rs/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autolib/name_override/Cargo.toml b/tests/autolib/name_override/Cargo.toml deleted file mode 100644 index dca1704..0000000 --- a/tests/autolib/name_override/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" - -[lib] -name = "foo" diff --git a/tests/autolib/name_override/src/lib.rs b/tests/autolib/name_override/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autolib/other_override/Cargo.toml b/tests/autolib/other_override/Cargo.toml deleted file mode 100644 index 1f91b9f..0000000 --- a/tests/autolib/other_override/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" -edition = "2018" - -[lib] -proc-macro = true -test = false diff --git a/tests/autolib/other_override/src/lib.rs b/tests/autolib/other_override/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autolib/path_override/Cargo.toml b/tests/autolib/path_override/Cargo.toml deleted file mode 100644 index bb10500..0000000 --- a/tests/autolib/path_override/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "auto-lib" -version = "0.1.0" - -[lib] -path = "src/foo.rs" \ No newline at end of file diff --git a/tests/autolib/path_override/src/foo.rs b/tests/autolib/path_override/src/foo.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/autolib/path_override/src/lib.rs b/tests/autolib/path_override/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/metadata/Cargo.toml b/tests/metadata/Cargo.toml deleted file mode 100644 index 482dbde..0000000 --- a/tests/metadata/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "metadata" -version = "0.1.0" -build = "foobar.rs" - -[lib] -path = "lib.rs" \ No newline at end of file diff --git a/tests/metadata/build.rs b/tests/metadata/build.rs deleted file mode 100644 index fa3fd96..0000000 --- a/tests/metadata/build.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Presence of this files ensures that `metadata` test checks that explicit `build` key in -// Cargo.toml has precedence over auto-detected build.rs file. diff --git a/tests/metadata/lib.rs b/tests/metadata/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/parse.rs b/tests/parse.rs index 1e92223..3666008 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -3,6 +3,8 @@ use cargo_manifest::{Manifest, MaybeInherited}; use std::fs::read; use std::str::FromStr; +mod utils; + #[test] fn own() { let m = Manifest::from_slice(&read("Cargo.toml").unwrap()).unwrap(); @@ -32,19 +34,47 @@ fn opt_version() { #[test] fn autobin() { - let m = Manifest::from_path("tests/autobin/Cargo.toml").expect("load autobin"); + let manifest = r#" + [package] + name = "auto-bin" + version = "0.1.0" + publish = false + edition = "2018" + + [badges] + travis-ci = { repository = "…" } + "#; + let tempdir = utils::prepare(manifest, vec!["src/main.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); insta::assert_debug_snapshot!(m); } #[test] fn autobuild() { - let m = Manifest::from_path("tests/autobuild/Cargo.toml").expect("load autobuild"); + let manifest = r#" + [package] + name = "buildrstest" + version = "0.2.0" + "#; + let tempdir = utils::prepare(manifest, vec!["build.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); insta::assert_debug_snapshot!(m); } +/// Checks that explicit `build` key in Cargo.toml has precedence over auto-detected build.rs file. #[test] fn metadata() { - let m = Manifest::from_path("tests/metadata/Cargo.toml").expect("load metadata"); + let manifest = r#" + [package] + name = "metadata" + version = "0.1.0" + build = "foobar.rs" + + [lib] + path = "lib.rs" + "#; + let tempdir = utils::prepare(manifest, vec!["build.rs", "lib.rs"]); + let m = Manifest::from_path(tempdir.path().join("Cargo.toml")).unwrap(); insta::assert_debug_snapshot!(m); } diff --git a/tests/utils.rs b/tests/utils.rs new file mode 100644 index 0000000..9267997 --- /dev/null +++ b/tests/utils.rs @@ -0,0 +1,17 @@ +use tempfile::TempDir; + +pub fn prepare(manifest: &str, extra_files: Vec<&str>) -> TempDir { + let tempdir = tempfile::tempdir().unwrap(); + + // Create `Cargo.toml` manifest file + std::fs::write(tempdir.path().join("Cargo.toml"), manifest).unwrap(); + + // Create extra files + for file in extra_files { + let path = tempdir.path().join(file); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, "").unwrap(); + } + + tempdir +}