Skip to content

Commit 63e4b3c

Browse files
authored
Merge pull request #2514 from berryzplus/feature/add_pytest
Pythonスクリプトのテストを追加する
2 parents 544df33 + b62e139 commit 63e4b3c

19 files changed

Lines changed: 516 additions & 226 deletions

.github/workflows/build-on-msys2.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@ jobs:
2828
with:
2929
submodules: true
3030

31+
- name: Install OpenCppCoverage (winget)
32+
if: ${{ matrix.config == 'Debug' }}
33+
run: |
34+
winget install `
35+
--id OpenCppCoverage.OpenCppCoverage `
36+
--exact `
37+
--accept-package-agreements `
38+
--accept-source-agreements
39+
shell: pwsh
40+
3141
- name: Set up Python
3242
uses: actions/setup-python@v6
43+
with:
44+
python-version: ${{ vars.PYTHON_VERSION || '3.14.3' }}
45+
cache: 'pip'
46+
- run: pip install -r requirements.txt
3347

3448
- name: Set up MSYS2
3549
uses: msys2/setup-msys2@v2
@@ -41,6 +55,7 @@ jobs:
4155
mingw-w64-x86_64-gcc
4256
mingw-w64-x86_64-cmake
4357
mingw-w64-x86_64-make
58+
mingw-w64-x86_64-python
4459
mingw-w64-x86_64-7zip
4560
mingw-w64-x86_64-gdb
4661

.github/workflows/build-sakura.yml

Lines changed: 91 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,71 @@ jobs:
104104
- name: Add msbuild to PATH
105105
uses: microsoft/setup-msbuild@v3
106106

