Skip to content

Commit

Permalink
tests: Declare manifests and extra files in test code (#44)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Turbo87 authored Jun 28, 2024
1 parent 450baaa commit 64f7a85
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 55 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ toml = { version = "0.8", features = ["preserve_order"] }

[dev-dependencies]
insta = "1.39.0"
tempfile = "3.10.1"
8 changes: 0 additions & 8 deletions tests/autobin/Cargo.toml

This file was deleted.

Empty file removed tests/autobin/src/main.rs
Empty file.
3 changes: 0 additions & 3 deletions tests/autobuild/Cargo.toml

This file was deleted.

Empty file removed tests/autobuild/build.rs
Empty file.
49 changes: 44 additions & 5 deletions tests/autolib.rs
Original file line number Diff line number Diff line change
@@ -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"));
Expand All @@ -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"));
Expand All @@ -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"));
Expand All @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions tests/autolib/bin/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion tests/autolib/bin/src/main.rs

This file was deleted.

3 changes: 0 additions & 3 deletions tests/autolib/lib_rs/Cargo.toml

This file was deleted.

Empty file removed tests/autolib/lib_rs/src/lib.rs
Empty file.
6 changes: 0 additions & 6 deletions tests/autolib/name_override/Cargo.toml

This file was deleted.

Empty file.
8 changes: 0 additions & 8 deletions tests/autolib/other_override/Cargo.toml

This file was deleted.

Empty file.
6 changes: 0 additions & 6 deletions tests/autolib/path_override/Cargo.toml

This file was deleted.

Empty file.
Empty file.
7 changes: 0 additions & 7 deletions tests/metadata/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/metadata/build.rs

This file was deleted.

Empty file removed tests/metadata/lib.rs
Empty file.
36 changes: 33 additions & 3 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 64f7a85

Please sign in to comment.