Skip to content

Commit

Permalink
Add some extra diff-based testing utilities
Browse files Browse the repository at this point in the history
These will be used in the imports tests to allow snapshotting diffs of the
relevant imports.lock files.
  • Loading branch information
mystor committed Aug 19, 2022
1 parent 908b917 commit f6546d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ features = [

[dev-dependencies]
insta = "1.15.0"
similar = "2.1.0"
20 changes: 20 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ impl Store {
Ok(())
}

/// Mock `commit`. Returns the serialized value for each file in the store.
/// Doesn't take `self` by value so that it can continue to be used.
#[cfg(test)]
pub fn mock_commit(&self) -> SortedMap<String, String> {
let mut audits = Vec::new();
let mut config = Vec::new();
let mut imports = Vec::new();
store_audits(&mut audits, self.audits.clone()).unwrap();
store_config(&mut config, self.config.clone()).unwrap();
store_imports(&mut imports, self.imports.clone()).unwrap();

[
(AUDITS_TOML.to_owned(), String::from_utf8(audits).unwrap()),
(CONFIG_TOML.to_owned(), String::from_utf8(config).unwrap()),
(IMPORTS_LOCK.to_owned(), String::from_utf8(imports).unwrap()),
]
.into_iter()
.collect()
}

/// Validate the store's integrity
#[allow(clippy::for_kv_map)]
pub fn validate(&self) -> Result<(), StoreValidateErrors> {
Expand Down
28 changes: 27 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
editor::Editor,
format::{
AuditKind, CriteriaName, CriteriaStr, Delta, DependencyCriteria, FastMap, MetaConfig,
PackageName, PackageStr, PolicyEntry, VersionReq, SAFE_TO_DEPLOY, SAFE_TO_RUN,
PackageName, PackageStr, PolicyEntry, SortedSet, VersionReq, SAFE_TO_DEPLOY, SAFE_TO_RUN,
},
init_files,
out::Out,
Expand Down Expand Up @@ -1152,6 +1152,32 @@ impl Out for BasicTestOutput {
}
}

/// Format a diff between the old and new strings for reporting.
fn generate_diff(old: &str, new: &str) -> String {
similar::utils::diff_lines(similar::Algorithm::Myers, old, new)
.into_iter()
.map(|(tag, line)| format!("{}{}", tag, line))
.collect()
}

/// Generate a diff between two values returned from `Store::mock_commit`.
fn diff_store_commits(old: &SortedMap<String, String>, new: &SortedMap<String, String>) -> String {
use std::fmt::Write;
let mut result = String::new();
let keys = old.keys().chain(new.keys()).collect::<SortedSet<&String>>();
for key in keys {
let old = old.get(key).map(|s| &s[..]).unwrap_or("");
let new = new.get(key).map(|s| &s[..]).unwrap_or("");
if old == new {
writeln!(&mut result, "{key}: (unchanged)").unwrap();
continue;
}
let diff = generate_diff(old, new);
writeln!(&mut result, "{key}:\n{diff}").unwrap();
}
result
}

// TESTING BACKLOG:
//
// * custom policies
Expand Down

0 comments on commit f6546d8

Please sign in to comment.