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

Added tests for runfiles::parse_repo_mapping #2869

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
112 changes: 112 additions & 0 deletions tools/runfiles/runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ impl std::fmt::Display for RunfilesError {

impl std::error::Error for RunfilesError {}

impl PartialEq for RunfilesError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::RunfilesDirIoError(l0), Self::RunfilesDirIoError(r0)) => {
l0.to_string() == r0.to_string()
}
(Self::RunfilesManifestIoError(l0), Self::RunfilesManifestIoError(r0)) => {
l0.to_string() == r0.to_string()
}
(Self::RepoMappingIoError(l0), Self::RepoMappingIoError(r0)) => {
l0.to_string() == r0.to_string()
}
(Self::RunfileNotFound(l0), Self::RunfileNotFound(r0)) => l0 == r0,
(Self::RunfileIoError(l0), Self::RunfileIoError(r0)) => {
l0.to_string() == r0.to_string()
}
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}

/// A specialized [`std::result::Result`] type for
pub type Result<T> = std::result::Result<T, RunfilesError>;

Expand Down Expand Up @@ -428,4 +449,95 @@ mod test {

assert_eq!(r.rlocation("does/not/exist"), None);
}

fn dedent(text: &str) -> String {
text.lines()
.map(|l| l.trim_start())
.collect::<Vec<&str>>()
.join("\n")
}

#[test]
fn test_parse_repo_mapping() {
let temp_dir = PathBuf::from(std::env::var("TEST_TMPDIR").unwrap());
std::fs::create_dir_all(&temp_dir).unwrap();

let valid = temp_dir.join("test_parse_repo_mapping.txt");
std::fs::write(
&valid,
dedent(
r#",rules_rust,rules_rust
bazel_tools,__main__,rules_rust
local_config_cc,rules_rust,rules_rust
local_config_sh,rules_rust,rules_rust
local_config_xcode,rules_rust,rules_rust
platforms,rules_rust,rules_rust
rules_rust_tinyjson,rules_rust,rules_rust
rust_darwin_aarch64__aarch64-apple-darwin__stable_tools,rules_rust,rules_rust
"#,
),
)
.unwrap();

assert_eq!(
parse_repo_mapping(valid),
Ok(RepoMapping::from([
(
("local_config_xcode".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
),
(
("platforms".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
),
(
(
"rust_darwin_aarch64__aarch64-apple-darwin__stable_tools".to_owned(),
"rules_rust".to_owned()
),
"rules_rust".to_owned()
),
(
("rules_rust_tinyjson".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
),
(
("local_config_sh".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
),
(
("bazel_tools".to_owned(), "__main__".to_owned()),
"rules_rust".to_owned()
),
(
("local_config_cc".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
),
(
("".to_owned(), "rules_rust".to_owned()),
"rules_rust".to_owned()
)
]))
);
}

#[test]
fn test_parse_repo_mapping_invalid_file() {
let temp_dir = PathBuf::from(std::env::var("TEST_TMPDIR").unwrap());
std::fs::create_dir_all(&temp_dir).unwrap();

let invalid = temp_dir.join("test_parse_repo_mapping_invalid_file.txt");

assert!(matches!(
parse_repo_mapping(invalid.clone()).err().unwrap(),
RunfilesError::RepoMappingIoError(_)
));

std::fs::write(&invalid, "invalid").unwrap();

assert_eq!(
parse_repo_mapping(invalid),
Err(RunfilesError::RepoMappingInvalidFormat),
);
}
}
Loading