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

Fix/pact merge v4 without provider states #7

Merged
merged 4 commits into from
Jun 6, 2024
Merged
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
3 changes: 2 additions & 1 deletion compatibility-suite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ futures = "0.3.29"
itertools = "0.12.1"
lazy_static = "1.4.0"
maplit = "1.0.2"
pact_models = { version = "~1.2.0" }
pact_models = { version = "~1.2.1", path = "../rust/pact_models" }
pact_matching = { version = "1.2.3", path = "../rust/pact_matching" }
pact_mock_server = { version = "1.2.8" }
pact_verifier = { version = "1.2.1", path = "../rust/pact_verifier" }
Expand Down Expand Up @@ -64,3 +64,4 @@ harness = false

[patch.crates-io]
pact_matching = { path = "../rust/pact_matching" }
pact_models = { path = "../rust/pact_models" }
55 changes: 8 additions & 47 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ exclude = [
]
resolver = "2"

[patch.crates-io]
onig = { git = "https://github.com/rust-onig/rust-onig", default-features = false }

[profile.release]
strip = true
opt-level = "z"
codegen-units = 1
codegen-units = 1

[patch.crates-io]
onig = { git = "https://github.com/rust-onig/rust-onig", default-features = false }
pact_models = { version = "~1.2.1", path = "./pact_models" }
2 changes: 1 addition & 1 deletion rust/pact_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude = [
clap = "2.31.2"
serde_json = "1.0"
serde = "1.0"
pact_models = { version = "~1.2.0", path = "../pact_models" }
pact_models = { version = "~1.2.1", path = "../pact_models" }
pact_matching = { version = "~1.1.10", path = "../pact_matching" }
anyhow = "1.0.40"
log = "0.4.14"
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_consumer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ lazy_static = "1.4.0"
maplit = "1.0.2"
pact_matching = { version = "~1.2.3", path = "../pact_matching", default-features = false }
pact_mock_server = { version = "~1.2.8", default-features = false }
pact_models = { version = "~1.2.0", default-features = false }
pact_models = { version = "~1.2.1", default-features = false }
pact-plugin-driver = { version = "~0.6.1", optional = true, default-features = false }
regex = "1.10.4"
serde_json = "1.0.115"
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ multipart = { version = "0.18.0", default-features = false, features = ["client"
onig = { version = "6.4.0", default-features = false }
pact_matching = { version = "~1.2.3", path = "../pact_matching" }
pact_mock_server = { version = "~1.2.8" }
pact_models = { version = "~1.2.0" }
pact_models = { version = "~1.2.1" }
pact-plugin-driver = { version = "~0.6.1" }
pact_verifier = { version = "~1.2.1", path = "../pact_verifier" }
panic-message = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_matching/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mime = "0.3.17"
multer = { version = "3.0.0", features = ["all"], optional = true }
nom = "7.1.3"
onig = { version = "6.4.0", default-features = false }
pact_models = { version = "~1.2.0", default-features = false }
pact_models = { version = "~1.2.1", default-features = false }
pact-plugin-driver = { version = "~0.6.1", optional = true, default-features = false }
rand = "0.8.5"
reqwest = { version = "0.12.3", default-features = false, features = ["rustls-tls-native-roots", "json"] }
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_models/src/message_pact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl ReadWritePact for MessagePact {
let messages: Vec<Result<Message, String>> = self.messages.iter()
.merge_join_by(pact.interactions().iter(), |a, b| {
let cmp = Ord::cmp(&a.description, &b.description());
if cmp == Ordering::Equal {
if cmp == Ordering::Equal && ! &a.provider_states().is_empty(){
Ord::cmp(&a.provider_states.iter().map(|p| p.name.clone()).collect::<Vec<String>>(),
&b.provider_states().iter().map(|p| p.name.clone()).collect::<Vec<String>>())
} else {
Expand Down
83 changes: 82 additions & 1 deletion rust/pact_models/src/pact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,12 @@ pub fn write_pact(
warn!("Note: Existing pact is an older specification version ({:?}), and will be upgraded",
existing_pact.specification_version());
}

// println!("existing pact: {}",serde_json::to_string_pretty(&existing_pact.to_json(pact_spec)?)?);
// println!("new pact: {}",serde_json::to_string_pretty(&pact.to_json(pact_spec)?)?);
// println!("merging pacts");

let merged_pact = pact.merge(existing_pact.deref())?;
// println!("merged pact json: {}",serde_json::to_string_pretty(&merged_pact.to_json(pact_spec)?)?);
let pact_json = serde_json::to_string_pretty(&merged_pact.to_json(pact_spec)?)?;

with_write_lock(path, &mut f, 3, &mut |f| {
Expand Down Expand Up @@ -1010,6 +1014,83 @@ mod tests {
}}"#, PACT_RUST_VERSION.unwrap())));
}

#[test]
fn write_pact_test_should_merge_duplicate_pacts() {
let pact = RequestResponsePact { consumer: Consumer { name: "dupe_consumer".to_string() },
provider: Provider { name: "dupe_provider".to_string() },
interactions: vec![
RequestResponseInteraction {
description: "Test Interaction".to_string(),
request: Request { headers: Some(hashmap!{
"Accept".to_string()=>vec!["application/json".to_string()]
}), .. Request::default() },
.. RequestResponseInteraction::default()
}
],
metadata: btreemap!{},
specification_version: PactSpecification::V3
};
let pact2 = RequestResponsePact { consumer: Consumer { name: "dupe_consumer".to_string() },
provider: Provider { name: "dupe_provider".to_string() },
interactions: vec![
RequestResponseInteraction {
description: "Test Interaction".to_string(),
request: Request { headers: Some(hashmap!{
"Accept".to_string()=>vec!["application/json".to_string()]
}), .. Request::default() },
.. RequestResponseInteraction::default()
}
],
metadata: btreemap!{},
specification_version: PactSpecification::V3
};
let mut dir = env::temp_dir();
let x = rand::random::<u16>();
dir.push(format!("pact_test_{}", x));
dir.push(pact.default_file_name());

let result = write_pact(pact.boxed(), dir.as_path(), PactSpecification::V3, false);
let result2 = write_pact(pact2.boxed(), dir.as_path(), PactSpecification::V3, false);

let pact_file: String = read_pact_file(dir.as_path().to_str().unwrap()).unwrap_or("".to_string());
fs::remove_dir_all(dir.parent().unwrap()).unwrap_or(());

expect!(result).to(be_ok());
expect!(result2).to(be_ok());
expect!(pact_file).to(be_equal_to(format!(r#"{{
"consumer": {{
"name": "dupe_consumer"
}},
"interactions": [
{{
"description": "Test Interaction",
"request": {{
"headers": {{
"Accept": "application/json"
}},
"method": "GET",
"path": "/"
}},
"response": {{
"status": 200
}}
}}
],
"metadata": {{
"pactRust": {{
"models": "{}"
}},
"pactSpecification": {{
"version": "3.0.0"
}}
}},
"provider": {{
"name": "dupe_provider"
}}
}}"#, PACT_RUST_VERSION.unwrap())));
}


#[test]
fn write_pact_test_should_not_merge_pacts_with_conflicts() {
let pact = RequestResponsePact { consumer: Consumer { name: "write_pact_test_consumer".to_string() },
Expand Down
Loading
Loading