1515# specific language governing permissions and limitations
1616# under the License.
1717import io
18+ from pathlib import Path
1819from unittest import mock
1920from urllib .error import HTTPError , URLError
2021
2122import pytest
2223
2324from scripts import change_detector
25+ from superset .utils import json
2426
2527
2628def _make_response (body : bytes ) -> mock .MagicMock :
@@ -188,6 +190,155 @@ def test_grype_config_changes_trigger_docker_build() -> None:
188190 )
189191
190192
193+ def test_fetch_changed_files_push_uses_full_push_range () -> None :
194+ changed_files = [
195+ {"filename" : "superset/foo.py" },
196+ {"filename" : "superset-frontend/src/foo.tsx" },
197+ ]
198+
199+ with mock .patch .object (
200+ change_detector ,
201+ "fetch_files_github_api" ,
202+ return_value = {"files" : changed_files },
203+ ) as fetch_mock :
204+ result = change_detector .fetch_changed_files_push (
205+ "apache/superset" ,
206+ "after-sha" ,
207+ "before-sha" ,
208+ )
209+
210+ assert result == [
211+ "superset/foo.py" ,
212+ "superset-frontend/src/foo.tsx" ,
213+ ]
214+
215+ fetch_mock .assert_called_once_with (
216+ "https://api.github.com/repos/apache/superset/compare/before-sha...after-sha"
217+ )
218+
219+
220+ def test_get_push_event_returns_empty_without_event_path (
221+ monkeypatch : pytest .MonkeyPatch ,
222+ ) -> None :
223+ """Local runs have no event file, so there is no `before` to compare from."""
224+ monkeypatch .delenv ("GITHUB_EVENT_PATH" , raising = False )
225+
226+ assert change_detector .get_push_event () == {}
227+
228+
229+ def test_get_push_event_reads_event_file (
230+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
231+ ) -> None :
232+ """The webhook payload GitHub writes to GITHUB_EVENT_PATH is returned as-is."""
233+ event = {"before" : "before-sha" , "after" : "after-sha" , "forced" : False }
234+ event_file = tmp_path / "github-event.json"
235+ event_file .write_text (json .dumps (event ))
236+ monkeypatch .setenv ("GITHUB_EVENT_PATH" , str (event_file ))
237+
238+ assert change_detector .get_push_event () == event
239+
240+
241+ def test_main_push_detects_changes_from_multiple_commits (
242+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
243+ ) -> None :
244+ """The push range is `before...after`: the final commit only touches the
245+ frontend, yet python must be detected from the earlier commit. A revert to
246+ the parent-only compare fails the python assertion."""
247+ event_file = tmp_path / "github-event.json"
248+ event_file .write_text (json .dumps ({"before" : "before-sha" , "after" : "after-sha" }))
249+ output_file = tmp_path / "github-output"
250+ monkeypatch .setenv ("GITHUB_EVENT_PATH" , str (event_file ))
251+ monkeypatch .setenv ("GITHUB_OUTPUT" , str (output_file ))
252+
253+ responses = {
254+ "https://api.github.com/repos/apache/superset/compare/before-sha...after-sha" : {
255+ "files" : [
256+ {"filename" : "superset/foo.py" },
257+ {"filename" : "superset-frontend/src/foo.tsx" },
258+ ]
259+ },
260+ "https://api.github.com/repos/apache/superset/commits/after-sha" : {
261+ "parents" : [{"sha" : "parent-sha" }]
262+ },
263+ "https://api.github.com/repos/apache/superset/compare/parent-sha...after-sha" : {
264+ "files" : [{"filename" : "superset-frontend/src/foo.tsx" }]
265+ },
266+ }
267+ with mock .patch .object (
268+ change_detector ,
269+ "fetch_files_github_api" ,
270+ side_effect = lambda url : responses [url ],
271+ ):
272+ change_detector .main ("push" , "after-sha" , "apache/superset" )
273+
274+ output = output_file .read_text ()
275+ assert "python=true" in output
276+ assert "frontend=true" in output
277+
278+
279+ def test_main_push_without_event_file_compares_against_parent (
280+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
281+ ) -> None :
282+ """Local runs have no event file, so the parent-only compare is kept."""
283+ output_file = tmp_path / "github-output"
284+ monkeypatch .delenv ("GITHUB_EVENT_PATH" , raising = False )
285+ monkeypatch .setenv ("GITHUB_OUTPUT" , str (output_file ))
286+
287+ responses = {
288+ "https://api.github.com/repos/apache/superset/commits/after-sha" : {
289+ "parents" : [{"sha" : "parent-sha" }]
290+ },
291+ "https://api.github.com/repos/apache/superset/compare/parent-sha...after-sha" : {
292+ "files" : [{"filename" : "superset-frontend/src/foo.tsx" }]
293+ },
294+ }
295+ with mock .patch .object (
296+ change_detector ,
297+ "fetch_files_github_api" ,
298+ side_effect = lambda url : responses [url ],
299+ ):
300+ change_detector .main ("push" , "after-sha" , "apache/superset" )
301+
302+ output = output_file .read_text ()
303+ assert "frontend=true" in output
304+ assert "python=true" not in output
305+
306+
307+ @pytest .mark .parametrize (
308+ "event" ,
309+ [
310+ pytest .param (
311+ {"before" : "a" * 40 , "after" : "b" * 40 , "forced" : True },
312+ id = "forced-push" ,
313+ ),
314+ pytest .param (
315+ {"before" : change_detector .ZERO_SHA , "after" : "b" * 40 , "forced" : False },
316+ id = "new-branch-first-push" ,
317+ ),
318+ ],
319+ )
320+ def test_main_push_assumes_all_changed_for_forced_or_new_branch_push (
321+ event : dict [str , object ], tmp_path : Path , monkeypatch : pytest .MonkeyPatch
322+ ) -> None :
323+ """A force push rewrites history, so `before...after` can skip real changes,
324+ and a zero `before` has no base to compare against. Both must run every
325+ group and every language without calling the GitHub API."""
326+ event_file = tmp_path / "github-event.json"
327+ event_file .write_text (json .dumps (event ))
328+ output_file = tmp_path / "github-output"
329+ monkeypatch .setenv ("GITHUB_EVENT_PATH" , str (event_file ))
330+ monkeypatch .setenv ("GITHUB_OUTPUT" , str (output_file ))
331+
332+ with mock .patch .object (change_detector , "fetch_files_github_api" ) as fetch_mock :
333+ change_detector .main ("push" , "b" * 40 , "apache/superset" )
334+
335+ fetch_mock .assert_not_called ()
336+ output = output_file .read_text ().splitlines ()
337+ for group in change_detector .PATTERNS :
338+ assert f"{ group } =true" in output
339+ assert 'languages=["javascript", "python"]' in output
340+
341+
191342@pytest .mark .parametrize (
192343 "path" ,
193344 [
@@ -252,7 +403,10 @@ def test_main_writes_languages_to_github_output(tmp_path, monkeypatch) -> None:
252403 monkeypatch .setattr (
253404 change_detector ,
254405 "fetch_changed_files_push" ,
255- lambda repo , sha : ["superset/foo.py" , "superset-frontend/src/bar.tsx" ],
406+ lambda repo , sha , before_sha = None : [
407+ "superset/foo.py" ,
408+ "superset-frontend/src/bar.tsx" ,
409+ ],
256410 )
257411
258412 change_detector .main ("push" , "deadbeef" , "apache/superset" )
@@ -272,7 +426,7 @@ def test_main_languages_respects_the_99_file_cap(tmp_path, monkeypatch) -> None:
272426 monkeypatch .setattr (
273427 change_detector ,
274428 "fetch_changed_files_push" ,
275- lambda repo , sha : [f"docs/page{ i } .md" for i in range (99 )],
429+ lambda repo , sha , before_sha = None : [f"docs/page{ i } .md" for i in range (99 )],
276430 )
277431
278432 change_detector .main ("push" , "deadbeef" , "apache/superset" )
0 commit comments