diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 3335fbcde..e5e1e7d52 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -40,6 +40,7 @@ * [tryserver:tests/gerrit_change_fetch_ref_timeout](#recipes-tryserver_tests_gerrit_change_fetch_ref_timeout) (Python3 ✅) * [tryserver:tests/gerrit_change_owner](#recipes-tryserver_tests_gerrit_change_owner) (Python3 ✅) * [tryserver:tests/gerrit_change_target_ref](#recipes-tryserver_tests_gerrit_change_target_ref) (Python3 ✅) + * [tryserver:tests/get_files_affected_by_patch](#recipes-tryserver_tests_get_files_affected_by_patch) (Python3 ✅) * [tryserver:tests/require_is_tryserver](#recipes-tryserver_tests_require_is_tryserver) (Python3 ✅) * [windows_sdk:examples/full](#recipes-windows_sdk_examples_full) (Python3 ✅) ## Recipe Modules @@ -852,7 +853,7 @@ Args: * report_files_via_property: name of the output property to report the list of the files. If None (default), do not report. -Returned paths will be relative to to patch_root. +Returned paths will be relative to to api.path['root']. — **def [get\_footer](/recipes/recipe_modules/tryserver/api.py#365)(self, tag, patch_text=None):** @@ -1139,6 +1140,13 @@ PYTHON_VERSION_COMPATIBILITY: PY2+3 PYTHON_VERSION_COMPATIBILITY: PY2+3 — **def [RunSteps](/recipes/recipe_modules/tryserver/tests/gerrit_change_target_ref.py#18)(api):** +### *recipes* / [tryserver:tests/get\_files\_affected\_by\_patch](/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py) + +[DEPS](/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py#9): [tryserver](#recipe_modules-tryserver), [recipe\_engine/assertions][recipe_engine/recipe_modules/assertions], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties] + +PYTHON_VERSION_COMPATIBILITY: PY2+3 + +— **def [RunSteps](/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py#18)(api):** ### *recipes* / [tryserver:tests/require\_is\_tryserver](/recipes/recipe_modules/tryserver/tests/require_is_tryserver.py) [DEPS](/recipes/recipe_modules/tryserver/tests/require_is_tryserver.py#12): [tryserver](#recipe_modules-tryserver), [recipe\_engine/buildbucket][recipe_engine/recipe_modules/buildbucket], [recipe\_engine/properties][recipe_engine/recipe_modules/properties] diff --git a/recipes/recipe_modules/tryserver/api.py b/recipes/recipe_modules/tryserver/api.py index 099c3ea6d..f1cfc4ede 100644 --- a/recipes/recipe_modules/tryserver/api.py +++ b/recipes/recipe_modules/tryserver/api.py @@ -233,7 +233,7 @@ class TryserverApi(recipe_api.RecipeApi): * report_files_via_property: name of the output property to report the list of the files. If None (default), do not report. - Returned paths will be relative to to patch_root. + Returned paths will be relative to to api.path['root']. """ cwd = self.m.context.cwd or self.m.path['start_dir'].join(patch_root) with self.m.context(cwd=cwd): diff --git a/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch.json b/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch.json index 9851c05a5..adfe04a7e 100644 --- a/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch.json +++ b/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch.json @@ -232,35 +232,6 @@ "@@@STEP_LOG_END@json.output (exception)@@@" ] }, - { - "cmd": [ - "git", - "-c", - "core.quotePath=false", - "diff", - "--cached", - "--name-only" - ], - "infra_step": true, - "luci_context": { - "realm": { - "name": "chromium:linux" - }, - "resultdb": { - "current_invocation": { - "name": "invocations/build:8945511751514863184", - "update_token": "token" - }, - "hostname": "rdbhost" - } - }, - "name": "git diff to analyze patch", - "~followup_annotations": [ - "@@@STEP_LOG_LINE@files@None/foo.cc@@@", - "@@@STEP_LOG_END@files@@@", - "@@@SET_BUILD_PROPERTY@affected_files@{\"first_100\": [\"None/foo.cc\"], \"total_count\": 1}@@@" - ] - }, { "cmd": [], "name": "TRYJOB SET SUBPROJECT_TAG", diff --git a/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch_and_target_ref.json b/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch_and_target_ref.json index 365321eca..c0c87fbbf 100644 --- a/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch_and_target_ref.json +++ b/recipes/recipe_modules/tryserver/examples/full.expected/with_gerrit_patch_and_target_ref.json @@ -232,35 +232,6 @@ "@@@STEP_LOG_END@json.output (exception)@@@" ] }, - { - "cmd": [ - "git", - "-c", - "core.quotePath=false", - "diff", - "--cached", - "--name-only" - ], - "infra_step": true, - "luci_context": { - "realm": { - "name": "chromium:linux" - }, - "resultdb": { - "current_invocation": { - "name": "invocations/build:8945511751514863184", - "update_token": "token" - }, - "hostname": "rdbhost" - } - }, - "name": "git diff to analyze patch", - "~followup_annotations": [ - "@@@STEP_LOG_LINE@files@None/foo.cc@@@", - "@@@STEP_LOG_END@files@@@", - "@@@SET_BUILD_PROPERTY@affected_files@{\"first_100\": [\"None/foo.cc\"], \"total_count\": 1}@@@" - ] - }, { "cmd": [], "name": "TRYJOB SET SUBPROJECT_TAG", diff --git a/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch.json b/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch.json index 6eb984f91..3a9ed46a2 100644 --- a/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch.json +++ b/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch.json @@ -1,21 +1,4 @@ [ - { - "cmd": [ - "git", - "-c", - "core.quotePath=false", - "diff", - "--cached", - "--name-only" - ], - "infra_step": true, - "name": "git diff to analyze patch", - "~followup_annotations": [ - "@@@STEP_LOG_LINE@files@foo.cc@@@", - "@@@STEP_LOG_END@files@@@", - "@@@SET_BUILD_PROPERTY@affected_files@{\"first_100\": [\"foo.cc\"], \"total_count\": 1}@@@" - ] - }, { "cmd": [ "vpython", diff --git a/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch_new.json b/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch_new.json index 0583160b2..3a9ed46a2 100644 --- a/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch_new.json +++ b/recipes/recipe_modules/tryserver/examples/full.expected/with_wrong_patch_new.json @@ -1,22 +1,4 @@ [ - { - "cmd": [ - "git", - "-c", - "core.quotePath=false", - "diff", - "--cached", - "--name-only" - ], - "cwd": "[START_DIR]\\sub\\project", - "infra_step": true, - "name": "git diff to analyze patch", - "~followup_annotations": [ - "@@@STEP_LOG_LINE@files@sub/project/foo.cc@@@", - "@@@STEP_LOG_END@files@@@", - "@@@SET_BUILD_PROPERTY@affected_files@{\"first_100\": [\"sub/project/foo.cc\"], \"total_count\": 1}@@@" - ] - }, { "cmd": [ "vpython", diff --git a/recipes/recipe_modules/tryserver/examples/full.py b/recipes/recipe_modules/tryserver/examples/full.py index 49b6eaad9..e780eb243 100644 --- a/recipes/recipe_modules/tryserver/examples/full.py +++ b/recipes/recipe_modules/tryserver/examples/full.py @@ -45,10 +45,6 @@ def RunSteps(api): if api.tryserver.is_gerrit_issue: api.tryserver.get_footers() api.tryserver.get_footer('testfooter') - api.tryserver.get_files_affected_by_patch( - api.properties.get('test_patch_root'), - report_files_via_property='affected_files', - ) if api.tryserver.is_tryserver: api.tryserver.set_subproject_tag('v8') diff --git a/recipes/recipe_modules/tryserver/test_api.py b/recipes/recipe_modules/tryserver/test_api.py index 4d166d79c..54e87b06c 100644 --- a/recipes/recipe_modules/tryserver/test_api.py +++ b/recipes/recipe_modules/tryserver/test_api.py @@ -12,3 +12,22 @@ class TryserverTestApi(recipe_test_api.RecipeTestApi): def gerrit_change_target_ref(target_ref): assert target_ref.startswith('refs/') return target_ref + + def get_files_affected_by_patch(self, + files, + step_name='git diff to analyze patch'): + """Override test data for the `get_files_affected_by_patch` method. + + Args: + files: The files that git should report as changed as paths + relative to the root of the repo. + + Example: + yield api.test( + 'my_test', + api.tryserver.get_files_affected_by_patch('foo.cc', + 'bar/baz.cc'), + ) + """ + output = self.m.raw_io.stream_output('\n'.join(files)) + return self.override_step_data(step_name, output) diff --git a/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.expected/report-files-via-property.json b/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.expected/report-files-via-property.json new file mode 100644 index 000000000..4eb692b00 --- /dev/null +++ b/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.expected/report-files-via-property.json @@ -0,0 +1,24 @@ +[ + { + "cmd": [ + "git", + "-c", + "core.quotePath=false", + "diff", + "--cached", + "--name-only" + ], + "cwd": "[START_DIR]/test/patch/root", + "infra_step": true, + "name": "git diff to analyze patch", + "~followup_annotations": [ + "@@@STEP_LOG_LINE@files@test/patch/root/baz/shaz.cc@@@", + "@@@STEP_LOG_LINE@files@test/patch/root/foo/bar.cc@@@", + "@@@STEP_LOG_END@files@@@", + "@@@SET_BUILD_PROPERTY@affected-files@{\"first_100\": [\"test/patch/root/baz/shaz.cc\", \"test/patch/root/foo/bar.cc\"], \"total_count\": 2}@@@" + ] + }, + { + "name": "$result" + } +] \ No newline at end of file diff --git a/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py b/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py new file mode 100644 index 000000000..02ffaec8e --- /dev/null +++ b/recipes/recipe_modules/tryserver/tests/get_files_affected_by_patch.py @@ -0,0 +1,92 @@ +# Copyright 2022 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +from recipe_engine import post_process + +PYTHON_VERSION_COMPATIBILITY = 'PY2+3' + +DEPS = [ + 'tryserver', + 'recipe_engine/assertions', + 'recipe_engine/path', + 'recipe_engine/platform', + 'recipe_engine/properties', +] + + +def RunSteps(api): + files = api.tryserver.get_files_affected_by_patch( + api.properties['patch_root'], + report_files_via_property=api.properties.get('report_files_via_property')) + api.assertions.assertCountEqual(files, api.properties['expected_files']) + + +def GenTests(api): + def no_properties_set(check, steps, step_name): + check(not steps[step_name].output_properties) + + yield api.test( + 'basic', + api.properties( + patch_root='', + expected_files=['foo.cc'], + ), + api.post_check(no_properties_set, 'git diff to analyze patch'), + api.post_check(post_process.StatusSuccess), + api.post_process(post_process.DropExpectation), + ) + + yield api.test( + 'patch_root', + api.properties( + patch_root='test/patch/root', + expected_files=['test/patch/root/foo.cc'], + ), + api.post_check(post_process.StatusSuccess), + api.post_process(post_process.DropExpectation), + ) + + yield api.test( + 'test-data', + api.tryserver.get_files_affected_by_patch(['foo/bar.cc', 'baz/shaz.cc']), + api.properties( + patch_root='test/patch/root', + expected_files=[ + 'test/patch/root/foo/bar.cc', + 'test/patch/root/baz/shaz.cc', + ], + ), + api.post_check(post_process.StatusSuccess), + api.post_process(post_process.DropExpectation), + ) + + yield api.test( + 'report-files-via-property', + api.tryserver.get_files_affected_by_patch(['foo/bar.cc', 'baz/shaz.cc']), + api.properties( + patch_root='test/patch/root', + report_files_via_property='affected-files', + expected_files=[ + 'test/patch/root/foo/bar.cc', + 'test/patch/root/baz/shaz.cc', + ], + ), + api.post_check(post_process.StatusSuccess), + ) + + yield api.test( + 'windows', + api.tryserver.get_files_affected_by_patch(['foo/bar.cc', 'baz/shaz.cc']), + api.platform('win', 32), + api.properties( + patch_root='test\\patch\\root', + report_files_via_property='affected-files', + expected_files=[ + 'test/patch/root/foo/bar.cc', + 'test/patch/root/baz/shaz.cc', + ], + ), + api.post_check(post_process.StatusSuccess), + api.post_process(post_process.DropExpectation), + )