diff --git a/git-checks-core/CHANGELOG.md b/git-checks-core/CHANGELOG.md index e5d1eef9d21f0bc1af2efa3519959d4727d3897a..f51b6d42e3a5544b1ebbf6219c4699d850b21024 100644 --- a/git-checks-core/CHANGELOG.md +++ b/git-checks-core/CHANGELOG.md @@ -1,3 +1,7 @@ +# v1.3.1 (unreleased) + + * `CheckResult` now has an `impl Clone`. + # v1.3.0 * `GitCheckConfiguration::run_topic_multi_base` has been added to exclude diff --git a/git-checks-core/src/check.rs b/git-checks-core/src/check.rs index a8dd5c66160e7731504b991e60a393ae04d93491..ab6622f341bf80ce8ff922f97e95015d0aeb42c3 100644 --- a/git-checks-core/src/check.rs +++ b/git-checks-core/src/check.rs @@ -15,7 +15,7 @@ use crate::commit::{Commit, Content, Topic}; use crate::context::CheckGitContext; /// The results of a check. -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub struct CheckResult { /// The warnings from running checks. warnings: Vec, @@ -248,3 +248,201 @@ where self.check(ctx, topic) } } + +#[cfg(test)] +mod tests { + use crate::CheckResult; + + #[test] + fn test_check_result_add_warning() { + let mut result = CheckResult::new(); + assert!(result.warnings().is_empty()); + result.add_warning("warning"); + assert!(!result.warnings().is_empty()); + } + + #[test] + fn test_check_result_add_error() { + let mut result = CheckResult::new(); + assert!(result.errors().is_empty()); + result.add_error("error"); + assert!(!result.errors().is_empty()); + } + + #[test] + fn test_check_result_add_alert() { + let mut result = CheckResult::new(); + assert!(result.alerts().is_empty()); + result.add_alert("error", true); + assert!(!result.alerts().is_empty()); + } + + #[test] + fn test_check_result_make_temporary() { + let mut result = CheckResult::new(); + assert!(!result.temporary()); + result.make_temporary(); + assert!(result.temporary()); + } + + #[test] + fn test_check_result_whitelist() { + let mut result = CheckResult::new(); + assert!(!result.allowed()); + result.whitelist(); + assert!(result.allowed()); + } + + #[test] + fn test_check_result_combine_temporary() { + let temp_result = { + let mut result = CheckResult::new(); + result.make_temporary(); + result + }; + let non_temp_result = CheckResult::new(); + + let items = &[ + (&temp_result, &non_temp_result, true), + (&temp_result, &temp_result, true), + (&non_temp_result, &non_temp_result, false), + (&non_temp_result, &temp_result, true), + ]; + + for (l, r, e) in items { + assert_eq!((**l).clone().combine((**r).clone()).temporary(), *e); + } + } + + #[test] + fn test_check_result_combine_whitelist() { + let temp_result = { + let mut result = CheckResult::new(); + result.whitelist(); + result + }; + let non_temp_result = CheckResult::new(); + + let items = &[ + (&temp_result, &non_temp_result, true), + (&temp_result, &temp_result, true), + (&non_temp_result, &non_temp_result, false), + (&non_temp_result, &temp_result, true), + ]; + + for (l, r, e) in items { + assert_eq!((**l).clone().combine((**r).clone()).allowed(), *e); + } + } + + mod mock { + use std::sync::Mutex; + + use crate::impl_prelude::*; + + #[derive(Debug)] + pub struct MockCheck { + checked: Mutex, + } + + impl Default for MockCheck { + fn default() -> Self { + Self { + checked: Mutex::new(false), + } + } + } + + impl Drop for MockCheck { + fn drop(&mut self) { + let flag = self.checked.lock().expect("poisoned mock check lock"); + assert!(*flag); + } + } + + impl MockCheck { + fn trip(&self) { + let mut flag = self.checked.lock().expect("poisoned mock check lock"); + *flag = true; + } + } + + impl ContentCheck for MockCheck { + fn name(&self) -> &str { + self.trip(); + "mock-check" + } + + fn check( + &self, + _: &CheckGitContext, + _: &dyn Content, + ) -> Result> { + self.trip(); + Ok(CheckResult::new()) + } + } + } + + const TARGET_COMMIT: &str = "27ff3ef5532d76afa046f76f4dd8f588dc3e83c3"; + const SIMPLE_COMMIT: &str = "43adb8173eb6d7a39f98e1ec3351cf27414c9aa1"; + + fn test_run_check(conf: &crate::GitCheckConfiguration, name: &str) { + use std::path::Path; + + use git_workarea::{CommitId, GitContext, Identity}; + + let gitdir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.git")); + if !gitdir.exists() { + panic!("The tests must be run from a git checkout."); + } + + let ctx = GitContext::new(gitdir); + let identity = Identity::new( + "Rust Git Checks Core Tests", + "rust-git-checks-core@example.com", + ); + conf.run_topic( + &ctx, + name, + &CommitId::new(TARGET_COMMIT), + &CommitId::new(SIMPLE_COMMIT), + &identity, + ) + .unwrap(); + } + + #[test] + fn test_impl_check_for_content_name() { + use crate::{Check, ContentCheck}; + + let check = mock::MockCheck::default(); + + assert_eq!(Check::name(&check), ContentCheck::name(&check)); + } + + #[test] + fn test_impl_check_for_content_check() { + let check = mock::MockCheck::default(); + let mut conf = crate::GitCheckConfiguration::new(); + conf.add_check(&check); + test_run_check(&conf, "test_impl_check_for_content_check"); + } + + #[test] + fn test_impl_topiccheck_for_content_name() { + use crate::{ContentCheck, TopicCheck}; + + let check = mock::MockCheck::default(); + + assert_eq!(TopicCheck::name(&check), ContentCheck::name(&check)); + } + + #[test] + fn test_impl_topiccheck_for_content_check() { + let check = mock::MockCheck::default(); + let mut conf = crate::GitCheckConfiguration::new(); + conf.add_topic_check(&check); + test_run_check(&conf, "test_impl_topiccheck_for_content_check"); + } +} diff --git a/git-checks-core/src/commit.rs b/git-checks-core/src/commit.rs index d896b6e5ec7e509761842ee8a294fc04ec81e80e..a36954e6c50c093687e0ac12bf884a55097d0221 100644 --- a/git-checks-core/src/commit.rs +++ b/git-checks-core/src/commit.rs @@ -1005,6 +1005,17 @@ mod tests { } } + #[test] + fn test_filename_partial_eq() { + let fn_a1 = FileName::new("a").unwrap(); + let fn_a2 = FileName::new("a").unwrap(); + let fn_b = FileName::new("b").unwrap(); + + assert_eq!(fn_a1, fn_a2); + assert_ne!(fn_a1, fn_b); + assert_ne!(fn_a2, fn_b); + } + const BAD_COMMIT: &str = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; #[test] @@ -2493,4 +2504,35 @@ mod tests { assert_eq!(topic.diffs.len(), 0); } + + #[test] + fn test_impl_content_for_commit_path_diff() { + let ctx = make_context(); + + let commit = Commit::new(&ctx, &CommitId::new(CHANGES_COMMIT)).unwrap(); + let file = FileName::Normal("src/context.rs".into()); + + assert_eq!( + Content::path_diff(&commit, &file).unwrap(), + commit.path_diff(&file).unwrap(), + ); + } + + #[test] + fn test_impl_content_for_topic_path_diff() { + let ctx = make_context(); + + let topic = Topic::new( + &ctx, + &CommitId::new(ROOT_COMMIT), + &CommitId::new(CHANGES_COMMIT), + ) + .unwrap(); + let file = FileName::Normal("src/context.rs".into()); + + assert_eq!( + Content::path_diff(&topic, &file).unwrap(), + topic.path_diff(&file).unwrap(), + ); + } } diff --git a/git-checks/src/allow_robot.rs b/git-checks/src/allow_robot.rs index c50f46a4cdea75e81bb2ba8691e37bb8300f620a..4897c4ad7ca2ecd02d8b0ad926f210b0a3a11ccb 100644 --- a/git-checks/src/allow_robot.rs +++ b/git-checks/src/allow_robot.rs @@ -24,7 +24,7 @@ pub struct AllowRobot { impl AllowRobot { /// Create a new builder. pub fn builder() -> AllowRobotBuilder { - AllowRobotBuilder::default() + Default::default() } } diff --git a/git-checks/src/bad_commit.rs b/git-checks/src/bad_commit.rs index ff0bf634f99b3bc9d1455a8517880ad60a1aa866..01eb40753cf25fb6e3dc44a51810e913574e2af2 100644 --- a/git-checks/src/bad_commit.rs +++ b/git-checks/src/bad_commit.rs @@ -49,7 +49,7 @@ pub struct BadCommit { impl BadCommit { /// Create a new builder. pub fn builder() -> BadCommitBuilder { - BadCommitBuilder::default() + Default::default() } fn apply(&self, result: &mut CheckResult) { diff --git a/git-checks/src/bad_commits.rs b/git-checks/src/bad_commits.rs index a4878cd30fc8c56f66eea33f08b377dc0672256d..70b39e42e77110138d7ee6890f59d182bde4bec4 100644 --- a/git-checks/src/bad_commits.rs +++ b/git-checks/src/bad_commits.rs @@ -58,7 +58,7 @@ impl BadCommitsBuilder { impl BadCommits { /// Create a new builder. pub fn builder() -> BadCommitsBuilder { - BadCommitsBuilder::default() + Default::default() } } @@ -208,7 +208,7 @@ pub(crate) mod config { #[cfg(test)] mod tests { - use git_checks_core::TopicCheck; + use git_checks_core::{Check, TopicCheck}; use git_workarea::CommitId; use crate::test::*; @@ -232,6 +232,15 @@ mod tests { .is_ok()); } + #[test] + fn test_bad_commits_name_commit() { + let check = BadCommits::builder() + .bad_commits([BAD_COMMIT].iter().copied().map(CommitId::new)) + .build() + .unwrap(); + assert_eq!(Check::name(&check), "bad-commits"); + } + #[test] fn test_bad_commits_name_topic() { let check = BadCommits::builder() diff --git a/git-checks/src/binary_format.rs b/git-checks/src/binary_format.rs index 0498a02aac0ba792d94801c1414241e503d0527c..1566bbe5098bc35bebd3fcd0ec0c80de5bb2799b 100644 --- a/git-checks/src/binary_format.rs +++ b/git-checks/src/binary_format.rs @@ -20,6 +20,7 @@ type PeOffset = u32; const PE_OFFSET_SIZE: usize = mem::size_of::(); const PE_MAGIC: &[u8] = &[0x50, 0x45, 0x00, 0x00]; // PE 0x00 0x00 +#[derive(Debug, Clone, Copy, PartialEq)] pub(crate) enum BinaryFormat { Ar, Elf, @@ -53,7 +54,7 @@ fn detect_pe(file: &[u8]) -> bool { // Find the offset in the file. let mut offset = [0; PE_OFFSET_SIZE]; - offset.copy_from_slice(&file[PE_OFFSET..PE_OFFSET + PE_OFFSET_SIZE]); + offset.copy_from_slice(&file[PE_OFFSET..][..PE_OFFSET_SIZE]); // Read the offset. It is in native-endian order and we don't know the endianness of the // binary, so try it as big- and little-endian. @@ -92,3 +93,59 @@ fn detect_binary_format_impl(content: &[u8]) -> Option { None } } + +#[cfg(test)] +mod tests { + use crate::binary_format::{detect_binary_format, BinaryFormat}; + + #[test] + fn test_detect_binary_format_elf() { + assert_eq!(detect_binary_format(b"\x7fELF"), Some(BinaryFormat::Elf)); + } + + #[test] + fn test_detect_binary_format_ar() { + assert_eq!(detect_binary_format(b"!\n"), Some(BinaryFormat::Ar)); + } + + #[test] + fn test_detect_binary_format_macho() { + assert_eq!( + detect_binary_format(b"\xcf\xfa\xed\xfe"), + Some(BinaryFormat::MachO), + ); + assert_eq!( + detect_binary_format(b"\xfe\xed\xfa\xcf"), + Some(BinaryFormat::MachO), + ); + assert_eq!( + detect_binary_format(b"\xca\xfe\xba\xbe"), + Some(BinaryFormat::MachO), + ); + assert_eq!( + detect_binary_format(b"\xbe\xba\xfe\xca"), + Some(BinaryFormat::MachO), + ); + } + + #[test] + fn test_detect_pe() { + use super::{detect_pe, PE_OFFSET, PE_OFFSET_SIZE}; + + assert!(!detect_pe(&[])); + assert!(!detect_pe(&[0; PE_OFFSET])); + assert!(!detect_pe(&[0; PE_OFFSET + PE_OFFSET_SIZE - 1])); + assert!(!detect_pe(&[0; PE_OFFSET + PE_OFFSET_SIZE])); + } + + #[test] + fn test_detect_binary_format_pe() { + let pe_craft_be = + b"buffer_PE\x00\x000123456789012345678901234567890123456789012345678\x00\x00\x00\x07"; + let pe_craft_le = + b"buffer_PE\x00\x000123456789012345678901234567890123456789012345678\x07\x00\x00\x00"; + + assert_eq!(detect_binary_format(pe_craft_be), Some(BinaryFormat::Pe)); + assert_eq!(detect_binary_format(pe_craft_le), Some(BinaryFormat::Pe)); + } +} diff --git a/git-checks/src/changelog.rs b/git-checks/src/changelog.rs index 4cdce51f4d84e1a99583a2df98de0a4242843af8..4ce86eec7e91401d630a0febfaecbe180d8fddc5 100644 --- a/git-checks/src/changelog.rs +++ b/git-checks/src/changelog.rs @@ -178,7 +178,7 @@ pub struct Changelog { impl Changelog { /// Create a new builder. pub fn builder() -> ChangelogBuilder { - ChangelogBuilder::default() + Default::default() } } diff --git a/git-checks/src/check_end_of_line.rs b/git-checks/src/check_end_of_line.rs index 258877067292f25be0efd6301629b56dba11c61c..e6f206ac1cd97c496ab6a64cbba68a92ab4ff545 100644 --- a/git-checks/src/check_end_of_line.rs +++ b/git-checks/src/check_end_of_line.rs @@ -16,7 +16,7 @@ pub struct CheckEndOfLine {} impl CheckEndOfLine { /// Create a new builder. pub fn builder() -> CheckEndOfLineBuilder { - CheckEndOfLineBuilder::default() + Default::default() } } @@ -97,7 +97,7 @@ pub(crate) mod config { type Check = CheckEndOfLine; fn into_check(self) -> Self::Check { - CheckEndOfLine::default() + Default::default() } } diff --git a/git-checks/src/check_executable_permissions.rs b/git-checks/src/check_executable_permissions.rs index e2fb5064dcde79fcb32c88144a446f7664996897..cd388012822c4f39c984863e66b42b4229a0018c 100644 --- a/git-checks/src/check_executable_permissions.rs +++ b/git-checks/src/check_executable_permissions.rs @@ -43,7 +43,7 @@ impl CheckExecutablePermissionsBuilder { impl CheckExecutablePermissions { /// Create a new builder. pub fn builder() -> CheckExecutablePermissionsBuilder { - CheckExecutablePermissionsBuilder::default() + Default::default() } } diff --git a/git-checks/src/check_size.rs b/git-checks/src/check_size.rs index 71fa2c22479c6fd5c648e05fcb33a3a1eb8ddd65..1f3dd65585e616967f8b19be112d91e1bedb5405 100644 --- a/git-checks/src/check_size.rs +++ b/git-checks/src/check_size.rs @@ -43,7 +43,7 @@ pub struct CheckSize { impl CheckSize { /// Create a new builder. pub fn builder() -> CheckSizeBuilder { - CheckSizeBuilder::default() + Default::default() } } diff --git a/git-checks/src/check_whitespace.rs b/git-checks/src/check_whitespace.rs index e17c8d569d53bcbc675a83c772d19b6f9ce92c7f..bbd7d371d0eb47eefa299abef9d8f3a0fae34052 100644 --- a/git-checks/src/check_whitespace.rs +++ b/git-checks/src/check_whitespace.rs @@ -26,7 +26,7 @@ pub struct CheckWhitespace {} impl CheckWhitespace { /// Create a new builder. pub fn builder() -> CheckWhitespaceBuilder { - CheckWhitespaceBuilder::default() + Default::default() } fn diff_tree( @@ -121,7 +121,7 @@ pub(crate) mod config { type Check = CheckWhitespace; fn into_check(self) -> Self::Check { - CheckWhitespace::default() + Default::default() } } diff --git a/git-checks/src/commit_subject.rs b/git-checks/src/commit_subject.rs index 930abec39f1ad02e5430a2c79225a683135de7a8..0935cbda982d2bc6902a81406b9c5fb0f9360e4f 100644 --- a/git-checks/src/commit_subject.rs +++ b/git-checks/src/commit_subject.rs @@ -147,7 +147,7 @@ impl CommitSubjectBuilder { impl CommitSubject { /// Create a new builder. pub fn builder() -> CommitSubjectBuilder { - CommitSubjectBuilder::default() + Default::default() } /// Whether the summary is generated or not. diff --git a/git-checks/src/fast_forward.rs b/git-checks/src/fast_forward.rs index ee2b5b7b712fd3635706b86872bb6565882ed0b9..c49fb6f1e21131bdfdcabe046e6c2c889747e351 100644 --- a/git-checks/src/fast_forward.rs +++ b/git-checks/src/fast_forward.rs @@ -63,7 +63,7 @@ pub struct FastForward { impl FastForward { /// Create a new builder. pub fn builder() -> FastForwardBuilder { - FastForwardBuilder::default() + Default::default() } } diff --git a/git-checks/src/formatting.rs b/git-checks/src/formatting.rs index 9ff3488fa319a0405afddaa16bbd7d080554aa4f..97e99509d15d8f7ccaa0f932ba40668d8da85ebf 100644 --- a/git-checks/src/formatting.rs +++ b/git-checks/src/formatting.rs @@ -201,7 +201,7 @@ impl FormattingBuilder { impl Formatting { /// Create a new builder. pub fn builder() -> FormattingBuilder { - FormattingBuilder::default() + Default::default() } /// Check a path using the formatter. diff --git a/git-checks/src/invalid_paths.rs b/git-checks/src/invalid_paths.rs index d1514e212b1405b5cfbff1c8d0bb8a67ce0eeaaf..84ee4269052469cdc4f404fe5451d96cbc994df5 100644 --- a/git-checks/src/invalid_paths.rs +++ b/git-checks/src/invalid_paths.rs @@ -51,7 +51,7 @@ const WINDOWS_FORBIDDEN_NAMES: &[&[u8]] = &[ impl InvalidPaths { /// Create a new builder. pub fn builder() -> InvalidPathsBuilder { - InvalidPathsBuilder::default() + Default::default() } fn disallowed_whitespace(&self, c: char) -> bool { diff --git a/git-checks/src/invalid_utf8.rs b/git-checks/src/invalid_utf8.rs index aacb6028cebbe639539a7254824da493fbefa6db..e39acbcf263528cd44e9896e1580525a89980234 100644 --- a/git-checks/src/invalid_utf8.rs +++ b/git-checks/src/invalid_utf8.rs @@ -21,7 +21,7 @@ pub struct InvalidUtf8 {} impl InvalidUtf8 { /// Create a new builder. pub fn builder() -> InvalidUtf8Builder { - InvalidUtf8Builder::default() + Default::default() } } @@ -105,7 +105,7 @@ pub(crate) mod config { type Check = InvalidUtf8; fn into_check(self) -> Self::Check { - InvalidUtf8::default() + Default::default() } } diff --git a/git-checks/src/lfs_pointer.rs b/git-checks/src/lfs_pointer.rs index 311f899853f1c83aee432045c39976b080c1209d..37cb19fbc679249d5320cfb44931707f245bebcf 100644 --- a/git-checks/src/lfs_pointer.rs +++ b/git-checks/src/lfs_pointer.rs @@ -21,7 +21,7 @@ pub struct LfsPointer {} impl LfsPointer { /// Create a new builder. pub fn builder() -> LfsPointerBuilder { - LfsPointerBuilder::default() + Default::default() } fn check_line( @@ -348,7 +348,7 @@ pub(crate) mod config { type Check = LfsPointer; fn into_check(self) -> Self::Check { - LfsPointer::default() + Default::default() } } diff --git a/git-checks/src/reject_bidi.rs b/git-checks/src/reject_bidi.rs index f9a08b04ae45b256f90b116799eb558b8ae138dd..e23ca0b60c6f9f521ed60f58273996030663edb0 100644 --- a/git-checks/src/reject_bidi.rs +++ b/git-checks/src/reject_bidi.rs @@ -34,7 +34,7 @@ pub struct RejectBiDi { impl RejectBiDi { /// Create a new builder. pub fn builder() -> RejectBiDiBuilder { - RejectBiDiBuilder::default() + Default::default() } } diff --git a/git-checks/src/reject_binaries.rs b/git-checks/src/reject_binaries.rs index c8f32325295e04dc56661bc562e52902644e8c57..7e2486270cff7f07428ec46f02fdae8b80e8894c 100644 --- a/git-checks/src/reject_binaries.rs +++ b/git-checks/src/reject_binaries.rs @@ -24,7 +24,7 @@ pub struct RejectBinaries {} impl RejectBinaries { /// Create a new builder. pub fn builder() -> RejectBinariesBuilder { - RejectBinariesBuilder::default() + Default::default() } } @@ -118,7 +118,7 @@ pub(crate) mod config { type Check = RejectBinaries; fn into_check(self) -> Self::Check { - RejectBinaries::default() + Default::default() } } diff --git a/git-checks/src/reject_conflict_paths.rs b/git-checks/src/reject_conflict_paths.rs index 4fd14b327cb5df3f1b436ed9c4c0a11e04e5f7de..138ad8c5e0a26f62fd77f55b86d4376b65e25867 100644 --- a/git-checks/src/reject_conflict_paths.rs +++ b/git-checks/src/reject_conflict_paths.rs @@ -44,7 +44,7 @@ const ORIG_SUFFIX: &str = ".orig"; impl RejectConflictPaths { /// Create a new builder. pub fn builder() -> RejectConflictPathsBuilder { - RejectConflictPathsBuilder::default() + Default::default() } fn check_conflict_path_name( diff --git a/git-checks/src/reject_merges.rs b/git-checks/src/reject_merges.rs index baad3d64e1bac125fd668f46d7449933fec81186..eda2b19cf1c5c269d99a86a0a10c9fc75af70520 100644 --- a/git-checks/src/reject_merges.rs +++ b/git-checks/src/reject_merges.rs @@ -17,7 +17,7 @@ pub struct RejectMerges {} impl RejectMerges { /// Create a new builder. pub fn builder() -> RejectMergesBuilder { - RejectMergesBuilder::default() + Default::default() } } @@ -62,7 +62,7 @@ pub(crate) mod config { type Check = RejectMerges; fn into_check(self) -> Self::Check { - RejectMerges::default() + Default::default() } } diff --git a/git-checks/src/reject_separate_root.rs b/git-checks/src/reject_separate_root.rs index 18248678f0e11468c52551da72571627459ddce9..bd226bcfceee85d724a221ef5c7ed898ffea23a1 100644 --- a/git-checks/src/reject_separate_root.rs +++ b/git-checks/src/reject_separate_root.rs @@ -17,7 +17,7 @@ pub struct RejectSeparateRoot {} impl RejectSeparateRoot { /// Create a new builder. pub fn builder() -> RejectSeparateRootBuilder { - RejectSeparateRootBuilder::default() + Default::default() } } @@ -61,7 +61,7 @@ pub(crate) mod config { type Check = RejectSeparateRoot; fn into_check(self) -> Self::Check { - RejectSeparateRoot::default() + Default::default() } } diff --git a/git-checks/src/reject_symlinks.rs b/git-checks/src/reject_symlinks.rs index 7fc272b42f0bbf1ca58855aa4b22220fcea552f4..c3a9dac34e59b9b286d78fcf12e5b351d8a6cb2f 100644 --- a/git-checks/src/reject_symlinks.rs +++ b/git-checks/src/reject_symlinks.rs @@ -17,7 +17,7 @@ pub struct RejectSymlinks {} impl RejectSymlinks { /// Create a new builder. pub fn builder() -> RejectSymlinksBuilder { - RejectSymlinksBuilder::default() + Default::default() } } @@ -74,7 +74,7 @@ pub(crate) mod config { type Check = RejectSymlinks; fn into_check(self) -> Self::Check { - RejectSymlinks::default() + Default::default() } } diff --git a/git-checks/src/release_branch.rs b/git-checks/src/release_branch.rs index d67da70c366632d01143497dae783059926aa2d7..f13ff04c07d24e655a9b1f4920ee22d6086195c6 100644 --- a/git-checks/src/release_branch.rs +++ b/git-checks/src/release_branch.rs @@ -65,7 +65,7 @@ pub struct ReleaseBranch { impl ReleaseBranch { /// Create a new builder. pub fn builder() -> ReleaseBranchBuilder { - ReleaseBranchBuilder::default() + Default::default() } } diff --git a/git-checks/src/restricted_path.rs b/git-checks/src/restricted_path.rs index 4f26ba45559b7529d123053846e870a3375a40c2..73fe0158e9ade4348a211856fdd983a451426ae1 100644 --- a/git-checks/src/restricted_path.rs +++ b/git-checks/src/restricted_path.rs @@ -29,7 +29,7 @@ pub struct RestrictedPath { impl RestrictedPath { /// Create a new builder. pub fn builder() -> RestrictedPathBuilder { - RestrictedPathBuilder::default() + Default::default() } } diff --git a/git-checks/src/submodule_available.rs b/git-checks/src/submodule_available.rs index 18e554f5fecefdb4e8e943961f5a2df9da283cf8..e05ba18cafc84a538ac0f5bf8ce0c2e30a327be1 100644 --- a/git-checks/src/submodule_available.rs +++ b/git-checks/src/submodule_available.rs @@ -68,7 +68,7 @@ pub struct SubmoduleAvailable { impl SubmoduleAvailable { /// Create a new builder. pub fn builder() -> SubmoduleAvailableBuilder { - SubmoduleAvailableBuilder::default() + Default::default() } } diff --git a/git-checks/src/submodule_rewind.rs b/git-checks/src/submodule_rewind.rs index 23f39ffab4e2e1a2031291ac50c5313ac0b2fcf2..23fd8e77ca6295fdf352ce1f6d8d95e2fdc18f9f 100644 --- a/git-checks/src/submodule_rewind.rs +++ b/git-checks/src/submodule_rewind.rs @@ -47,7 +47,7 @@ pub struct SubmoduleRewind {} impl SubmoduleRewind { /// Create a new builder. pub fn builder() -> SubmoduleRewindBuilder { - SubmoduleRewindBuilder::default() + Default::default() } } @@ -60,13 +60,9 @@ impl Check for SubmoduleRewind { let mut result = CheckResult::new(); for diff in &commit.diffs { - // Ignore diffs which are not submodules on the new side. - if diff.new_mode != "160000" { - continue; - } - - // Ignore submodules which have not been modified. - if diff.status == StatusChange::Deleted || diff.status == StatusChange::Added { + // Ignore diffs which are not submodules on the new side and submodules without a + // history. + if diff.new_mode != "160000" || diff.status == StatusChange::Added { continue; } @@ -159,7 +155,7 @@ pub(crate) mod config { type Check = SubmoduleRewind; fn into_check(self) -> Self::Check { - SubmoduleRewind::default() + Default::default() } } diff --git a/git-checks/src/submodule_watch.rs b/git-checks/src/submodule_watch.rs index 04aa6147061747a45273a19fae4b79b719d9b6ec..aebd2a5a4b5e00e2d2e9b9bfde66f4211208b7d1 100644 --- a/git-checks/src/submodule_watch.rs +++ b/git-checks/src/submodule_watch.rs @@ -30,7 +30,7 @@ pub struct SubmoduleWatch { impl SubmoduleWatch { /// Checks that submodules in the project are available. pub fn builder() -> SubmoduleWatchBuilder { - SubmoduleWatchBuilder::default() + Default::default() } } diff --git a/git-checks/src/third_party.rs b/git-checks/src/third_party.rs index 931a1981b6479599858cdf16853cf264dd077cd0..38ebec80852ce094be813c9ed67f2af855d96506 100644 --- a/git-checks/src/third_party.rs +++ b/git-checks/src/third_party.rs @@ -126,7 +126,7 @@ pub struct ThirdParty { impl ThirdParty { /// Create a new third party import configuration. pub fn builder() -> ThirdPartyBuilder { - ThirdPartyBuilder::default() + Default::default() } } diff --git a/git-checks/src/valid_name.rs b/git-checks/src/valid_name.rs index e70d42e2bcdbfa8d76014ff822b86eff9d581cf7..b52bd3eac91c4f26c1c38830a3f11a25fef4454b 100644 --- a/git-checks/src/valid_name.rs +++ b/git-checks/src/valid_name.rs @@ -56,8 +56,10 @@ impl ValidNameFullNamePolicy { const LOCK_POISONED: &str = "DNS cache lock poisoned"; const DEFAULT_TTL_CACHE_SIZE: usize = 100; // 24 hours +// XXX(rust-???): use `Duration::from_days(1); https://github.com/rust-lang/rust/issues/120301 const DEFAULT_TTL_CACHE_HIT_DURATION: Duration = Duration::from_secs(24 * 60 * 60); // 5 minutes +// XXX(rust-???): use `Duration::from_mins(5); https://github.com/rust-lang/rust/issues/120301 const DEFAULT_TTL_CACHE_MISS_DURATION: Duration = Duration::from_secs(5 * 60); lazy_static! { @@ -217,7 +219,7 @@ impl ValidNameError { impl ValidName { /// Create a new builder. pub fn builder() -> ValidNameBuilder { - ValidNameBuilder::default() + Default::default() } /// Check that a name is valid. @@ -599,6 +601,8 @@ pub(crate) mod config { #[cfg(test)] mod tests { + use std::time::{Duration, Instant}; + use git_checks_core::{BranchCheck, Check}; use git_workarea::Identity; @@ -656,9 +660,10 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_valid_name_whitelist() { let check = ValidName::builder() - .trust_domains(["baddomain.invalid"].iter().cloned()) + .whitelisted_domains(["baddomain.invalid"].iter().cloned()) .build() .unwrap(); let result = run_check("test_valid_name_whitelist", BAD_TOPIC, check); @@ -681,6 +686,32 @@ mod tests { ]); } + #[test] + fn test_valid_name_trust_domains() { + let check = ValidName::builder() + .trust_domains(["baddomain.invalid"].iter().cloned()) + .build() + .unwrap(); + let result = run_check("test_valid_name_trust_domains", BAD_TOPIC, check); + test_result_errors(result, &[ + "The author name (`Mononym`) for commit edac4e5b3a00eac60280a78ee84b5ef8d4cce97a has \ + no space in it. A full name is required for contribution. Please set the `user.name` \ + Git configuration value.", + "The author email (`bademail`) for commit 9de4928f5ec425eef414ee7620d0692fda56ebb0 \ + has an unknown domain. Please set the `user.email` Git configuration value.", + "The committer name (`Mononym`) for commit 1debf1735a6e28880ef08f13baeea4b71a08a846 \ + has no space in it. A full name is required for contribution. Please set the \ + `user.name` Git configuration value.", + "The committer email (`bademail`) for commit da71ae048e5a387d6809558d59ad073d0e4fb089 \ + has an unknown domain. Please set the `user.email` Git configuration value.", + "The given name (`Mononym`) for commit 91d9fceb226bfc0faeb8a4e54b4f0b5a1ffd39e8 has \ + no space in it. A full name is required for contribution. Please set the `user.name` \ + Git configuration value.", + "The given email (`bademail`) for commit 91d9fceb226bfc0faeb8a4e54b4f0b5a1ffd39e8 has \ + an unknown domain. Please set the `user.email` Git configuration value.", + ]); + } + #[test] fn test_valid_name_preferred() { let check = ValidName::builder() @@ -760,13 +791,13 @@ mod tests { } #[test] - fn test_valid_name_branch_whitelist() { + fn test_valid_name_branch_trust_domains() { let check = ValidName::builder() .trust_domains(["baddomain.invalid"].iter().cloned()) .build() .unwrap(); run_branch_check_ok( - "test_valid_name_branch_whitelist/ok", + "test_valid_name_branch_trust_domains/ok", BAD_TOPIC, check.clone(), ); @@ -781,7 +812,7 @@ mod tests { for contribution. Please set the `user.name` Git configuration value.", ]); let result = run_branch_check_ident( - "test_valid_name_branch_whitelist/bademail", + "test_valid_name_branch_trust_domains/bademail", BAD_TOPIC, check.clone(), bademail_ident(), @@ -794,7 +825,7 @@ mod tests { ], ); run_branch_check_ident_ok( - "test_valid_name_branch_whitelist/bademail_mx", + "test_valid_name_branch_trust_domains/bademail_mx", BAD_TOPIC, check, bademail_mx_ident(), @@ -918,4 +949,52 @@ mod tests { assert_eq!(cloned.dns_cache.lock().unwrap().iter().count(), 0); assert_eq!(cloned.trust_domains, check.trust_domains); } + + #[test] + fn test_host_lookup_cache_duration() { + use super::HostLookup; + + let items = [ + (HostLookup::Hit, super::DEFAULT_TTL_CACHE_HIT_DURATION), + ( + HostLookup::Miss { + valid_until: None, + }, + super::DEFAULT_TTL_CACHE_MISS_DURATION, + ), + ]; + + for (l, d) in items { + assert_eq!(l.cache_duration(), d); + } + + let short_timeout = Duration::from_secs(60); + let soon = Instant::now() + short_timeout; + let long_timeout = Duration::from_secs(10000); + let later = Instant::now() + long_timeout; + + let range_items = [ + ( + HostLookup::Miss { + valid_until: Some(soon), + }, + short_timeout, + ), + ( + HostLookup::Miss { + valid_until: Some(later), + }, + long_timeout, + ), + ]; + + for (l, d) in range_items { + let duration = l.cache_duration(); + // Make sure the duration is less than the given timeout. + assert!(duration <= d); + // But also "close" to the given timeout. If a platform takes longer than 1 second to + // get here, there are other performance issues to consider first. + assert!(d - duration <= Duration::from_secs(1)); + } + } }