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