diff --git a/src/main.rs b/src/main.rs index 3403d5a..ad5a365 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,13 +114,13 @@ fn cli() -> Result<(), Error> { let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config).change_context(Error::Io)?); match args.command { - Command::Validate => ownership.validate(false).change_context(Error::ValidationFailed)?, + Command::Validate => ownership.validate().change_context(Error::ValidationFailed)?, Command::Generate => { std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?; } Command::GenerateAndValidate => { std::fs::write(codeowners_file_path, ownership.generate_file()).change_context(Error::Io)?; - ownership.validate(true).change_context(Error::ValidationFailed)? + ownership.validate().change_context(Error::ValidationFailed)? } Command::ForFile { name } => { let file_owners = ownership.for_file(&name).change_context(Error::Io)?; diff --git a/src/ownership.rs b/src/ownership.rs index 3ac5b70..eb1f07a 100644 --- a/src/ownership.rs +++ b/src/ownership.rs @@ -86,7 +86,7 @@ impl Ownership { } #[instrument(level = "debug", skip_all)] - pub fn validate(&self, skip_codeowners_file_validation: bool) -> Result<(), ValidatorErrors> { + pub fn validate(&self) -> Result<(), ValidatorErrors> { info!("validating file ownership"); let validator = Validator { project: self.project.clone(), @@ -94,7 +94,7 @@ impl Ownership { file_generator: FileGenerator { mappers: self.mappers() }, }; - validator.validate(skip_codeowners_file_validation) + validator.validate() } #[instrument(level = "debug", skip_all)] diff --git a/src/ownership/validator.rs b/src/ownership/validator.rs index 2e49800..75dd7be 100644 --- a/src/ownership/validator.rs +++ b/src/ownership/validator.rs @@ -36,7 +36,7 @@ pub struct Errors(Vec); impl Validator { #[instrument(level = "debug", skip_all)] - pub fn validate(&self, skip_codeowners_file_validation: bool) -> Result<(), Errors> { + pub fn validate(&self) -> Result<(), Errors> { let mut validation_errors = Vec::new(); debug!("validate_invalid_team"); @@ -45,10 +45,8 @@ impl Validator { debug!("validate_file_ownership"); validation_errors.append(&mut self.validate_file_ownership()); - if !skip_codeowners_file_validation { - debug!("validate_codeowners_file"); - validation_errors.append(&mut self.validate_codeowners_file()); - } + debug!("validate_codeowners_file"); + validation_errors.append(&mut self.validate_codeowners_file()); if validation_errors.is_empty() { Ok(()) @@ -129,10 +127,15 @@ impl Validator { fn validate_codeowners_file(&self) -> Vec { let generated_file = self.file_generator.generate_file(); - if generated_file != self.project.codeowners_file { - vec![Error::CodeownershipFileIsStale] - } else { - vec![] + match self.project.get_codeowners_file() { + Ok(current_file) => { + if generated_file != current_file { + vec![Error::CodeownershipFileIsStale] + } else { + vec![] + } + } + Err(_) => vec![Error::CodeownershipFileIsStale], // Treat any read error as stale file } } diff --git a/src/project.rs b/src/project.rs index 3507d4f..e80c7b9 100644 --- a/src/project.rs +++ b/src/project.rs @@ -23,7 +23,7 @@ pub struct Project { pub packages: Vec, pub vendored_gems: Vec, pub teams: Vec, - pub codeowners_file: String, + pub codeowners_file_path: PathBuf, pub directory_codeowner_files: Vec, } @@ -249,12 +249,6 @@ impl Project { "finished scanning project", ); - let codeowners_file: String = if codeowners_file_path.exists() { - std::fs::read_to_string(codeowners_file_path).change_context(Error::Io)? - } else { - "".to_owned() - }; - let owned_files = owned_files(owned_file_paths); Ok(Project { @@ -263,11 +257,20 @@ impl Project { vendored_gems, teams, packages, - codeowners_file, + codeowners_file_path: codeowners_file_path.to_path_buf(), directory_codeowner_files, }) } + pub fn get_codeowners_file(&self) -> Result { + let codeowners_file: String = if self.codeowners_file_path.exists() { + std::fs::read_to_string(&self.codeowners_file_path).change_context(Error::Io)? + } else { + "".to_owned() + }; + Ok(codeowners_file) + } + pub fn relative_path<'a>(&'a self, absolute_path: &'a Path) -> &'a Path { absolute_path .strip_prefix(&self.base_path)