-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
161 lines (144 loc) · 6.81 KB
/
Copy pathaction.yml
File metadata and controls
161 lines (144 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
name: "Python Mutation Report"
description: "Comment on the PR with mutation test results for changed lines. This will swiftly identify untested areas."
inputs:
lock-file-path:
description: "Path of lock file to generate requirements.txt"
required: true
src-directory:
description: "Path of source directory to be mutated"
required: true
test-directory:
description: "Path of test directory"
required: true
where-to-run-test:
description: "Where to run the test. If none is specified, run from the root directory."
required: false
default: "."
python-version:
description: "Python version to use. Default is 3.11."
required: false
default: "3.11"
branding:
icon: "check-circle"
color: "green"
runs:
using: "composite"
steps:
- name: Validate checkout action has been done in advance
run: |
if [ -z "$(ls -A)" ]; then
echo "Error: File or folder not found under workspace. Repository needs to be checked out in advance, have you done it?"
exit 1
fi
shell: bash
- name: Validate input parameters is not empty
run: |
[[ "${{ inputs.lock-file-path }}" ]] || { echo "Error: lock-file-path input is empty" ; exit 1; }
[[ "${{ inputs.src-directory }}" ]] || { echo "Error: src-directory input is empty" ; exit 1; }
[[ "${{ inputs.test-directory }}" ]] || { echo "Error: test-directory input is empty" ; exit 1; }
shell: bash
- name: Validate trigger event is pull_request
if: ${{ github.event_name != 'pull_request' }}
run: |
echo "This workflow is supposed to work on pull request events, but it worked on ${{ github.event_name }}"
exit 1
shell: bash
- name: Validate input parameters are directories or files
run: |
[ -f "${{ inputs.lock-file-path }}" ] || { echo "Error: lock-file-path is not a file" ; exit 1; }
[ -d "${{ inputs.src-directory }}" ] || { echo "Error: src-directory is not a directory" ; exit 1; }
[ -d "${{ inputs.test-directory }}" ] || { echo "Error: test-directory is not a directory" ; exit 1; }
[ -d "${{ inputs.where-to-run-test }}" ] || { echo "Error: where-to-run-test is not a directory" ; exit 1; }
shell: bash
- name: Output changed filenames to txt
env:
GITHUB_TOKEN: ${{ github.token }}
URL: ${{ github.event.pull_request.html_url }}
run: gh pr diff "${URL}" --name-only > "${{ github.action_path }}/temporary/PR_diff_filenames.txt"
# Strings with \n cannot be set in $GITHUB_OUTPUT or $GITHUB_ENV, so use a text file instead.
shell: bash
- name: Output diff contents to txt
env:
GH_TOKEN: ${{ github.token }}
URL: ${{ github.event.pull_request.html_url }}
run: gh pr diff "${URL}" > "${{ github.action_path }}/temporary/PR_diff_contents.txt"
shell: bash
- name: Make mutmut run command from PR changed files
# It needs to be run from the root of the repository as it retrieves the files under src and test with glob.
run: python3 "${{ github.action_path }}/scripts/make_command_of_run.py"
env:
SRC_DIRECTORY: ${{ inputs.src-directory }}
TEST_DIRECTORY: ${{ inputs.test-directory }}
COMPOSITE_ACTION_PATH: ${{ github.action_path }}
WORKSPACE_PATH: ${{ github.workspace }}
shell: bash
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "${{ inputs.python-version }}"
- name: Install actions repository dependencies
run: |
sed '/-e/d' "${{ github.action_path }}/requirements.lock" > "${{ github.action_path }}/requirements.txt"
pip3 install --no-cache-dir -r "${{ github.action_path }}/requirements.txt"
shell: bash
# pytest may not need to be installed if it is already installed in the lock file
- name: Generate requirements.txt
# Uses the path of the lock file from root, so must be run from root.
run: python3 "${{ github.action_path }}/scripts/generate_requirements.py"
env:
LOCK_FILE_PATH: ${{ inputs.lock-file-path }}
shell: bash
- name: Install caller repository dependencies
run: pip3 install --no-cache-dir -r requirements.txt
shell: bash
- name: Create __init__.py in test directory if src and test are not same directory
run: |
src_absolute_path=$(readlink -f "${{ inputs.src-directory }}")
test_absolute_path=$(readlink -f "${{ inputs.test-directory }}")
if [ "$src_absolute_path" != "$test_absolute_path" ]; then
echo "The src and test paths point to different locations. So, creating __init__.py in test-directory."
cd "${{ inputs.test-directory }}"
find . -name '__pycache__' -prune -o -type d -exec touch {}/__init__.py \;
else
echo "The src and test paths point to the same location. So, skipping the creation of __init__.py."
fi
shell: bash
# Create __init__.py because import errors occur when trying to run only some test files.
# However, if src and test are in the same directory, the generation of __int__.py causes import errors.
- name: Generate pre mutation mutmut config to plant mutants in the changed line only
env:
WHERE_TO_RUN_TEST: ${{ inputs.where-to-run-test }}
COMPOSITE_ACTION_PATH: ${{ github.action_path }}
WORKSPACE_PATH: ${{ github.workspace }}
run: python3 "${{ github.action_path }}/scripts/generate_pre_mutation_config.py"
shell: bash
- name: Execute mutation test
run: |
cd "${{ inputs.where-to-run-test }}"
if [ -f .mutmut-cache ]; then
echo "Removing .mutmut-cache" ; rm .mutmut-cache;
fi
bash "${{ github.action_path }}/temporary/mutmut-run.sh"
shell: bash
continue-on-error: true
# If all mutants cannot be killed, an error occurs with an exit code of 2.
# Even if an error occurs, continue-on-error is required to allow subsequent steps to be performed
- name: Output mutation test result to a text file.
run: |
cd "${{ inputs.where-to-run-test }}"
mutmut html ; mv html "${{ github.action_path }}/temporary";
mutmut junitxml --suspicious-policy=error > "${{ github.action_path }}/temporary/junit.xml"
shell: bash
# Move html directory to temporary directory?
- name: Reshape result for PR comment
run: python3 "${{ github.action_path }}/scripts/reshape_result_for_PR_comment.py"
env:
COMPOSITE_ACTION_PATH: ${{ github.action_path }}
WORKSPACE_PATH: ${{ github.workspace }}
shell: bash
- name: Post comments to PR
env:
GITHUB_TOKEN: ${{ github.token }}
URL: ${{ github.event.pull_request.html_url }}
run: gh pr comment "${URL}" --body-file "${{ github.action_path }}/temporary/PR_comment.txt"
shell: bash