@@ -54,7 +54,7 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st
5454
5555ENGINE_STUB = '''#!/usr/bin/env python3
5656"""CodeBoarding CLI stand-in: records each call and writes a minimal analysis."""
57- import json, os, sys
57+ import datetime, json, os, sys
5858
5959argv = sys.argv[1:]
6060output = argv[argv.index("--output-dir") + 1]
@@ -77,6 +77,8 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st
7777 sys.exit(0)
7878if argv[0] == "full":
7979 metadata = {"depth_cap": int(argv[argv.index("--depth-cap") + 1])}
80+ # Like Core, so no two analyses it writes are alike.
81+ metadata["generated_at"] = datetime.datetime.now(datetime.timezone.utc).isoformat()
8082with open(analysis, "w") as handle:
8183 json.dump({"metadata": metadata, "components": [], "components_relations": []}, handle)
8284print(json.dumps({"requiresFullAnalysis": False, "analysis_path": analysis}))
@@ -985,25 +987,59 @@ def test_a_bundle_without_both_static_analysis_files_is_not_used(self) -> None:
985987 self .assertEqual ([p .name for p in state .iterdir ()], ["analysis.json" ])
986988 self .assertNotIn ("Reusing" , self .log )
987989
988- def test_static_analysis_made_with_other_ignore_rules_is_not_used (self ) -> None :
989- # Core keeps cached files whatever the current rules say, so a mismatch would persist.
990+ def _publish (self , staged : Path ) -> None :
991+ """Writes the bundle as action.yml's base uploads publish a staged state:
992+ upload-artifact leaves dotfiles out unless the step sets include-hidden-files."""
990993 import zipfile
991994
992- shas = self ._history (2 )
993- self ._add_static_sha ()
994- with zipfile .ZipFile (self .bundle , "a" ) as archive :
995- archive .writestr (".codeboardingignore" , "docs/\n " )
996- self ._serve ([self ._artifact (f"codeboarding-base-{ self .OTHER_CFG } -{ shas [- 1 ]} " )])
995+ steps = (ROOT / "action.yml" ).read_text (encoding = "utf-8" ).split ("\n - name:" )
996+ uploads = [s for s in steps if "actions/upload-artifact@" in s and "/out/base" in s ]
997+ self .assertTrue (uploads )
998+ hidden = all ("include-hidden-files: true" in s for s in uploads )
999+ with zipfile .ZipFile (self .bundle , "w" ) as archive :
1000+ for path in sorted (staged .rglob ("*" )):
1001+ relative = path .relative_to (staged )
1002+ if path .is_file () and (hidden or not any (part .startswith ("." ) for part in relative .parts )):
1003+ archive .write (path , relative .as_posix ())
9971004
998- for ignore in (None , "tests/\n " ):
999- with self .subTest (ignore = ignore ):
1000- state = self ._reuse_static (shas [- 1 ], ignore )
1001- self .assertFalse ((state / "static_analysis.pkl" ).exists ())
1002- self .assertIn ("made with another .codeboardingignore" , self .log )
1005+ def test_static_analysis_is_reused_only_under_the_ignore_rules_it_was_made_with (self ) -> None :
1006+ # Core keeps cached files whatever the current rules say, so a mismatch would persist.
1007+ git = ["git" , "-C" , str (self .origin ), "-c" , "user.name=T" , "-c" , "user.email=t@example.com" ]
1008+ ignore_file = self .origin / ".codeboarding" / ".codeboardingignore"
1009+
1010+ def commit (ignore : str | None ) -> str :
1011+ ignore_file .parent .mkdir (exist_ok = True )
1012+ if ignore is None :
1013+ ignore_file .unlink ()
1014+ else :
1015+ ignore_file .write_text (ignore , encoding = "utf-8" )
1016+ subprocess .run ([* git , "add" , "-A" ], check = True )
1017+ subprocess .run ([* git , "-c" , "commit.gpgsign=false" , "commit" , "-q" , "--allow-empty" , "-m" , "c" ], check = True )
1018+ return subprocess .check_output ([* git , "rev-parse" , "HEAD" ], text = True ).strip ()
1019+
1020+ self ._history (1 )
1021+ saved , same_rules , rules_removed = commit ("docs/\n " ), commit ("docs/\n " ), commit (None )
1022+ # The base saved for `saved`, staged as keep_user_config and the analysis left it.
1023+ staged = self .root / "staged"
1024+ staged .mkdir ()
1025+ for name , content in (
1026+ ("analysis.json" , json .dumps ({"metadata" : {"depth_cap" : 2 }, "components" : []})),
1027+ ("static_analysis.pkl" , "pickle" ),
1028+ ("static_analysis.sha" , "a" * 40 ),
1029+ ("metadata.json" , json .dumps ({"kind" : "base" })),
1030+ (".codeboardingignore" , "docs/\n " ),
1031+ ):
1032+ (staged / name ).write_text (content , encoding = "utf-8" )
1033+ self ._publish (staged )
1034+ self ._serve ([self ._artifact (f"codeboarding-base-{ self .OTHER_CFG } -{ saved } " )])
10031035
1004- state = self ._reuse_static (shas [ - 1 ] , "docs/\n " )
1036+ state = self ._reuse_static (same_rules , "docs/\n " )
10051037 self .assertTrue ((state / "static_analysis.pkl" ).exists (), "the same rules must not block reuse" )
10061038
1039+ state = self ._reuse_static (rules_removed )
1040+ self .assertFalse ((state / "static_analysis.pkl" ).exists ())
1041+ self .assertIn (f"The saved static analysis of { saved [:7 ]} was made with another .codeboardingignore" , self .log )
1042+
10071043 def test_a_nearer_base_under_another_configuration_is_no_analysis_to_continue_from (self ) -> None :
10081044 shas = self ._history (4 )
10091045 self ._serve (
@@ -1184,10 +1220,32 @@ def test_state_is_published_from_the_directory_the_analysis_stages(self) -> None
11841220 def test_a_fresh_run_fetches_no_saved_analysis (self ) -> None :
11851221 # A fetched base's id would otherwise reach the review artifact whenever this
11861222 # run's own base is not published, pairing two independently built graphs.
1187- fetches = [s for s in self ._steps () if s .get ("run" , "" ).endswith ('fetch-state.sh"' )]
1223+ # The steps still run, so what an earlier use of the action in this job
1224+ # fetched is cleared rather than continued from.
1225+ steps = (ROOT / "action.yml" ).read_text (encoding = "utf-8" ).split ("\n - name:" )
1226+ fetches = [s for s in steps if 'fetch-state.sh"' in s ]
11881227 self .assertEqual (len (fetches ), 2 )
11891228 for step in fetches :
1190- self .assertIn ("inputs.fresh_analysis != 'true'" , step ["if" ], step ["name" ])
1229+ condition = next (line for line in step .splitlines () if line .strip ().startswith ("if:" ))
1230+ self .assertNotIn ("fresh_analysis" , condition )
1231+ self .assertIn ("ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs." , step )
1232+
1233+ def test_fetching_no_name_still_clears_the_destination (self ) -> None :
1234+ with tempfile .TemporaryDirectory () as tmp :
1235+ dest = Path (tmp ) / "base"
1236+ dest .mkdir ()
1237+ (dest / "analysis.json" ).write_text ("{}" , encoding = "utf-8" )
1238+
1239+ result = subprocess .run (
1240+ [str (SCRIPTS / "fetch-state.sh" )],
1241+ env = {"PATH" : os .environ ["PATH" ], "ARTIFACT_NAME" : "" , "DEST" : str (dest )},
1242+ capture_output = True ,
1243+ text = True ,
1244+ check = False ,
1245+ )
1246+
1247+ self .assertEqual (result .returncode , 0 , result .stderr )
1248+ self .assertFalse (dest .exists (), "an earlier invocation's state survived" )
11911249
11921250 def test_a_fork_publishes_nothing_another_run_would_read (self ) -> None :
11931251 # A base graph is named for a commit, so every pull request forking there
0 commit comments