From df12c06174c5a53f4fc1065dbc56dafe57b6e4b0 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 30 Nov 2021 21:53:11 +0100 Subject: [PATCH 1/2] Treat empty input strings as None --- python/publish_unit_test_results.py | 3 +- python/test/test_action_script.py | 50 +++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/python/publish_unit_test_results.py b/python/publish_unit_test_results.py index 2ee3b61b..4bebc20d 100644 --- a/python/publish_unit_test_results.py +++ b/python/publish_unit_test_results.py @@ -162,7 +162,8 @@ def get_var(name: str, options: dict) -> Optional[str]: Returns the value from the given dict with key 'INPUT_$key', or if this does not exist, key 'key'. """ - return options.get(f'INPUT_{name}') or options.get(name) + # the last 'or None' turns empty strings into None + return options.get(f'INPUT_{name}') or options.get(name) or None def check_var(var: Union[str, List[str]], diff --git a/python/test/test_action_script.py b/python/test/test_action_script.py index 3a1b0fcb..2083f5c4 100644 --- a/python/test/test_action_script.py +++ b/python/test/test_action_script.py @@ -11,8 +11,8 @@ fail_on_mode_nothing, comment_mode_off, comment_mode_create, comment_mode_update from publish.github_action import GithubAction from publish.unittestresults import ParsedUnitTestResults, ParseError -from publish_unit_test_results import get_conclusion, get_commit_sha, \ - get_settings, get_annotations_config, Settings, get_files, throttle_gh_request_raw, is_float +from publish_unit_test_results import get_conclusion, get_commit_sha, get_var, \ + get_settings, get_annotations_config, Settings, get_files, throttle_gh_request_raw, is_float, main from test import chdir event = dict(pull_request=dict(head=dict(sha='event_sha'))) @@ -120,6 +120,13 @@ def test_event_sha(self): actual = get_commit_sha(event, event_name, options) self.assertEqual('event_sha', actual) + def test_get_var(self): + self.assertIsNone(get_var('NAME', dict())) + self.assertIsNone(get_var('NAME', dict(name='case sensitive'))) + self.assertEquals(get_var('NAME', dict(NAME='value')), 'value') + self.assertEquals(get_var('NAME', dict(INPUT_NAME='precedence', NAME='value')), 'precedence') + self.assertIsNone(get_var('NAME', dict(NAME=''))) + @staticmethod def get_settings(token='token', api_url='http://github.api.url/', @@ -178,6 +185,16 @@ def test_get_settings(self): if key not in {'GITHUB_API_URL', 'GITHUB_GRAPHQL_URL', 'GITHUB_SHA'}} self.do_test_get_settings(**options) + def test_get_settings_event_file(self): + self.do_test_get_settings(expected=self.get_settings(event_file=None)) + self.do_test_get_settings(EVENT_FILE='', expected=self.get_settings(event_file=None)) + self.do_test_get_settings(EVENT_FILE=None, expected=self.get_settings(event_file=None)) + + with tempfile.NamedTemporaryFile(mode='wb') as file: + file.write(b'{}') + file.flush() + self.do_test_get_settings(EVENT_FILE=file.name, expected=self.get_settings(event_file=file.name)) + def test_get_settings_github_api_url(self): self.do_test_get_settings(GITHUB_API_URL='https://api.github.onpremise.com', expected=self.get_settings(api_url='https://api.github.onpremise.com')) self.do_test_get_settings(GITHUB_API_URL=None, expected=self.get_settings(api_url='https://api.github.com')) @@ -690,3 +707,32 @@ def test_is_float(self): ]: with self.subTest(value=value): self.assertEqual(expected, is_float(value)) + + def test_main_fork_pr_check(self): + with tempfile.NamedTemporaryFile(mode='wb') as file: + file.write(b'{ "pull_request": { "head": { "repo": { "full_name": "fork/repo" } } } }') + file.flush() + + gha = mock.MagicMock() + settings = get_settings(dict( + COMMIT='commit', + GITHUB_TOKEN='********', + GITHUB_EVENT_PATH=file.name, + GITHUB_EVENT_NAME='pull_request', + GITHUB_REPOSITORY='repo', + EVENT_FILE=None + ), gha) + + def do_raise(*args): + # if this is raised, the tested main method did not return where expected but continued + raise RuntimeError('This is not expected to be called') + + with mock.patch('publish_unit_test_results.get_files') as m: + m.side_effect = do_raise + main(settings, gha) + + gha.warning.assert_called_once_with('This action is running on a pull_request event for a fork repository. ' + 'It cannot do anything useful like creating check runs or pull request ' + 'comments. To run the action on fork repository pull requests, see ' + 'https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20' + '/README.md#support-fork-repositories-and-dependabot-branches') From 3949fda39130effd63d48412d12c17b85c9b0848 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 30 Nov 2021 22:32:37 +0100 Subject: [PATCH 2/2] Close and delete the tmp file on Windows --- python/test/test_action_script.py | 36 +++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/python/test/test_action_script.py b/python/test/test_action_script.py index 2083f5c4..4cae46bc 100644 --- a/python/test/test_action_script.py +++ b/python/test/test_action_script.py @@ -1,6 +1,7 @@ import json import logging import os +import sys import tempfile import unittest from typing import Optional @@ -190,10 +191,17 @@ def test_get_settings_event_file(self): self.do_test_get_settings(EVENT_FILE='', expected=self.get_settings(event_file=None)) self.do_test_get_settings(EVENT_FILE=None, expected=self.get_settings(event_file=None)) - with tempfile.NamedTemporaryFile(mode='wb') as file: + with tempfile.NamedTemporaryFile(mode='wb', delete=sys.platform != 'win32') as file: file.write(b'{}') file.flush() - self.do_test_get_settings(EVENT_FILE=file.name, expected=self.get_settings(event_file=file.name)) + if sys.platform == 'win32': + file.close() + + try: + self.do_test_get_settings(EVENT_FILE=file.name, expected=self.get_settings(event_file=file.name)) + finally: + if sys.platform == 'win32': + os.unlink(file.name) def test_get_settings_github_api_url(self): self.do_test_get_settings(GITHUB_API_URL='https://api.github.onpremise.com', expected=self.get_settings(api_url='https://api.github.onpremise.com')) @@ -709,19 +717,25 @@ def test_is_float(self): self.assertEqual(expected, is_float(value)) def test_main_fork_pr_check(self): - with tempfile.NamedTemporaryFile(mode='wb') as file: + with tempfile.NamedTemporaryFile(mode='wb', delete=sys.platform != 'win32') as file: file.write(b'{ "pull_request": { "head": { "repo": { "full_name": "fork/repo" } } } }') file.flush() + if sys.platform == 'win32': + file.close() gha = mock.MagicMock() - settings = get_settings(dict( - COMMIT='commit', - GITHUB_TOKEN='********', - GITHUB_EVENT_PATH=file.name, - GITHUB_EVENT_NAME='pull_request', - GITHUB_REPOSITORY='repo', - EVENT_FILE=None - ), gha) + try: + settings = get_settings(dict( + COMMIT='commit', + GITHUB_TOKEN='********', + GITHUB_EVENT_PATH=file.name, + GITHUB_EVENT_NAME='pull_request', + GITHUB_REPOSITORY='repo', + EVENT_FILE=None + ), gha) + finally: + if sys.platform == 'win32': + os.unlink(file.name) def do_raise(*args): # if this is raised, the tested main method did not return where expected but continued