@@ -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
@@ -15,6 +16,7 @@ use crate::core::build_steps::clippy::{LintConfig, get_clippy_rules_in_order};
1516use crate :: core:: build_steps:: llvm;
1617use crate :: core:: build_steps:: llvm:: LLVM_INVALIDATION_PATHS ;
1718use crate :: core:: config:: { LldMode , Target , TargetSelection , TomlConfig } ;
19+ use crate :: utils:: tests:: git:: git_test;
1820
1921pub ( crate ) fn parse ( config : & str ) -> Config {
2022 Config :: parse_inner (
@@ -537,3 +539,171 @@ fn test_ci_flag() {
537539 let config = Config :: parse_inner ( Flags :: parse ( & [ "check" . into ( ) ] ) , |& _| toml:: from_str ( "" ) ) ;
538540 assert_eq ! ( config. is_running_on_ci, CiEnv :: is_ci( ) ) ;
539541}
542+
543+ #[ test]
544+ fn test_pr_ci_unchanged_anywhere ( ) {
545+ git_test ( |ctx| {
546+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
547+ ctx. create_nonupstream_merge ( & [ "b" ] ) ;
548+ let src = ctx. check_modifications ( & [ "c" ] , CiEnv :: GitHubActions ) ;
549+ assert_eq ! ( src, PathFreshness :: LastModifiedUpstream { upstream: sha } ) ;
550+ } ) ;
551+ }
552+
553+ #[ test]
554+ fn test_pr_ci_changed_in_pr ( ) {
555+ git_test ( |ctx| {
556+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
557+ ctx. create_nonupstream_merge ( & [ "b" ] ) ;
558+ let src = ctx. check_modifications ( & [ "b" ] , CiEnv :: GitHubActions ) ;
559+ assert_eq ! ( src, PathFreshness :: HasLocalModifications { upstream: sha } ) ;
560+ } ) ;
561+ }
562+
563+ #[ test]
564+ fn test_auto_ci_unchanged_anywhere_select_parent ( ) {
565+ git_test ( |ctx| {
566+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
567+ ctx. create_upstream_merge ( & [ "b" ] ) ;
568+ let src = ctx. check_modifications ( & [ "c" ] , CiEnv :: GitHubActions ) ;
569+ assert_eq ! ( src, PathFreshness :: LastModifiedUpstream { upstream: sha } ) ;
570+ } ) ;
571+ }
572+
573+ #[ test]
574+ fn test_auto_ci_changed_in_pr ( ) {
575+ git_test ( |ctx| {
576+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
577+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
578+ let src = ctx. check_modifications ( & [ "c" , "d" ] , CiEnv :: GitHubActions ) ;
579+ assert_eq ! ( src, PathFreshness :: HasLocalModifications { upstream: sha } ) ;
580+ } ) ;
581+ }
582+
583+ #[ test]
584+ fn test_local_uncommitted_modifications ( ) {
585+ git_test ( |ctx| {
586+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
587+ ctx. create_branch ( "feature" ) ;
588+ ctx. modify ( "a" ) ;
589+
590+ assert_eq ! (
591+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
592+ PathFreshness :: HasLocalModifications { upstream: sha }
593+ ) ;
594+ } ) ;
595+ }
596+
597+ #[ test]
598+ fn test_local_committed_modifications ( ) {
599+ git_test ( |ctx| {
600+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
601+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
602+ ctx. create_branch ( "feature" ) ;
603+ ctx. modify ( "x" ) ;
604+ ctx. commit ( ) ;
605+ ctx. modify ( "a" ) ;
606+ ctx. commit ( ) ;
607+
608+ assert_eq ! (
609+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
610+ PathFreshness :: HasLocalModifications { upstream: sha }
611+ ) ;
612+ } ) ;
613+ }
614+
615+ #[ test]
616+ fn test_local_committed_modifications_subdirectory ( ) {
617+ git_test ( |ctx| {
618+ let sha = ctx. create_upstream_merge ( & [ "a/b/c" ] ) ;
619+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
620+ ctx. create_branch ( "feature" ) ;
621+ ctx. modify ( "a/b/d" ) ;
622+ ctx. commit ( ) ;
623+
624+ assert_eq ! (
625+ ctx. check_modifications( & [ "a/b" ] , CiEnv :: None ) ,
626+ PathFreshness :: HasLocalModifications { upstream: sha }
627+ ) ;
628+ } ) ;
629+ }
630+
631+ #[ test]
632+ fn test_local_changes_in_head_upstream ( ) {
633+ git_test ( |ctx| {
634+ // We want to resolve to the upstream commit that made modifications to a,
635+ // even if it is currently HEAD
636+ let sha = ctx. create_upstream_merge ( & [ "a" ] ) ;
637+ assert_eq ! (
638+ ctx. check_modifications( & [ "a" , "d" ] , CiEnv :: None ) ,
639+ PathFreshness :: LastModifiedUpstream { upstream: sha }
640+ ) ;
641+ } ) ;
642+ }
643+
644+ #[ test]
645+ fn test_local_changes_in_previous_upstream ( ) {
646+ git_test ( |ctx| {
647+ // We want to resolve to this commit, which modified a
648+ let sha = ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
649+ // Not to this commit, which is the latest upstream commit
650+ ctx. create_upstream_merge ( & [ "b" , "c" ] ) ;
651+ ctx. create_branch ( "feature" ) ;
652+ ctx. modify ( "d" ) ;
653+ ctx. commit ( ) ;
654+ assert_eq ! (
655+ ctx. check_modifications( & [ "a" ] , CiEnv :: None ) ,
656+ PathFreshness :: LastModifiedUpstream { upstream: sha }
657+ ) ;
658+ } ) ;
659+ }
660+
661+ #[ test]
662+ fn test_local_no_upstream_commit_with_changes ( ) {
663+ git_test ( |ctx| {
664+ ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
665+ ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
666+ // We want to fall back to this commit, because there are no commits
667+ // that modified `x`.
668+ let sha = ctx. create_upstream_merge ( & [ "a" , "e" ] ) ;
669+ ctx. create_branch ( "feature" ) ;
670+ ctx. modify ( "d" ) ;
671+ ctx. commit ( ) ;
672+ assert_eq ! (
673+ ctx. check_modifications( & [ "x" ] , CiEnv :: None ) ,
674+ PathFreshness :: LastModifiedUpstream { upstream: sha }
675+ ) ;
676+ } ) ;
677+ }
678+
679+ #[ test]
680+ fn test_local_no_upstream_commit ( ) {
681+ git_test ( |ctx| {
682+ let src = ctx. check_modifications ( & [ "c" , "d" ] , CiEnv :: None ) ;
683+ assert_eq ! ( src, PathFreshness :: MissingUpstream ) ;
684+ } ) ;
685+ }
686+
687+ #[ test]
688+ fn test_local_changes_negative_path ( ) {
689+ git_test ( |ctx| {
690+ let upstream = ctx. create_upstream_merge ( & [ "a" ] ) ;
691+ ctx. create_branch ( "feature" ) ;
692+ ctx. modify ( "b" ) ;
693+ ctx. modify ( "d" ) ;
694+ ctx. commit ( ) ;
695+
696+ assert_eq ! (
697+ ctx. check_modifications( & [ ":!b" , ":!d" ] , CiEnv :: None ) ,
698+ PathFreshness :: LastModifiedUpstream { upstream: upstream. clone( ) }
699+ ) ;
700+ assert_eq ! (
701+ ctx. check_modifications( & [ ":!c" ] , CiEnv :: None ) ,
702+ PathFreshness :: HasLocalModifications { upstream: upstream. clone( ) }
703+ ) ;
704+ assert_eq ! (
705+ ctx. check_modifications( & [ ":!d" , ":!x" ] , CiEnv :: None ) ,
706+ PathFreshness :: HasLocalModifications { upstream }
707+ ) ;
708+ } ) ;
709+ }
0 commit comments