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

Add snapshot test based on Gerust-generated project #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
599 changes: 599 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ installers = []
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
# Publish jobs to run in CI
pr-run-mode = "plan"

[dev-dependencies]
git2 = "0.19.0"
insta = { version = "1.41", features = ["glob"] }
rand = "0.8.5"

[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cargo_manifest::{Dependency, DependencyDetail, DepsSet, Manifest, Workspace}
use guppy::VersionReq;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Formatter;
use std::path::PathBuf;
use toml_edit::{Array, Key};

mod dedup;
Expand All @@ -17,7 +18,11 @@ pub struct AutoInheritConf {
pub prefer_simple_dotted: bool,
/// Package name(s) of workspace member(s) to exclude.
#[arg(short, long)]
exclude_members: Vec<String>,
pub exclude_members: Vec<String>,

/// Path of the workspace manifest
#[arg(short, long)]
pub manifest_path: Option<PathBuf>,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -121,7 +126,10 @@ macro_rules! get_either_table_mut {
}

pub fn auto_inherit(conf: AutoInheritConf) -> Result<(), anyhow::Error> {
let metadata = guppy::MetadataCommand::new().exec().context(
let mut metadata_cmd = guppy::MetadataCommand::new();
conf.manifest_path.map(|p| metadata_cmd.manifest_path(p));

let metadata = metadata_cmd.exec().context(
"Failed to execute `cargo metadata`. Was the command invoked inside a Rust project?",
)?;
let graph = metadata
Expand Down
69 changes: 69 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{env::temp_dir, fs, path::PathBuf};

use cargo_autoinherit::{auto_inherit, AutoInheritConf};
use git2::Repository;
use insta::assert_snapshot;
use rand::{
distributions::{Alphanumeric, DistString},
thread_rng,
};

const TEST_REPO_URL: &str = match option_env!("TEST_REPO_URL") {
Some(url) => url,
None => "https://github.com/hdoordt/cargo-autoinherit-test",
};

fn clone_test_project() -> PathBuf {
let dir: String = Alphanumeric.sample_string(&mut thread_rng(), 32);
let dir = temp_dir().join(dir);
Repository::clone(TEST_REPO_URL, &dir).expect("Error cloning test repository");
dir
}

fn run_with_conf(conf: AutoInheritConf) -> PathBuf {
let repo_path = clone_test_project();

let conf = AutoInheritConf {
manifest_path: Some(PathBuf::from(format!(
"{}/Cargo.toml",
repo_path.to_str().unwrap()
))),
..conf
};
auto_inherit(conf).unwrap();
repo_path
}

#[test]
fn default_behavior() {
let repo_path = run_with_conf(AutoInheritConf::default());
insta::glob!(repo_path, "**/Cargo.toml", |p| {
let input = fs::read_to_string(dbg!(p)).unwrap();
assert_snapshot!(input);
});
}

#[test]
fn prefer_simple_dotted() {
let repo_path = run_with_conf(AutoInheritConf {
prefer_simple_dotted: true,
..Default::default()
});
insta::glob!(repo_path, "**/Cargo.toml", |p| {
let input = fs::read_to_string(dbg!(p)).unwrap();
assert_snapshot!(input);
});
}

#[test]
fn with_excluded_member() {
let repo_path = run_with_conf(AutoInheritConf {
prefer_simple_dotted: false,
exclude_members: vec!["cargo-autoinherit-test-cli".to_string()],
..Default::default()
});
insta::glob!(repo_path, "**/Cargo.toml", |p| {
let input = fs::read_to_string(dbg!(p)).unwrap();
assert_snapshot!(input);
});
}
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[workspace]
members = [
"cli",
"config",
"db",
"web",
"macros"
]
resolver = "2"
default-members = ["web"]

[workspace.metadata.cargo-autoinherit]
# Skip cargo-autoinherit for these packages
exclude-members = [
"cargo-autoinherit-test-web"
]

[workspace.dependencies]
anyhow = "1.0"
cargo-autoinherit-test-config = { path = "config" }
clap = "4.4"
cruet = "0.14"
dotenvy = "0.15"
fake = "2.9"
figment = "0.10"
googletest = "0.12"
guppy = "0.17"
include_dir = "0.7"
insta = "1.38"
liquid = "~0.26"
quote = "1.0"
rand = "0.8"
regex = "1.10"
serde = "1.0"
sqlx = "0.8"
syn = "2.0"
thiserror = "1.0"
tokio = "1.34"
tracing = "0.1"
url = "2.5"
utoipa = "5.1.3"
uuid = "1.5"
validator = "0.18"
36 changes: 36 additions & 0 deletions tests/snapshots/main__default_behavior@cli__Cargo.toml.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[package]
name = "cargo-autoinherit-test-cli"
version = "0.0.1"
edition = "2021"
publish = false

[lib]
# examples in docs don't run without additional setup
doctest = false

[[bin]]
name = "db"
path = "src/bin/db.rs"
[[bin]]
name = "generate"
path = "src/bin/generate.rs"

[dependencies]
anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] }
cruet = { workspace = true }
guppy = { workspace = true }
include_dir = { workspace = true }
liquid = { workspace = true }
cargo-autoinherit-test-config = { workspace = true }
sqlx = { workspace = true, features = ["runtime-tokio", "tls-rustls", "postgres", "macros", "uuid", "migrate", "chrono"] }
url = { workspace = true }
tokio = { workspace = true, features = ["full"] }

[dev-dependencies]
insta = { workspace = true }
25 changes: 25 additions & 0 deletions tests/snapshots/main__default_behavior@config__Cargo.toml.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[package]
name = "cargo-autoinherit-test-config"
version = "0.0.1"
edition = "2021"
publish = false

[lib]
# examples in docs don't run without config files being in place, etc.
doctest = false

[dependencies]
anyhow = { workspace = true }
dotenvy = { workspace = true }
figment = { workspace = true, features = ["toml", "env"] }
serde = { workspace = true, features = ["derive"] }
tracing = { workspace = true }

[dev-dependencies]
figment = { workspace = true, features = ["toml", "env", "test"] }
googletest = { workspace = true }
31 changes: 31 additions & 0 deletions tests/snapshots/main__default_behavior@db__Cargo.toml.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[package]
name = "cargo-autoinherit-test-db"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
# examples in docs don't run without a running database, etc.
doctest = false

[features]
test-helpers = ["dep:fake", "dep:rand", "dep:regex"]
openapi = ["dep:utoipa"]

[dependencies]
anyhow = { workspace = true }
fake = { workspace = true, features = ["derive"], optional = true }
cargo-autoinherit-test-config = { workspace = true }
rand = { workspace = true, optional = true }
regex = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
sqlx = { workspace = true, features = ["runtime-tokio", "tls-rustls", "postgres", "macros", "uuid", "migrate", "chrono"] }
thiserror = { workspace = true }
utoipa = { workspace = true, features = ["uuid"], optional = true }
uuid = { workspace = true, features = ["serde"] }
validator = { workspace = true, features = ["derive"] }
19 changes: 19 additions & 0 deletions tests/snapshots/main__default_behavior@macros__Cargo.toml.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[package]
name = "cargo-autoinherit-test-macros"
version = "0.0.1"
edition = "2021"
publish = false

[lib]
proc-macro = true
# examples in docs don't run without a running database, etc.
doctest = false

[dependencies]
quote = { workspace = true }
syn = { workspace = true, features = ["full"] }
45 changes: 45 additions & 0 deletions tests/snapshots/main__default_behavior@web__Cargo.toml.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[package]
name = "cargo-autoinherit-test-web"
version = "0.0.1"
edition = "2021"
publish = false

[lib]
# examples in docs don't run without a running database, etc.
doctest = false

[features]
test-helpers = ["dep:serde_json", "dep:tower", "dep:hyper"]

[dependencies]
anyhow = "1.0"
axum = { version = "0.7", features = ["macros"] }
cargo-autoinherit-test-config = { path = "../config" }
cargo-autoinherit-test-db = { path = "../db", features = ["openapi"] }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.34", features = ["full"] }
tower-http = { version = "0.6", features = ["full"] }
tracing = "0.1"
tracing-panic = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "registry", "fmt"] }
uuid = { version = "1.6", features = ["serde"] }
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"
tower = { version = "0.5", features = ["util"], optional = true }
hyper = { version = "1.0", features = ["full"], optional = true }
cargo-autoinherit-test-macros = { path = "../macros" }
validator = { version = "0.18.1", features = ["derive"] }
utoipa = { version = "5.1.2", features = ["axum_extras", "uuid"] }
utoipa-axum = {version = "0.1.2" }
utoipa-swagger-ui = { version = "8.0.3", features = ["axum", "reqwest"] }

[dev-dependencies]
fake = "2.9"
googletest = "0.12"
cargo-autoinherit-test-db = { path = "../db", features = ["test-helpers"] }
cargo-autoinherit-test-web = { path = ".", features = ["test-helpers"] }
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/main.rs
expression: input
snapshot_kind: text
---
[workspace]
members = [
"cli",
"config",
"db",
"web",
"macros"
]
resolver = "2"
default-members = ["web"]

[workspace.metadata.cargo-autoinherit]
# Skip cargo-autoinherit for these packages
exclude-members = [
"cargo-autoinherit-test-web"
]

[workspace.dependencies]
anyhow = "1.0"
cargo-autoinherit-test-config = { path = "config" }
clap = "4.4"
cruet = "0.14"
dotenvy = "0.15"
fake = "2.9"
figment = "0.10"
googletest = "0.12"
guppy = "0.17"
include_dir = "0.7"
insta = "1.38"
liquid = "~0.26"
quote = "1.0"
rand = "0.8"
regex = "1.10"
serde = "1.0"
sqlx = "0.8"
syn = "2.0"
thiserror = "1.0"
tokio = "1.34"
tracing = "0.1"
url = "2.5"
utoipa = "5.1.3"
uuid = "1.5"
validator = "0.18"
Loading