Skip to content

Commit

Permalink
Revert "Improve files option, support multiline, excludes and default…
Browse files Browse the repository at this point in the history
… value (#121)" (#125)

This reverts commit 1453355.
  • Loading branch information
EnricoMi authored May 2, 2021
1 parent 991a325 commit 3d06770
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 196 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The action can be configured by the following options. They are all optional exc

|Option|Default Value|Description|
|:-----|:-----:|:----------|
|`files`|`*.xml`|File patterns to select the test result XML files, e.g. `test-results/**/*.xml`. Use multiline string for multiple patterns. Supports `*`, `**`, `?`, `[]`. Excludes files when starting with `!`. |
|`files`| |A file pattern to select the test result XML files, e.g. `test-results/**/*.xml`|
|`github_token`|`${{github.token}}`|An alternative GitHub token, other than the default provided by GitHub Actions runner.|
|`commit`|`${{env.GITHUB_SHA}}`|An alternative commit SHA to which test results are published. The `push` and `pull_request`events are handled, but for other [workflow events](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#push) `GITHUB_SHA` may refer to different kinds of commits. See [GitHub Workflow documentation](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows) for details.|
|`check_name`|`"Unit Test Results"`|An alternative name for the check result.|
Expand All @@ -101,18 +101,9 @@ The action can be configured by the following options. They are all optional exc
|`check_run_annotations`|`all tests, skipped tests`|Adds additional information to the check run (comma-separated list):<br>`all tests` - list all found tests,<br>`skipped tests` - list all skipped tests,<br>`none` - no extra annotations at all|
|`check_run_annotations_branch`|default branch|Adds check run annotations only on given branches. If not given, this defaults to the default branch of your repository, e.g. `main` or `master`. Comma separated list of branch names allowed, asterisk `"*"` matches all branches. Example: `main, master, branch_one`|

Files can be selected via the `files` variable, which is optional and defaults to `*.xml` in the current working directory.
[It supports wildcards](https://docs.python.org/3/library/glob.html#glob.glob) like `*`, `**`, `?` and `[]`.
The `**` wildcard matches all files and directories recursively: `./`, `./*/`, `./*/*/`, etc.
You can provide multiple file patterns, one pattern per line. Patterns starting with `!` exclude the matching files.
There have to be at least one pattern starting without a `!`:

```yaml
with:
files: |
*.xml
!config.xml
```
Files can be selected via the `files` variable, which is optional and defaults to the current working directory.
It supports wildcards like `*`, `**`, `?` and `[]`. The `**` wildcard matches
[directories recursively](https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob): `./`, `./*/`, `./*/*/`, etc.

Pull request comments highlight removal of tests or tests that the pull request moves into skip state.
Those removed or skipped tests are added as a list, which is limited in length by `test_changes_limit`,
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inputs:
required: false
default: 'test failures'
files:
description: 'File patterns of test result files. Supports *, **, ?, and []. Use multiline string for multiple patterns.'
description: 'File pattern of test result files'
required: true
report_individual_runs:
description: 'Individual runs of the same test may see different failures. Reports all individual failures when set "true" or the first only otherwise'
Expand Down
2 changes: 1 addition & 1 deletion composite/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inputs:
required: false
default: 'test failures'
files:
description: 'File patterns of test result files. Supports *, **, ?, and []. Use multiline string for multiple patterns.'
description: 'File pattern of test result files'
required: true
report_individual_runs:
description: 'Individual runs of the same test may see different failures. Reports all individual failures when set "true" or the first only otherwise'
Expand Down
21 changes: 4 additions & 17 deletions python/publish_unit_test_results.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
import logging
import os
import re
from glob import glob
import pathlib
from typing import List, Optional, Union

import github
Expand Down Expand Up @@ -37,19 +36,6 @@ def get_github(token: str, url: str, retries: int, backoff_factor: float) -> git
return github.Github(login_or_token=token, base_url=url, retry=retry)


def get_files(multiline_files_globs: str) -> List[str]:
multiline_files_globs = re.split('\r?\n\r?', multiline_files_globs)
included = {str(file)
for files_glob in multiline_files_globs
for file in glob(files_glob, recursive=True)
if not files_glob.startswith('!')}
excluded = {str(file)
for files_glob in multiline_files_globs
for file in glob(files_glob[1:], recursive=True)
if files_glob.startswith('!')}
return list(included - excluded)


def main(settings: Settings) -> None:
gha = GithubAction()

Expand All @@ -62,7 +48,7 @@ def main(settings: Settings) -> None:
return

# resolve the files_glob to files
files = get_files(settings.files_glob)
files = [str(file) for file in pathlib.Path().glob(settings.files_glob)]
if len(files) == 0:
gha.warning(f'Could not find any files for {settings.files_glob}')
else:
Expand Down Expand Up @@ -174,7 +160,7 @@ def get_settings(options: dict) -> Settings:
commit=get_var('COMMIT', options) or get_commit_sha(event, event_name, options),
fail_on_errors=fail_on_errors,
fail_on_failures=fail_on_failures,
files_glob=get_var('FILES', options) or '*.xml',
files_glob=get_var('FILES', options),
check_name=check_name,
comment_title=get_var('COMMENT_TITLE', options) or check_name,
comment_on_pr=get_var('COMMENT_ON_PR', options) != 'false',
Expand All @@ -191,6 +177,7 @@ def get_settings(options: dict) -> Settings:
check_var(settings.repo, 'GITHUB_REPOSITORY', 'GitHub repository')
check_var(settings.commit, 'COMMIT, GITHUB_SHA or event file', 'Commit SHA')
check_var(settings.pull_request_build, 'PULL_REQUEST_BUILD', 'Pull Request build', pull_request_build_modes)
check_var(settings.files_glob, 'FILES', 'Files pattern')
check_var(settings.hide_comment_mode, 'HIDE_COMMENTS', 'Hide comments mode', hide_comments_modes)
check_var(settings.check_run_annotation, 'CHECK_RUN_ANNOTATIONS', 'Check run annotations', available_annotations)

Expand Down
14 changes: 0 additions & 14 deletions python/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from contextlib import contextmanager


def n(number, delta=None):
if delta is None:
return dict(number=number)
Expand All @@ -26,13 +22,3 @@ def d(duration, delta=None):
if delta is None:
return dict(duration=duration)
return dict(duration=duration, delta=delta)


@contextmanager
def chdir(path: str):
cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(cwd)
155 changes: 5 additions & 150 deletions python/test/test_action_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
fail_on_mode_nothing
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
from test import chdir
get_settings, get_annotations_config, Settings

event = dict(pull_request=dict(head=dict(sha='event_sha')))

Expand Down Expand Up @@ -173,11 +172,6 @@ def test_get_settings_github_api_url_default(self):
def test_get_settings_github_graphql_url_default(self):
self.do_test_get_settings(GITHUB_GRAPHQL_URL=None, expected=self.get_settings(graphql_url='https://api.github.com/graphql'))

def test_get_settings_files(self):
self.do_test_get_settings(FILES='file', expected=self.get_settings(files_glob='file'))
self.do_test_get_settings(FILES='file\nfile2', expected=self.get_settings(files_glob='file\nfile2'))
self.do_test_get_settings(FILES=None, expected=self.get_settings(files_glob='*.xml'))

def test_get_settings_commit_default(self):
event = {'pull_request': {'head': {'sha': 'sha2'}}}
self.do_test_get_settings(INPUT_COMMIT='sha', GITHUB_EVENT_NAME='pull_request', event=event, GITHUB_SHA='default', expected=self.get_settings(commit='sha', event=event, event_name='pull_request'))
Expand Down Expand Up @@ -260,6 +254,10 @@ def test_get_settings_missing_options(self):
self.do_test_get_settings(COMMIT=None)
self.assertEqual('Commit SHA must be provided via action input or environment variable COMMIT, GITHUB_SHA or event file', str(re.exception))

with self.assertRaises(RuntimeError) as re:
self.do_test_get_settings(FILES=None)
self.assertEqual('Files pattern must be provided via action input or environment variable FILES', str(re.exception))

def test_get_settings_unknown_values(self):
with self.assertRaises(RuntimeError) as re:
self.do_test_get_settings(HIDE_COMMENTS='hide')
Expand Down Expand Up @@ -385,146 +383,3 @@ def test_get_annotations_config_in_all_branches(self):
def test_get_annotations_config_default(self):
config = get_annotations_config({}, None)
self.assertEqual(['all tests', 'skipped tests'], config)

def test_get_files_single(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('file1.txt')
self.assertEqual(['file1.txt'], sorted(files))

def test_get_files_multi(self):
for sep in ['\n', '\r\n', '\n\r', '\n\n', '\r\n\r\n']:
with self.subTest(sep=sep):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files(f'file1.txt{sep}file2.txt')
self.assertEqual(['file1.txt', 'file2.txt'], sorted(files))

def test_get_files_single_wildcard(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
for wildcard in ['*.txt', 'file?.txt']:
with self.subTest(wildcard=wildcard):
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files(wildcard)
self.assertEqual(['file1.txt', 'file2.txt'], sorted(files))

def test_get_files_multi_wildcard(self):
for sep in ['\n', '\r\n', '\n\r', '\n\n', '\r\n\r\n']:
with self.subTest(sep=sep):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files(f'*1.txt{sep}*3.bin')
self.assertEqual(['file1.txt', 'file3.bin'], sorted(files))

def test_get_files_subdir_and_wildcard(self):
filenames = [os.path.join('sub', 'file1.txt'),
os.path.join('sub', 'file2.txt'),
os.path.join('sub', 'file3.bin'),
os.path.join('sub2', 'file4.txt'),
'file5.txt']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
os.mkdir('sub')
os.mkdir('sub2')
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('sub/*.txt')
self.assertEqual([os.path.join('sub', 'file1.txt'),
os.path.join('sub', 'file2.txt')], sorted(files))

def test_get_files_recursive_wildcard(self):
filenames = [os.path.join('sub', 'file1.txt'),
os.path.join('sub', 'file2.txt'),
os.path.join('sub', 'file3.bin'),
os.path.join('sub2', 'file4.txt'),
'file5.txt']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
os.mkdir('sub')
os.mkdir('sub2')
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('**/*.txt')
self.assertEqual(sorted(['file5.txt',
os.path.join('sub', 'file1.txt'),
os.path.join('sub', 'file2.txt'),
os.path.join('sub2', 'file4.txt')]), sorted(files))

def test_get_files_character_range(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('file[0-2].*')
self.assertEqual(['file1.txt', 'file2.txt'], sorted(files))

def test_get_files_multi_match(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('*.txt\nfile*.txt\nfile2.*')
self.assertEqual(['file1.txt', 'file2.txt'], sorted(files))

def test_get_files_absolute_path_and_wildcard(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files(os.path.join(path, '*'))
self.assertEqual([os.path.join(path, file) for file in filenames], sorted(files))

def test_get_files_exclude_only(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('!file*.txt')
self.assertEqual([], sorted(files))

def test_get_files_include_and_exclude(self):
filenames = ['file1.txt', 'file2.txt', 'file3.bin']
with tempfile.TemporaryDirectory() as path:
with chdir(path):
for filename in filenames:
with open(filename, mode='w'):
pass

files = get_files('*.txt\n!file1.txt')
self.assertEqual(['file2.txt'], sorted(files))

0 comments on commit 3d06770

Please sign in to comment.