Skip to content

Commit 4312135

Browse files
authored
Merge pull request #21 from magic5644:feat-github-action-rework
Update GitHub Actions for multi-platform .NET support and improve null safety
2 parents 2efb699 + 06bef88 commit 4312135

3 files changed

Lines changed: 52 additions & 21 deletions

File tree

.github/workflows/dotnet.yml

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,50 @@
33

44
name: .NET
55

6-
on:
7-
push:
8-
branches: ["main"]
9-
pull_request:
10-
branches: ["main"]
6+
on: [push, pull_request]
7+
8+
env:
9+
DOTNET_CLI_TELEMETRY_OPTOUT: 1
1110

1211
jobs:
1312

1413
build:
15-
runs-on: ubuntu-latest
14+
name: ${{ matrix.platform.name }} ${{ matrix.dotnet.name }}
15+
runs-on: ${{ matrix.platform.os }}
16+
#runs-on: ubuntu-latest
17+
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
platform:
22+
- { name: Linux, os: ubuntu-24.04 }
23+
- { name: Windows, os: windows-2022 }
24+
- { name: macOS, os: macos-15 }
25+
dotnet:
26+
- { name: .NET 9, version: "9.0.x" }
1627

1728
steps:
1829
- uses: actions/checkout@v4
19-
- name: Setup .NET
30+
- id: setup-dotnet
2031
uses: actions/setup-dotnet@v4
2132
with:
22-
dotnet-version: 9.0.x
23-
- name: Restore dependencies
24-
run: dotnet restore
33+
dotnet-version: ${{ matrix.dotnet.version }}
34+
- name: Enforce SDK Version
35+
run: dotnet new globaljson --sdk-version ${{ steps.setup-dotnet.outputs.dotnet-version }} --force
2536
- name: Build
26-
run: dotnet build --no-restore
27-
28-
- run: dotnet test --results-directory "test-results" --collect:"Code Coverage"
29-
- run: dotnet tool update --global dotnet-coverage
30-
- run: dotnet-coverage merge --output test-result.cobertura.xml --output-format cobertura "test-results/**/*.coverage"
31-
- run: dotnet tool install --global dotnet-reportgenerator-globaltool
32-
- run: reportgenerator -reports:test-result.cobertura.xml -targetdir:coverage-report -reporttypes:"Html;JsonSummary;MarkdownSummaryGithub;Badges"
33-
- run: cat coverage-report/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
37+
run: dotnet build -c Release
38+
- name: Test
39+
if: matrix.platform.name == 'MacOS'
40+
run: |
41+
dotnet test --results-directory "test-results" --collect:"Code Coverage"
42+
dotnet tool update --global dotnet-coverage
43+
dotnet-coverage merge --output test-result.cobertura.xml --output-format cobertura "test-results/**/*.coverage"
44+
dotnet tool install --global dotnet-reportgenerator-globaltool
45+
reportgenerator -reports:test-result.cobertura.xml -targetdir:coverage-report -reporttypes:"Html;JsonSummary;MarkdownSummaryGithub;Badges"
46+
cat coverage-report/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
3447
3548
- name: ReportGenerator
49+
if: matrix.platform.name == 'MacOS'
3650
uses: danielpalme/ReportGenerator-GitHub-Action@5.3.11
3751
with:
3852
reports: "test-result.cobertura.xml"
@@ -41,34 +55,38 @@ jobs:
4155

4256
- name: Upload coverage report artifact
4357
uses: actions/upload-artifact@v4
58+
if: matrix.platform.name == 'MacOS'
4459
with:
4560
name: coverage-report
4661
path: coverage-report
4762

4863
- name: Upload coverage badge artifact
4964
uses: actions/upload-artifact@v4
65+
if: matrix.platform.name == 'MacOS'
5066
with:
5167
name: coverage-badge.svg
5268
path: coverage-report/badge_combined.svg
5369

5470
- name: Add comment to PR
55-
if: github.event_name == 'pull_request'
71+
if: github.event_name == 'pull_request' && matrix.platform.name == 'MacOS'
5672
run: gh pr comment $PR_NUMBER --body-file coverage-report/SummaryGithub.md
5773
env:
5874
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5975
PR_NUMBER: ${{ github.event.pull_request.number }}
6076

6177
- name: Publish coverage in build summary # Only applicable if 'MarkdownSummaryGithub' or one of the other Markdown report types is generated
78+
if: matrix.platform.name == 'MacOS'
6279
run: cat coverage-report/SummaryGithub.md >> $GITHUB_STEP_SUMMARY # Adjust path and filename if necessary
6380
shell: bash
6481

6582
- name: Generate Coverage Badge
83+
if: matrix.platform.name == 'MacOS'
6684
run: |
6785
echo "![Coverage](./coverage-report/badge_combined.svg)" > coverage-badge.md
6886
cat coverage-badge.md >> README.md
6987
7088
publish_badge:
71-
runs-on: ubuntu-latest
89+
runs-on: ubuntu-24.04
7290
needs: build
7391
steps:
7492
- name: Checkout gh-pages

CodeLineCounter.Tests/CoreUtilsTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public void GetUserChoice_Should_Return_Valid_Choice()
117117
int solutionCount = 5;
118118
string input = "3";
119119
var inputStream = new StringReader(input);
120+
var consoleOutput = new StringWriter();
121+
Console.SetOut(consoleOutput);
120122
Console.SetIn(inputStream);
121123

122124
// Act
@@ -133,6 +135,8 @@ public void GetUserChoice_Should_Return_Invalid_Choice()
133135
int solutionCount = 5;
134136
string input = "invalid";
135137
var inputStream = new StringReader(input);
138+
var consoleOutput = new StringWriter();
139+
Console.SetOut(consoleOutput);
136140
Console.SetIn(inputStream);
137141

138142
// Act
@@ -149,6 +153,8 @@ public void GetUserChoice_returns_valid_selection_for_valid_input()
149153
// Arrange
150154
var input = "2";
151155
var consoleInput = new StringReader(input);
156+
var consoleOutput = new StringWriter();
157+
Console.SetOut(consoleOutput);
152158
Console.SetIn(consoleInput);
153159

154160
// Act
@@ -166,6 +172,8 @@ public void GetUserChoice_handles_invalid_input(string input)
166172
{
167173
// Arrange
168174
var consoleInput = new StringReader(input);
175+
var consoleOutput = new StringWriter();
176+
Console.SetOut(consoleOutput);
169177
Console.SetIn(consoleInput);
170178

171179
// Act

CodeLineCounter/Services/DependencyAnalyzer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ public static void AnalyzeFile(string filePath, string sourceCode)
118118
{
119119
SourceClass = GetSimpleTypeName(classDeclaration),
120120
SourceNamespace = classDeclaration.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault()?.Name.ToString() ?? "",
121-
SourceAssembly = classDeclaration.SyntaxTree.GetRoot().DescendantNodes().OfType<CompilationUnitSyntax>().FirstOrDefault()?.Usings.FirstOrDefault()?.Name.ToString() ?? "",
121+
SourceAssembly = classDeclaration.SyntaxTree.GetRoot()
122+
.DescendantNodes()
123+
.OfType<CompilationUnitSyntax>()
124+
.FirstOrDefault()?
125+
.Usings.FirstOrDefault()?
126+
.Name?.ToString() ?? "",
122127
TargetClass = dependency.Split('.')[(dependency.Split('.').Length - 1)],
123128
TargetNamespace = dependency.Contains(".") ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",
124129
TargetAssembly = dependency.Contains(".") ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",

0 commit comments

Comments
 (0)