Skip to content

Commit

Permalink
Fix tests to make precommit.sh run on Windows (#1831)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com>
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
  • Loading branch information
3 people authored May 29, 2024
1 parent e521083 commit 2dd47fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
9 changes: 5 additions & 4 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,18 @@ fn gather_and_compare(registry: prometheus::Registry, expected: String, name: &'
let metric_families = registry.gather();
encoder.encode(&metric_families, &mut output).unwrap();

let expected = get_platform_specific_string(expected);
let output_string = get_platform_specific_string(String::from_utf8(output).unwrap());

assert_eq!(output_string, expected, "{name}");
}

/// Returns a String which uses the platform specific new line feed character.
fn get_platform_specific_string(input: String) -> String {
if cfg!(windows) {
input.replace('\n', "\r\n")
} else {
input
if cfg!(windows) && !input.ends_with("\r\n") && input.ends_with('\n') {
return input.replace('\n', "\r\n");
}
input
}

#[test]
Expand Down Expand Up @@ -812,6 +812,7 @@ fn duplicate_metrics() {
.expected_files
.into_iter()
.map(|f| fs::read_to_string(Path::new("./tests/data").join(f)).expect(f))
.map(get_platform_specific_string)
.collect();
gather_and_compare_multi(registry, possible_matches, tc.name);
}
Expand Down
11 changes: 5 additions & 6 deletions opentelemetry-proto/tests/grpc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const TONIC_INCLUDES: &[&str] = &["src/proto/opentelemetry-proto", "src/proto"];

#[test]
fn build_tonic() {
let before_build = build_content_map(TONIC_OUT_DIR, false);
let before_build = build_content_map(TONIC_OUT_DIR, true);

let out_dir = TempDir::new().expect("failed to create temp dir to store the generated files");

Expand Down Expand Up @@ -130,13 +130,12 @@ fn build_content_map(path: impl AsRef<Path>, normalize_line_feed: bool) -> HashM
.collect()
}

/// Returns a String with the platform specific new line feed character.
/// Returns a String which uses the platform specific new line feed character.
fn get_platform_specific_string(input: String) -> String {
if cfg!(windows) {
input.replace('\n', "\r\n")
} else {
input
if cfg!(windows) && !input.ends_with("\r\n") && input.ends_with('\n') {
return input.replace('\n', "\r\n");
}
input
}

fn ensure_files_are_same(
Expand Down

0 comments on commit 2dd47fd

Please sign in to comment.