From 1976f653db5db5894c392525a3d6f1f4e071179e Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Wed, 1 Dec 2021 11:25:38 +0100 Subject: [PATCH] Treat empty input strings as None (#198) --- python/publish_unit_test_results.py | 3 +- python/test/test_action_script.py | 64 ++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/python/publish_unit_test_results.py b/python/publish_unit_test_results.py index 532b7dfd..ccae1905 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 get_bool(name: str, options: dict, default: bool = False) -> Optional[str]: """ diff --git a/python/test/test_action_script.py b/python/test/test_action_script.py index 3a1b0fcb..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 @@ -11,8 +12,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 +121,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 +186,23 @@ 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', delete=sys.platform != 'win32') as file: + file.write(b'{}') + file.flush() + 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')) self.do_test_get_settings(GITHUB_API_URL=None, expected=self.get_settings(api_url='https://api.github.com')) @@ -690,3 +715,38 @@ 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', 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() + 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 + 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')