Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit bd1f52b

Browse files
ianfnelsonLeeGathercolealex-clayton-1SamTyrrellNHS
authored
fix: Sonar test coverage (#12)
Co-authored-by: LEE GATHERCOLE <[email protected]> Co-authored-by: Lee Gathercole <[email protected]> Co-authored-by: alex-clayton-1 <[email protected]> Co-authored-by: Tyrrellion <[email protected]>
1 parent 641b030 commit bd1f52b

File tree

6 files changed

+59
-53
lines changed

6 files changed

+59
-53
lines changed

.github/actions/perform-static-analysis/action.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ runs:
4444
run: |
4545
mkdir -p .sonar/scanner
4646
dotnet tool install dotnet-sonarscanner --tool-path ./.sonar/scanner
47-
dotnet tool install dotnet-coverage --tool-path ./.sonar/scanner
47+
dotnet tool install dotnet-reportgenerator-globaltool --tool-path ./.sonar/scanner
48+
4849
- name: Build and analyze
4950
shell: bash
5051
run: |
51-
echo "${{ inputs.sonar_project_key }}"
52-
echo "${{ inputs.sonar_organisation_key }}"
53-
echo "${{ inputs.sonar_token }}"
54-
./.sonar/scanner/dotnet-sonarscanner begin /k:"${{ inputs.sonar_project_key }}" /o:"${{ inputs.sonar_organisation_key }}" /d:sonar.token="${{ inputs.sonar_token }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths="coverage.xml" /d:sonar.typescript.lcov.reportsPaths="src/web/coverage/lcov.info" /d:sonar.lang.patterns.ts=**/*.ts,**/*.tsx,**/*.cts,**/*.mts /d:sonar.lang.patterns.js=**/*.js,**/*.jsx,**/*.cjs,**/*.mjs,**/*.vue /d:sonar.javascript.enabled=false
55-
dotnet build src/ServiceLayer.sln
56-
./.sonar/scanner/dotnet-coverage collect -f xml -o coverage.xml dotnet test src/ServiceLayer.sln
57-
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ inputs.sonar_token }}"
52+
export SONAR_PROJECT_KEY="${{ inputs.sonar_project_key }}"
53+
export SONAR_ORGANISATION_KEY="${{ inputs.sonar_organisation_key }}"
54+
export SONAR_TOKEN="${{ inputs.sonar_token }}"
55+
make test-coverage

.github/workflows/stage-2-test.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,6 @@ jobs:
6565
- name: "Save the linting result"
6666
run: |
6767
echo "Nothing to save"
68-
test-coverage:
69-
name: "Test coverage"
70-
needs: [test-unit]
71-
runs-on: ubuntu-latest
72-
timeout-minutes: 5
73-
steps:
74-
- name: "Checkout code"
75-
uses: actions/checkout@v4
76-
- name: "Run test coverage check"
77-
run: |
78-
make test-coverage
79-
- name: "Save the coverage check result"
80-
run: |
81-
echo "Nothing to save"
8268
perform-static-analysis:
8369
name: "Perform static analysis"
8470
needs: [test-unit]

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,7 @@ __queuestorage__
288288

289289
# Gitleaks report
290290
gitleaks-report.json
291+
292+
coverage/
293+
.sonar/
294+
.sonarqube/

scripts/tests/coverage.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
: "${SONAR_TOKEN:?SONAR_TOKEN is required}"
5+
: "${SONAR_PROJECT_KEY:?SONAR_PROJECT_KEY is required}"
6+
: "${SONAR_ORGANISATION_KEY:?SONAR_ORGANISATION_KEY is required}"
7+
8+
SONAR_SCANNER="./.sonar/scanner/dotnet-sonarscanner"
9+
REPORT_GENERATOR="./.sonar/scanner/reportgenerator"
10+
11+
12+
13+
$SONAR_SCANNER begin /k:"$SONAR_PROJECT_KEY" /o:"$SONAR_ORGANISATION_KEY" /d:sonar.token="$SONAR_TOKEN" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.coverageReportPaths="coverage/converted/SonarQube.xml"
14+
15+
dotnet build src/ServiceLayer.sln
16+
17+
make test-unit ARGS="--no-build"
18+
19+
$REPORT_GENERATOR \
20+
-reports:coverage/**/coverage.cobertura.xml \
21+
-targetdir:coverage/converted \
22+
-reporttypes:SonarQube
23+
24+
$SONAR_SCANNER end /d:sonar.token="$SONAR_TOKEN"

scripts/tests/test.mk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ test: # Run all the test tasks @Testing
6565
test-load
6666

6767
_test:
68-
set -e
69-
script="./scripts/tests/${name}.sh"
70-
if [ -e "$${script}" ]; then
71-
exec $${script}
72-
else
73-
echo "make test-${name} not implemented: $${script} not found" >&2
68+
@set -e; \
69+
script="./scripts/tests/$(name).sh"; \
70+
if [ -e "$$script" ]; then \
71+
exec "$$script"; \
72+
else \
73+
echo "make test-$(name) not implemented: $$script not found" >&2; \
7474
fi
7575

7676
${VERBOSE}.SILENT: \

scripts/tests/unit.sh

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
#!/bin/bash
2-
32
set -euo pipefail
43

5-
cd "$(git rev-parse --show-toplevel)"
6-
7-
dir="$PWD"
8-
UnitDir="$dir/tests/"
9-
ResDir="$UnitDir"results-unit
10-
Format="trx"
11-
12-
# Find all *.csproj files excluding the IntegrationTests folder and execute dotnet test, with build for now
13-
find "$UnitDir" -name '*.csproj' -not -path "$UnitDir/IntegrationTests/*" | while read -r file; do
14-
echo -e "\nRunning unit tests for:\n$file"
15-
dotnet test "$file" --filter "TestCategory!=Integration" --logger $Format --verbosity quiet
16-
done
17-
18-
19-
# Move all trx result files into a separate folder, for easier reporting
20-
mkdir -p "$ResDir"
21-
find "$UnitDir" -name "*.$Format" -not -path "$ResDir/*" | while read -r resfile; do
22-
mv "$resfile" "$ResDir"
4+
BUILD_ARGS=""
5+
if [[ "${1:-}" == "--no-build" ]]; then
6+
BUILD_ARGS="--no-build"
7+
fi
8+
9+
COVERAGE_DIR="coverage"
10+
TEST_PROJECTS=$(find tests -name '*.csproj')
11+
12+
rm -rf "$COVERAGE_DIR"
13+
mkdir -p "$COVERAGE_DIR"
14+
15+
for proj in $TEST_PROJECTS; do
16+
proj_name=$(basename "$proj" .csproj)
17+
out_file="$COVERAGE_DIR/$proj_name.coverage.xml"
18+
echo "🧪 Running coverage for $proj -> $out_file"
19+
dotnet test "$proj" \
20+
$BUILD_ARGS \
21+
--collect:"XPlat Code Coverage" \
22+
--results-directory coverage
2323
done
24-
25-
# List created results
26-
echo -e "\nCreated result files:\n"
27-
find "$ResDir" -name "*.$Format"
28-
29-
# echo "Test execution completed. See scripts/tests/unit.sh for more."

0 commit comments

Comments
 (0)