@@ -5,6 +5,7 @@ use std::io::Write;
55use std:: path:: Path ;
66
77use build_helper:: ci:: CiEnv ;
8+ use build_helper:: git:: PathFreshness ;
89use clap:: CommandFactory ;
910use serde:: Deserialize ;
1011
@@ -14,6 +15,7 @@ use crate::core::build_steps::clippy::{LintConfig, get_clippy_rules_in_order};
1415use crate :: core:: build_steps:: llvm;
1516use crate :: core:: build_steps:: llvm:: LLVM_INVALIDATION_PATHS ;
1617use crate :: core:: config:: { LldMode , Target , TargetSelection , TomlConfig } ;
18+ use crate :: utils:: tests:: git:: git_test;
1719
1820pub ( crate ) fn parse ( config : & str ) -> Config {
1921 Config :: parse_inner (
@@ -556,3 +558,171 @@ fn test_ci_flag() {
556558 let config = Config :: parse_inner ( Flags :: parse ( & [ "check" . into ( ) ] ) , |& _| toml:: from_str ( "" ) ) ;
557559 assert_eq ! ( config. is_running_on_ci, CiEnv :: is_ci( ) ) ;
558560}
561+
562+ #[ test]
563+ fn test_pr_ci_unchanged_anywhere ( ) {
564+ git_test ( |ctx| {
565+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
566+ ctx. create_nonupstream_merge ( & [ "b" ] ) ;
567+ let src = ctx. check_modifications ( & [ "c" ] , CiEnv :: GitHubActions ) ;
568+ assert_eq ! ( src, PathFreshness :: LastModifiedUpstream { upstream: sha } ) ;
569+ } ) ;
570+ }
571+
572+ #[ test]
573+ fn test_pr_ci_changed_in_pr ( ) {
574+ git_test ( |ctx| {
575+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
576+ ctx. create_nonupstream_merge ( & [ "b" ] ) ;
577+ let src = ctx. check_modifications ( & [ "b" ] , CiEnv :: GitHubActions ) ;
578+ assert_eq ! ( src, PathFreshness :: HasLocalModifications { upstream: sha } ) ;
579+ } ) ;
580+ }
581+
582+ #[ test]
583+ fn test_auto_ci_unchanged_anywhere_select_parent ( ) {
584+ git_test ( |ctx| {
585+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
586+ ctx. create_upstream_merge ( & [ "b" ] ) ;
587+ let src = ctx. check_modifications ( & [ "c" ] , CiEnv :: GitHubActions ) ;
588+ assert_eq ! ( src, PathFreshness :: LastModifiedUpstream { upstream: sha } ) ;
589+ } ) ;
590+ }
591+
592+ #[ test]
593+ fn test_auto_ci_changed_in_pr ( ) {
594+ git_test ( |ctx| {
595+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
596+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
597+ let src = ctx. check_modifications ( & [ "c" , "d" ] , CiEnv :: GitHubActions ) ;
598+ assert_eq ! ( src, PathFreshness :: HasLocalModifications { upstream: sha } ) ;
599+ } ) ;
600+ }
601+
602+ #[ test]
603+ fn test_local_uncommitted_modifications ( ) {
604+ git_test ( |ctx| {
605+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
606+ ctx. create_branch ( "feature" ) ;
607+ ctx. modify ( "a" ) ;
608+
609+ assert_eq ! (
610+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
611+ PathFreshness :: HasLocalModifications { upstream: sha }
612+ ) ;
613+ } ) ;
614+ }
615+
616+ #[ test]
617+ fn test_local_committed_modifications ( ) {
618+ git_test ( |ctx| {
619+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
620+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
621+ ctx. create_branch ( "feature" ) ;
622+ ctx. modify ( "x" ) ;
623+ ctx. commit ( ) ;
624+ ctx. modify ( "a" ) ;
625+ ctx. commit ( ) ;
626+
627+ assert_eq ! (
628+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
629+ PathFreshness :: HasLocalModifications { upstream: sha }
630+ ) ;
631+ } ) ;
632+ }
633+
634+ #[ test]
635+ fn test_local_committed_modifications_subdirectory ( ) {
636+ git_test ( |ctx| {
637+ let sha = ctx. create_upstream_merge ( & [ "a/b/c" ] ) ;
638+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
639+ ctx. create_branch ( "feature" ) ;
640+ ctx. modify ( "a/b/d" ) ;
641+ ctx. commit ( ) ;
642+
643+ assert_eq ! (
644+ ctx. check_modifications( & [ "a/b" ] , CiEnv :: None ) ,
645+ PathFreshness :: HasLocalModifications { upstream: sha }
646+ ) ;
647+ } ) ;
648+ }
649+
650+ #[ test]
651+ fn test_local_changes_in_head_upstream ( ) {
652+ git_test ( |ctx| {
653+ // We want to resolve to the upstream commit that made modifications to a,
654+ // even if it is currently HEAD
655+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
656+ assert_eq ! (
657+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
658+ PathFreshness :: LastModifiedUpstream { upstream: sha }
659+ ) ;
660+ } ) ;
661+ }
662+
663+ #[ test]
664+ fn test_local_changes_in_previous_upstream ( ) {
665+ git_test ( |ctx| {
666+ // We want to resolve to this commit, which modified a
667+ let sha = ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
668+ // Not to this commit, which is the latest upstream commit
669+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
670+ ctx. create_branch ( "feature" ) ;
671+ ctx. modify ( "d" ) ;
672+ ctx. commit ( ) ;
673+ assert_eq ! (
674+ ctx. check_modifications( & [ "a" ] , CiEnv :: None ) ,
675+ PathFreshness :: LastModifiedUpstream { upstream: sha }
676+ ) ;
677+ } ) ;
678+ }
679+
680+ #[ test]
681+ fn test_local_no_upstream_commit_with_changes ( ) {
682+ git_test ( |ctx| {
683+ ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
684+ ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
685+ // We want to fall back to this commit, because there are no commits
686+ // that modified `x`.
687+ let sha = ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
688+ ctx. create_branch ( "feature" ) ;
689+ ctx. modify ( "d" ) ;
690+ ctx. commit ( ) ;
691+ assert_eq ! (
692+ ctx. check_modifications( & [ "x" ] , CiEnv :: None ) ,
693+ PathFreshness :: LastModifiedUpstream { upstream: sha }
694+ ) ;
695+ } ) ;
696+ }
697+
698+ #[ test]
699+ fn test_local_no_upstream_commit ( ) {
700+ git_test ( |ctx| {
701+ let src = ctx. check_modifications ( & [ "c" , "d" ] , CiEnv :: None ) ;
702+ assert_eq ! ( src, PathFreshness :: MissingUpstream ) ;
703+ } ) ;
704+ }
705+
706+ #[ test]
707+ fn test_local_changes_negative_path ( ) {
708+ git_test ( |ctx| {
709+ let upstream = ctx. create_upstream_merge ( & [ "a" ] ) ;
710+ ctx. create_branch ( "feature" ) ;
711+ ctx. modify ( "b" ) ;
712+ ctx. modify ( "d" ) ;
713+ ctx. commit ( ) ;
714+
715+ assert_eq ! (
716+ ctx. check_modifications( & [ ":!b" , ":!d" ] , CiEnv :: None ) ,
717+ PathFreshness :: LastModifiedUpstream { upstream: upstream. clone( ) }
718+ ) ;
719+ assert_eq ! (
720+ ctx. check_modifications( & [ ":!c" ] , CiEnv :: None ) ,
721+ PathFreshness :: HasLocalModifications { upstream: upstream. clone( ) }
722+ ) ;
723+ assert_eq ! (
724+ ctx. check_modifications( & [ ":!d" , ":!x" ] , CiEnv :: None ) ,
725+ PathFreshness :: HasLocalModifications { upstream }
726+ ) ;
727+ } ) ;
728+ }
0 commit comments