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

chore: unify assert on fixtures #1142

Merged
merged 1 commit into from
Sep 4, 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
103 changes: 49 additions & 54 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,32 @@ mod tests {

use super::*;

macro_rules! assert_eq_fixture_file {
($expected_path:expr, $actual_path:expr) => {{
let actual = std::fs::read_to_string($actual_path).unwrap();

// Normalizes windows and Linux-style EOL
let actual = actual.replace("\r\n", "\n");

assert_eq_fixture_contents!($expected_path, actual);
}};
}

macro_rules! assert_eq_fixture_contents {
($expected_path:expr, $actual:expr) => {{
let expected = std::fs::read_to_string($expected_path).unwrap();

// Normalizes windows and Linux-style EOL
let expected = expected.replace("\r\n", "\n");

if expected != $actual {
std::fs::write($expected_path, &$actual).unwrap();
}

assert_eq!(expected, $actual);
}};
}

/// An example service generator that generates a trait with methods corresponding to the
/// service methods.
struct ServiceTraitGenerator;
Expand Down Expand Up @@ -427,19 +453,13 @@ mod tests {

config.compile_fds(fds).unwrap();

let out_file = tempdir.path().join("helloworld.rs");
#[cfg(feature = "format")]
let expected_content =
read_all_content("src/fixtures/helloworld/_expected_helloworld_formatted.rs")
.replace("\r\n", "\n");
#[cfg(not(feature = "format"))]
let expected_content = read_all_content("src/fixtures/helloworld/_expected_helloworld.rs")
.replace("\r\n", "\n");
let content = read_all_content(out_file).replace("\r\n", "\n");
assert_eq!(
expected_content, content,
"Unexpected content: \n{}",
content
assert_eq_fixture_file!(
if cfg!(feature = "format") {
"src/fixtures/helloworld/_expected_helloworld_formatted.rs"
} else {
"src/fixtures/helloworld/_expected_helloworld.rs"
},
tempdir.path().join("helloworld.rs")
);
}

Expand Down Expand Up @@ -471,12 +491,10 @@ mod tests {
assert!(!contents.is_empty());
} else {
// The file wasn't generated so the result include file should not reference it
let expected = read_all_content("src/fixtures/imports_empty/_expected_include.rs");
let actual = read_all_content(tempdir.path().join(Path::new(include_file)));
// Normalizes windows and Linux-style EOL
let expected = expected.replace("\r\n", "\n");
let actual = actual.replace("\r\n", "\n");
assert_eq!(expected, actual);
assert_eq_fixture_file!(
"src/fixtures/imports_empty/_expected_include.rs",
tempdir.path().join(Path::new(include_file))
);
}
}

Expand All @@ -495,24 +513,13 @@ mod tests {
)
.unwrap();

let out_file = tempdir.path().join("field_attributes.rs");

let content = read_all_content(out_file).replace("\r\n", "\n");

#[cfg(feature = "format")]
let expected_content = read_all_content(
"src/fixtures/field_attributes/_expected_field_attributes_formatted.rs",
)
.replace("\r\n", "\n");
#[cfg(not(feature = "format"))]
let expected_content =
read_all_content("src/fixtures/field_attributes/_expected_field_attributes.rs")
.replace("\r\n", "\n");

assert_eq!(
expected_content, content,
"Unexpected content: \n{}",
content
assert_eq_fixture_file!(
if cfg!(feature = "format") {
"src/fixtures/field_attributes/_expected_field_attributes_formatted.rs"
} else {
"src/fixtures/field_attributes/_expected_field_attributes.rs"
},
tempdir.path().join("field_attributes.rs")
);
}

Expand Down Expand Up @@ -543,23 +550,13 @@ mod tests {
)
.unwrap();

let expected = read_all_content("src/fixtures/alphabet/_expected_include.rs");
let actual = read_all_content(tempdir.path().join(Path::new(include_file)));
// Normalizes windows and Linux-style EOL
let expected = expected.replace("\r\n", "\n");
let actual = actual.replace("\r\n", "\n");

assert_eq!(expected, actual);
assert_eq_fixture_file!(
"src/fixtures/alphabet/_expected_include.rs",
tempdir.path().join(Path::new(include_file))
);
}
}

fn read_all_content(filepath: impl AsRef<Path>) -> String {
let mut f = File::open(filepath).unwrap();
let mut content = String::new();
f.read_to_string(&mut content).unwrap();
content
}

#[test]
fn write_includes() {
let modules = [
Expand All @@ -582,9 +579,7 @@ mod tests {
.default_package_filename("_.default")
.write_includes(modules.iter().collect(), &mut buf, None, &file_names)
.unwrap();
let expected =
read_all_content("src/fixtures/write_includes/_.includes.rs").replace("\r\n", "\n");
let actual = String::from_utf8(buf).unwrap();
assert_eq!(expected, actual);
assert_eq_fixture_contents!("src/fixtures/write_includes/_.includes.rs", actual);
}
}
68 changes: 25 additions & 43 deletions tests/src/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// protoc on Windows outputs \r\n line endings.
#![cfg(not(windows))]
#![cfg(feature = "std")]

use std::fs;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

macro_rules! assert_eq_bootstrapped_file {
($expected_path:expr, $actual_path:expr) => {{
let expected = std::fs::read_to_string($expected_path).unwrap();
let actual = std::fs::read_to_string($actual_path).unwrap();

// Normalizes windows and Linux-style EOL
let expected = expected.replace("\r\n", "\n");
let actual = actual.replace("\r\n", "\n");

if expected != actual {
std::fs::write($expected_path, &actual).unwrap();
}

assert_eq!(expected, actual);
}};
}

/// Test which bootstraps protobuf.rs and compiler.rs from the .proto definitions in the Protobuf
/// repo. Ensures that the checked-in compiled versions are up-to-date.
#[test]
Expand Down Expand Up @@ -49,49 +61,19 @@ fn bootstrap() {
)
.unwrap();

let mut bootstrapped_protobuf = String::new();
fs::File::open(tempdir.path().join("google.protobuf.rs"))
.unwrap()
.read_to_string(&mut bootstrapped_protobuf)
.unwrap();

let mut bootstrapped_compiler = String::new();
fs::File::open(tempdir.path().join("google.protobuf.compiler.rs"))
.unwrap()
.read_to_string(&mut bootstrapped_compiler)
.unwrap();

let src = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("no parent")
.join("prost-types")
.join("src");

let mut protobuf = String::new();
fs::File::open(src.join("protobuf.rs"))
.unwrap()
.read_to_string(&mut protobuf)
.unwrap();

let mut compiler = String::new();
fs::File::open(src.join("compiler.rs"))
.unwrap()
.read_to_string(&mut compiler)
.unwrap();

if protobuf != bootstrapped_protobuf {
fs::File::create(src.join("protobuf.rs"))
.unwrap()
.write_all(bootstrapped_protobuf.as_bytes())
.unwrap();
}
if compiler != bootstrapped_compiler {
fs::File::create(src.join("compiler.rs"))
.unwrap()
.write_all(bootstrapped_compiler.as_bytes())
.unwrap();
}
assert_eq_bootstrapped_file!(
src.join("protobuf.rs"),
tempdir.path().join("google.protobuf.rs")
);

assert_eq!(protobuf, bootstrapped_protobuf);
assert_eq!(compiler, bootstrapped_compiler);
assert_eq_bootstrapped_file!(
src.join("compiler.rs"),
tempdir.path().join("google.protobuf.compiler.rs")
);
}