Skip to content

Commit

Permalink
Merge pull request #86 from jsuereth/pr-update-tests-with-diff
Browse files Browse the repository at this point in the history
Extract diff utility and use it in weaver_forge tests
  • Loading branch information
jsuereth authored Apr 4, 2024
2 parents 82b26c7 + 577b741 commit 4f4f740
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 11 deletions.
11 changes: 10 additions & 1 deletion 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 @@ -49,6 +49,7 @@ name = "weaver"

[dependencies]
# local crates dependencies
weaver_diff = { path = "crates/weaver_diff" }
weaver_logger = { path = "crates/weaver_logger" }
weaver_resolver = { path = "crates/weaver_resolver" }
weaver_template = { path = "crates/weaver_template" }
Expand Down
22 changes: 22 additions & 0 deletions crates/weaver_diff/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "weaver_diff"
version = "0.1.0"
authors.workspace = true
repository.workspace = true
license.workspace = true
publish.workspace = true
edition.workspace = true
rust-version.workspace = true

[dependencies]
similar = "2.5.0"


[lints]
workspace = true

[package.metadata.cargo-machete]
# force cargo machete to ignore the following crates
# remove this section once this crate is integrated into the rest
# of the project
ignored = []
7 changes: 7 additions & 0 deletions crates/weaver_diff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Weaver diff library

Status: **Work-In-Progress**

This crate provides a minimal "diff" library with colored output.

It is designed to hide actual implementation details of diff and dependency selection from the rest of the weaver project.
5 changes: 5 additions & 0 deletions crates/weaver_diff/allowed-external-types.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
# This is used with cargo-check-external-types to reduce the surface area of downstream crates from
# the public API. Ideally this can have a few exceptions as possible.
allowed_external_types = []
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// SPDX-License-Identifier: Apache-2.0

//! Text diffing utilities.
//! This crate provides bare minimum support for colorized string differencing.
use similar::TextDiff;

const GREEN: &str = "\x1b[32m";
const RED: &str = "\x1b[31m";
const RESET: &str = "\x1b[0m";

// TODO - allow disabling colors
/// Constructs a "diff" string of the current snippet vs. updated on
/// outlining any changes that may need to be updated.
/// Constructs a "diff" string of the original vs. updated.
/// Will create colorized (ANSI) output w/ `+` representing additions and `-` representing removals.
#[must_use]
pub fn diff_output(original: &str, updated: &str) -> String {
let mut result = String::new();
let diff = similar::TextDiff::from_lines(original, updated);
let diff = TextDiff::from_lines(original, updated);
for change in diff.iter_all_changes() {
let sign = match change.tag() {
similar::ChangeTag::Delete => "-",
Expand Down
1 change: 1 addition & 0 deletions crates/weaver_forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version.workspace = true
workspace = true

[dependencies]
weaver_diff = { path = "../weaver_diff" }
weaver_logger = { path = "../weaver_logger" }
weaver_resolver = { path = "../weaver_resolver" }
weaver_resolved_schema = { path = "../weaver_resolved_schema" }
Expand Down
7 changes: 5 additions & 2 deletions crates/weaver_forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ mod tests {
use walkdir::WalkDir;

use crate::debug::print_dedup_errors;
use weaver_diff::diff_output;
use weaver_logger::TestLogger;
use weaver_resolver::SchemaResolver;
use weaver_semconv::SemConvRegistry;
Expand Down Expand Up @@ -519,8 +520,10 @@ mod tests {
observed_dir.as_ref().join(file)
);

eprintln!("Expected file content: {}", file1_content);
eprintln!("Observed file content: {}", file2_content);
eprintln!(
"Found differences:\n{}",
diff_output(&file1_content, &file2_content)
);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/weaver_semconv_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ rust-version.workspace = true

[dependencies]
weaver_cache = { path = "../weaver_cache" }
weaver_diff = { path = "../weaver_diff" }
weaver_logger = { path = "../weaver_logger" }
weaver_resolver = { path = "../weaver_resolver" }
weaver_resolved_schema = { path = "../weaver_resolved_schema" }
weaver_semconv = { path = "../weaver_semconv" }
itertools = "0.12.1"
similar = "2.5.0"
nom = "7.1.3"

thiserror.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/weaver_semconv_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::fs;
use weaver_cache::Cache;
use weaver_diff::diff_output;
use weaver_logger::Logger;
use weaver_resolved_schema::attribute::{Attribute, AttributeRef};
use weaver_resolved_schema::registry::{Group, Registry};
Expand All @@ -14,7 +15,6 @@ use weaver_resolver::SchemaResolver;
use weaver_semconv::path::RegistryPath;
use weaver_semconv::SemConvRegistry;

mod diff;
mod gen;
mod parser;

Expand Down Expand Up @@ -46,7 +46,7 @@ pub enum Error {
filter: String,
},
/// Errors thrown when we are running a dry run and markdown doesn't match.
#[error("Markdown is not equal:\n{}", diff::diff_output(.original, .updated))]
#[error("Markdown is not equal:\n{}", diff_output(.original, .updated))]
MarkdownIsNotEqual {
/// Original markdown value.
original: String,
Expand Down

0 comments on commit 4f4f740

Please sign in to comment.