-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
170 lines (150 loc) · 6.27 KB
/
action.yml
File metadata and controls
170 lines (150 loc) · 6.27 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
162
163
164
165
166
167
168
169
170
name: Python Unit Testing
description: A GitHub Action to run Python tests using pytest and optionally commit the badges to the repository.
author: Jason Miller
branding:
icon: check-circle
color: green
inputs:
python-version:
description: Python version to use for testing. If not specified, uses the default Python available on the runner.
required: false
default: '3.x'
requirements-file:
description: Path to a requirements file to install before testing. If not specified, only detected test frameworks will be installed.
required: false
default: 'requirements.txt'
pytest-options:
description: Options to pass to pytest if detected.
required: false
default: ''
commit-badges:
description: Generate SVG badges for test results and commit them to the repository.
required: false
default: 'false'
badges-directory:
description: Directory where badge SVG files will be saved (relative to repository root).
required: false
default: '.github/badges'
runs:
using: composite
steps:
- name: Setup Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
continue-on-error: false
- name: Install pytest
id: install-pytest
run: |
pip install pytest
echo "✓ Installed pytest" >> $GITHUB_STEP_SUMMARY
continue-on-error: false
shell: bash
- name: Install additional requirements
run: |
if [ -f "${{ inputs.requirements-file }}" ]; then
pip3 install -r "${{ inputs.requirements-file }}"
echo "✓ Installed requirements from ${{ inputs.requirements-file }}" >> $GITHUB_STEP_SUMMARY
elif [ "${{ inputs.requirements-file }}" != "" ]; then
echo "Warning: Requirements file not found: ${{ inputs.requirements-file }}" >> $GITHUB_STEP_SUMMARY
echo "Skipping additional requirements installation."
fi
shell: bash
if: inputs.requirements-file != ''
continue-on-error: false
- name: Run pytest
run: |
echo "Running pytest..."
pytest ${{ inputs.pytest-options }} --verbose --tb=short 2>&1 | tee pytest_output.txt
echo "PYTEST_EXIT_CODE=${PIPESTATUS[0]}" >> $GITHUB_ENV
shell: bash
if: steps.install-pytest.outcome == 'success'
continue-on-error: true
- name: Report pytest results
run: |
echo "" >> $GITHUB_STEP_SUMMARY
echo "## pytest :test_tube:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo '```text' >> $GITHUB_STEP_SUMMARY
cat pytest_output.txt >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
rm -f pytest_output.txt
shell: bash
if: steps.install-pytest.outcome == 'success'
continue-on-error: false
- name: Generate SVG badges
id: generate-badges
run: |
mkdir -p "${{ inputs.badges-directory }}"
# Function to create badge
create_badge() {
local name=$1
local status=$2
local color=$3
local file=$4
cat > "$file" << 'EOF'
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<clipPath id="a">
<rect width="120" height="20" rx="3" fill="#fff"/>
</clipPath>
<g clip-path="url(#a)">
<path fill="#555" d="M0 0h60v20H0z"/>
<path fill="COLOR" d="M60 0h60v20H60z"/>
<path fill="url(#b)" d="M0 0h120v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="30" y="15" fill="#010101" fill-opacity=".3">NAME</text>
<text x="30" y="14">NAME</text>
<text x="90" y="15" fill="#010101" fill-opacity=".3">STATUS</text>
<text x="90" y="14">STATUS</text>
</g>
</svg>
EOF
sed -i "s/NAME/$name/g; s/STATUS/$status/g; s/COLOR/$color/g" "$file"
}
# Create badges for detected frameworks
PYTEST_STATUS="passing"
PYTEST_COLOR="#4c1"
[ "${PYTEST_EXIT_CODE:-0}" != "0" ] && PYTEST_STATUS="failing" && PYTEST_COLOR="#e05d44"
create_badge "pytest" "$PYTEST_STATUS" "$PYTEST_COLOR" "${{ inputs.badges-directory }}/pytest.svg"
echo "" >> $GITHUB_STEP_SUMMARY
echo "## SVG Badge(s)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "✓ Generated SVG badges in ${{ inputs.badges-directory }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
shell: bash
if: inputs.commit-badges == 'true'
continue-on-error: false
- name: Commit badges to repository
run: |
if [ "${{ inputs.commit-badges }}" == "true" ]]; then
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# Add badge files if they exist
git add "${{ inputs.badges-directory }}/*.svg" 2>/dev/null || true
echo "## Git" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "✓ Prepared to commit badge files" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if git diff --staged --quiet || false; then
echo "No changes to commit" >> $GITHUB_STEP_SUMMARY
else
git commit -m "Update test badges [skip ci]"
if git push || false; then
echo "✓ Changes committed and pushed to repository" >> $GITHUB_STEP_SUMMARY
else
echo "⚠ Warning: Failed to push changes. This may be due to insufficient permissions or a merge conflict." >> $GITHUB_STEP_SUMMARY
echo "Please ensure the workflow has write permissions to the repository." >> $GITHUB_STEP_SUMMARY
exit 0
fi
fi
fi
shell: bash
if: steps.generate-badges.outcome == 'success' && inputs.commit-badges == 'true'
continue-on-error: false