107+
# SonarQubeのBuild Wrapperをインストールする
108+
- name: Install Build Wrapper
109+
if: ${{ matrix.platform == 'x64' && matrix.config == 'Debug' }}
110+
uses: SonarSource/sonarqube-scan-action/install-build-wrapper@713881670b6b3676cda39549040e2d88c70d582e # v8.2.0
111+
112+
# OpenCppCoverageをインストールする
113+
- name: Install OpenCppCoverage (winget)
114+
id: install_opencppcoverage_winget
115+
if: ${{ matrix.config == 'Debug' }}
116+
continue-on-error: true
117+
run: |
118+
winget install `
119+
--id OpenCppCoverage.OpenCppCoverage `
120+
--exact `
121+
--accept-package-agreements `
122+
--accept-source-agreements
123+
shell: pwsh
124+
125+
# wingetは失敗することがあるので、インストーラー方式も残しておく。
126+
- name: Install OpenCppCoverage (installer)
127+
if: ${{ matrix.config == 'Debug' && steps.install_opencppcoverage_winget.outcome == 'failure' }}
128+
working-directory: ${{ runner.temp }}
129+
run: |
130+
$installer = '.\OpenCppCoverageSetup.exe'
131+
Invoke-WebRequest `
132+
-Uri "https://github.com/OpenCppCoverage/OpenCppCoverage/releases/download/release-0.9.9.0/OpenCppCoverageSetup-x64-0.9.9.0.exe" `
133+
-OutFile $installer
134+
$expectedSha256 = '2295a733da39412c61e4f478677519dd0bb1893d88313ce56b468c9e50517888'
135+
$actualSha256 = (Get-FileHash $installer -Algorithm SHA256).Hash.ToLowerInvariant()
136+
if ($actualSha256 -ne $expectedSha256.ToLowerInvariant()) {
137+
throw "SHA256 mismatch: expected=$expectedSha256 actual=$actualSha256"
138+
}
139+
Start-Process `
140+
-FilePath $installer `
141+
-ArgumentList "/VERYSILENT", "/SUPPRESSMSGBOXES" `
142+
-Wait
143+
echo "C:\Program Files\OpenCppCoverage" >> $env:GITHUB_PATH
144+
shell: pwsh
145+
107146
- name: Set up Python
108-
if: ${{ matrix.platform == 'x64' }}
109147
uses: actions/setup-python@v6
148+
with:
149+
python-version: ${{ vars.PYTHON_VERSION || '3.14.3' }}
150+
cache: 'pip'
151+
- run: pip install -r requirements.txt
152+
153+
- name: Copy github-event.json
154+
run: |
155+
Copy-Item "$env:GITHUB_EVENT_PATH" github-event.json
156+
shell: pwsh
157+
158+
- name: Generate sonar metadata
159+
if: ${{ matrix.platform == 'x64' && matrix.config == 'Debug' }}
160+
env:
161+
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
162+
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
163+
run: |
164+
[ordered]@{
165+
event_name = '${{ github.event_name }}'
166+
ref_name = '${{ github.ref_name }}'
167+
pr_number = '${{ github.event.pull_request.number }}'
168+
pr_head_ref = $env:PR_HEAD_REF
169+
pr_base_ref = $env:PR_BASE_REF
170+
} | ConvertTo-Json | Set-Content sonar-metadata.json
171+
shell: pwsh
110172

111173
- name: Setup PR environment variables
112174
if: github.event_name == 'pull_request'
@@ -117,7 +179,13 @@ jobs:
117179
echo "GITHUB_PR_HEAD_SHORT_COMMIT=$('${{ github.event.pull_request.head.sha }}'.SubString(0, 8))" >> $env:GITHUB_ENV
118180
shell: pwsh
119181

182+
- name: Build with Build-Wrapper
183+
if: ${{ matrix.platform == 'x64' && matrix.config == 'Debug' }}
184+
run: build-wrapper-win-x86-64.exe --out-dir bw-output MsBuild.exe /p:Platform=${{ matrix.platform }} /p:Configuration=${{ matrix.config }}
185+
shell: cmd
186+
120187
- name: MSBuild
188+
if: ${{ matrix.platform != 'x64' || matrix.config != 'Debug' }}
121189
run: build-sln.bat ${{ matrix.platform }} ${{ matrix.config }}
122190
shell: cmd
123191

@@ -128,10 +196,30 @@ jobs:
128196
path: help
129197

130198
- name: Run unit tests
131-
run: .\tests1.exe --gtest_output=xml:${{github.workspace}}\tests1.exe-googletest-${{matrix.platform}}-${{matrix.config}}.xml
132-
working-directory: ${{github.workspace}}\${{matrix.platform}}\${{matrix.config}}
199+
run: ctest
200+
--test-dir build/${{ matrix.platform }}/CMakeTools
201+
-C ${{ matrix.config }}
202+
--output-on-failure
133203
shell: pwsh
134204

205+
- name: Upload test result
206+
uses: EnricoMi/publish-unit-test-result-action/windows@v2
207+
with:
208+
files: |
209+
*-googletest.xml
210+
211+
- name: Upload sonar-input artifact
212+
if: ${{ matrix.platform == 'x64' && matrix.config == 'Debug' }}
213+
uses: actions/upload-artifact@v7
214+
with:
215+
name: sonar-input
216+
path: |
217+
bw-output/
218+
tests*-coverage.xml
219+
tests*-googletest.xml
220+
sonar-metadata.json
221+
github-event.json
222+
135223
# Toolbar Bitmap Split/Mux
136224
- run: build-bmp-tools.bat ${{matrix.config}}
137225
name: Bitmap Split/Mux
@@ -181,98 +269,3 @@ jobs:
181269
with:
182270
name: Dev ${{ matrix.platform }} ${{ matrix.config }}
183271
path: 'sakura-*-Dev.zip'
184-
185-
sonar-build:
186-
name: MSBuild for SonarCloud (x64/Debug)
187-
runs-on: windows-latest
188-
189-
env:
190-
BUILD_PLATFORM: x64
191-
BUILD_CONFIGURATION: Debug
192-
193-
steps:
194-
- uses: actions/checkout@v7
195-
with:
196-
fetch-depth: 0
197-
submodules: true
198-
199-
- name: Add msbuild to PATH
200-
uses: microsoft/setup-msbuild@30375c66a4eea26614e0d39710365f22f8b0af57 # v3
201-
202-
- name: Set up Python
203-
uses: actions/setup-python@v6
204-
205-
- name: Install AutoHotKey
206-
run: |
207-
$AutohotkeyDir = "$HOME\autohotkey"
208-
if (!(Test-Path $AutohotkeyDir)) { New-Item -ItemType Directory -Path $AutohotkeyDir }
209-
$ProgressPreference = 'SilentlyContinue'
210-
Invoke-WebRequest -Uri https://github.com/AutoHotkey/AutoHotkey/releases/download/v2.0.19/AutoHotkey_2.0.19.zip -OutFile "AutoHotkey.zip"
211-
Expand-Archive -Path "AutoHotkey.zip" -DestinationPath $AutohotkeyDir -Force
212-
echo "AutoHotKey=$HOME\autohotkey\AutoHotkey32.exe" >> $env:GITHUB_ENV
213-
214-
- name: Install Locale Emulator
215-
run: |
216-
$LEExpandDir = "${{ github.workspace }}\tools\LocaleEmulator"
217-
$LEInitScript = "${{ github.workspace }}\tools\LocaleEmulator\init-locale-emulator.ahk"
218-
Expand-Archive "${{ github.workspace }}\externals\LocaleEmulator\locale-emulator.zip" "${LEExpandDir}"
219-
Start-Process $env:AutoHotKey "${LEInitScript}"
220-
Start-Process "${LEExpandDir}\LEInstaller.exe"
221-
echo "${LEExpandDir}" >> $env:GITHUB_PATH
222-
shell: pwsh
223-
224-
- name: Install Build Wrapper
225-
uses: SonarSource/sonarqube-scan-action/install-build-wrapper@713881670b6b3676cda39549040e2d88c70d582e # v8.2.0
226-
227-
- name: Build with Build-Wrapper
228-
run: build-wrapper-win-x86-64.exe --out-dir bw-output MsBuild.exe /p:Platform=%BUILD_PLATFORM% /p:Configuration=%BUILD_CONFIGURATION% /t:ReBuild
229-
shell: cmd
230-
231-
- name: Install OpenCppCoverage
232-
working-directory: ${{ runner.temp }}
233-
run: |
234-
$ProgressPreference = 'SilentlyContinue'
235-
Invoke-WebRequest -Uri "https://github.com/OpenCppCoverage/OpenCppCoverage/releases/download/release-0.9.9.0/OpenCppCoverageSetup-x64-0.9.9.0.exe" -OutFile OpenCppCoverageSetup.exe
236-
Start-Process -FilePath .\OpenCppCoverageSetup.exe -ArgumentList "/VERYSILENT", "/SUPPRESSMSGBOXES" -Wait
237-
echo "C:\Program Files\OpenCppCoverage" >> $env:GITHUB_PATH
238-
shell: pwsh
239-
240-
- name: Run Tests with coverage
241-
run: |
242-
$env:CMD_TESTS1 = $(Get-ChildItem -Recurse tests1.exe)[0].FullName
243-
OpenCppCoverage.exe `
244-
--export_type cobertura:tests1-coverage.xml `
245-
--modules $env:CMD_TESTS1 `
246-
--sources ${{ github.workspace }} `
247-
--working_dir ${{ github.workspace }}\${{ env.BUILD_PLATFORM }}\${{ env.BUILD_CONFIGURATION }} `
248-
--cover_children `
249-
-- `
250-
$env:CMD_TESTS1 `
251-
--gtest_output=xml:${{ github.workspace }}\tests1-googletest.xml
252-
shell: pwsh
253-
254-
- name: Save sonar metadata
255-
env:
256-
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
257-
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
258-
run: |
259-
[ordered]@{
260-
event_name = '${{ github.event_name }}'
261-
ref_name = '${{ github.ref_name }}'
262-
pr_number = '${{ github.event.pull_request.number }}'
263-
pr_head_ref = $env:PR_HEAD_REF
264-
pr_base_ref = $env:PR_BASE_REF
265-
} | ConvertTo-Json | Set-Content sonar-metadata.json
266-
Copy-Item "$env:GITHUB_EVENT_PATH" github-event.json
267-
shell: pwsh
268-
269-
- name: Upload sonar-input artifact
270-
uses: actions/upload-artifact@v7
271-
with:
272-
name: sonar-input
273-
path: |
274-
bw-output/
275-
tests1-coverage.xml
276-
tests1-googletest.xml
277-
sonar-metadata.json
278-
github-event.json

.github/workflows/check-encoding.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Check Encoding
1+
name: Check Encoding
22

33
on:
44
push:
@@ -26,23 +26,17 @@ jobs:
2626
run: set
2727
shell: cmd
2828

29-
- name: Set up Python ${{ matrix.python-version }}
29+
- name: Set up Python
3030
uses: actions/setup-python@v6
3131
with:
32-
python-version: '3.x'
32+
python-version: ${{ vars.PYTHON_VERSION || '3.14.3' }}
33+
cache: 'pip'
34+
- run: pip install -r requirements.txt
3335

3436
- name: Python version check
3537
run: python --version
3638
shell: cmd
3739

38-
- name: Upgrade pip
39-
run: python -m pip install --upgrade pip
40-
shell: cmd
41-
42-
- name: Install chardet
43-
run: python -m pip install chardet==6.0.0
44-
shell: cmd
45-
4640
- name: Check encoding
4741
run: python checkEncoding.py
4842
shell: cmd

.github/workflows/python-check.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Python Compile Check
1+
name: Python Compile Check
22

33
on:
44
push:
@@ -16,17 +16,20 @@ on:
1616
jobs:
1717
python-compile-check:
1818
runs-on: windows-latest
19-
strategy:
20-
matrix:
21-
python-version: ['3.x']
19+
2220
steps:
2321
- name: Checkout repository
2422
uses: actions/checkout@v7
23+
with:
24+
fetch-depth: 1
25+
submodules: false
2526

26-
- name: Set up Python ${{ matrix.python-version }}
27+
- name: Set up Python
2728
uses: actions/setup-python@v6
2829
with:
29-
python-version: ${{ matrix.python-version }}
30+
python-version: ${{ vars.PYTHON_VERSION || '3.14.3' }}
31+
cache: 'pip'
32+
- run: pip install -r requirements.txt
3033

3134
- name: Python version check
3235
run: python --version

.github/workflows/sonarscan.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
"-Dsonar.cfamily.compile-commands=bw-output/compile_commands.json"
7272
"-Dsonar.cfamily.cppunit.reportPath=*-googletest.xml"
7373
"-Dsonar.cfamily.cobertura.reportPaths=tests1-coverage.xml"
74+
"-Dsonar.python.coverage.reportPaths=pytest-coverage.xml"
7475
"-Dsonar.verbose=true"
7576
7677
- name: Upload Sonar CFamily failure reproducer (if present)

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ tags
2828
testData_*.txt
2929
UpgradeLog.htm
3030

31+
# PyTest出力ファイルは除外する
32+
.coverage
33+
pytest-coverage.xml
34+
3135
# SonarScanローカル実行用(解析ツールをダウンロードするフォルダ)
3236
.sonar
3337

@@ -38,7 +42,9 @@ UpgradeLog.htm
3842
SONAR_TOKEN
3943

4044
# SonarScanローカル実行用したときに生成されるテスト結果ファイル
41-
tests1-googletest.xml
45+
tests*-googletest.xml
46+
tests*-coverage.xml
47+
LastCoverageResults.log
4248

4349
# SonarScanローカル実行の失敗再現用アーカイブ
4450
sonar-cfamily-reproducer.tar.xz

CMakeLists.txt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,42 @@ add_subdirectory("${CMAKE_SOURCE_DIR}/tools/ppa_stub"
4646
add_subdirectory("${CMAKE_SOURCE_DIR}/tools/dll_plugin1"
4747
"${CMAKE_BINARY_DIR}/dll_plugin1")
4848

49+
# Convert paths to native Windows format (backslashes) for OpenCppCoverage
50+
file(TO_NATIVE_PATH "${CMAKE_SOURCE_DIR}" CMAKE_SOURCE_DIR_NATIVE)
51+
file(TO_NATIVE_PATH "${OUTPUT_DIRECTORY}" OUTPUT_DIRECTORY_NATIVE)
52+
4953
# Include tests1 definition
5054
include(${CMAKE_SOURCE_DIR}/src/test/cmake/tests1.cmake)
5155

56+
if(OpenCppCoverage_EXECUTABLE)
57+
add_test(NAME CoverageTest
58+
COMMAND ${OpenCppCoverage_EXECUTABLE}
59+
--export_type cobertura:${CMAKE_SOURCE_DIR_NATIVE}\\tests1-coverage.xml
60+
--modules ${OUTPUT_DIRECTORY_NATIVE}\\tests1.exe
61+
--sources ${CMAKE_SOURCE_DIR_NATIVE}
62+
--excluded_sources ${CMAKE_SOURCE_DIR_NATIVE}\\build
63+
--working_dir ${OUTPUT_DIRECTORY_NATIVE}
64+
--cover_children
65+
--
66+
$<TARGET_FILE:tests1>
67+
--gtest_output=xml:${CMAKE_SOURCE_DIR}/tests1-googletest.xml
68+
CONFIGURATIONS Debug
69+
)
70+
endif()
71+
5272
add_test(NAME UnitTest
5373
COMMAND $<TARGET_FILE:tests1>
54-
--gtest_output=xml:${OUTPUT_DIRECTORY}/tests1-googletest.xml
74+
--gtest_output=xml:${CMAKE_SOURCE_DIR}/tests1-googletest.xml
75+
WORKING_DIRECTORY ${OUTPUT_DIRECTORY}
76+
CONFIGURATIONS Release
77+
)
78+
79+
add_test(NAME pytest
80+
COMMAND ${Python3_EXECUTABLE} -m pytest
81+
--ignore=externals
82+
--cov
83+
--cov-config=src/test/py/.coveragerc
84+
--cov-report=xml:pytest-coverage.xml
85+
--cov-fail-under=80
86+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
5587
)

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
chardet>=6
2+
pytest>=8,<9
3+
pytest-cov>=5,<6
4+
pytest-mock>=3,<4

0 commit comments

Comments
 (0)