Skip to content

Commit 2dd47fd

Browse files
utpillaTommyCppcijothomas
authored
Fix tests to make precommit.sh run on Windows (#1831)
Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com> Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
1 parent e521083 commit 2dd47fd

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

opentelemetry-prometheus/tests/integration_test.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,18 @@ fn gather_and_compare(registry: prometheus::Registry, expected: String, name: &'
406406
let metric_families = registry.gather();
407407
encoder.encode(&metric_families, &mut output).unwrap();
408408

409+
let expected = get_platform_specific_string(expected);
409410
let output_string = get_platform_specific_string(String::from_utf8(output).unwrap());
410411

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

414415
/// Returns a String which uses the platform specific new line feed character.
415416
fn get_platform_specific_string(input: String) -> String {
416-
if cfg!(windows) {
417-
input.replace('\n', "\r\n")
418-
} else {
419-
input
417+
if cfg!(windows) && !input.ends_with("\r\n") && input.ends_with('\n') {
418+
return input.replace('\n', "\r\n");
420419
}
420+
input
421421
}
422422

423423
#[test]
@@ -812,6 +812,7 @@ fn duplicate_metrics() {
812812
.expected_files
813813
.into_iter()
814814
.map(|f| fs::read_to_string(Path::new("./tests/data").join(f)).expect(f))
815+
.map(get_platform_specific_string)
815816
.collect();
816817
gather_and_compare_multi(registry, possible_matches, tc.name);
817818
}

opentelemetry-proto/tests/grpc_build.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const TONIC_INCLUDES: &[&str] = &["src/proto/opentelemetry-proto", "src/proto"];
1818

1919
#[test]
2020
fn build_tonic() {
21-
let before_build = build_content_map(TONIC_OUT_DIR, false);
21+
let before_build = build_content_map(TONIC_OUT_DIR, true);
2222

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

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

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

142141
fn ensure_files_are_same(

0 commit comments

Comments
 (0)