Skip to content

Commit

Permalink
feat: include test & coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Jan 11, 2025
1 parent 18b4bfe commit 870db83
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 18 deletions.
11 changes: 11 additions & 0 deletions template/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 1,
"isRoot": true,
"tools": {
"jetbrains.dotcover.globaltool": {
"version": "2023.2.5",
"commands": ["dotnet-dotcover"],
"rollForward": false
}
}
}
3 changes: 2 additions & 1 deletion template/.envrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
watch_file "./nix/env.nix" "./nix/fmt.nix" "./nix/packages.nix" "./nix/shells.nix" "./nix/pre-commit.nix" "./flake.nix" "./parse.nix"
use flake
pls setup
PATH_add "$HOME/.dotnet/tools"
echo "ℹ️ Run 'pls setup' to complete setup"
41 changes: 41 additions & 0 deletions template/.github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ jobs:
# pre commit
- name: Run pre-commit
run: nix develop .#ci -c ./scripts/ci/pre-commit.sh

sonarqube:
name: SonarQube
runs-on:
- nscloud-ubuntu-22.04-amd64-4x8-with-cache
- nscloud-cache-size-50gb
- nscloud-cache-tag-let___platform___-let___service___-sonarqube-cache
- nscloud-git-mirror-1gb
steps:
# Setup
- uses: AtomiCloud/actions.setup-nix@v1.2.1
- uses: AtomiCloud/actions.cache-nuget@v1.0.1

# sonarqube
- name: Build and analyze
env:
PLATFORM: 'let___platform___'
SERVICE: 'let___service___'

SONAR_ORG: 'atomi-cloud'
SONAR_HOST: 'https://sonarcloud.io'
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: nix develop .#sonar -c scripts/ci/sonarqube.sh

test:
name: Test and Coverage
runs-on:
- nscloud-ubuntu-22.04-amd64-4x8-with-cache
- nscloud-cache-size-50gb
- nscloud-cache-tag-let___platform___-let___service___-test-cache
- nscloud-git-mirror-1gb
steps:
# Setup
- uses: AtomiCloud/actions.setup-nix@v1.2.1
- uses: AtomiCloud/actions.cache-nuget@v1.0.1

# pre commit
- name: Run Test
run: nix develop .#ci -c ./scripts/ci/test.sh

publish:
name: Publish Current Commit
runs-on:
Expand Down Expand Up @@ -51,6 +91,7 @@ jobs:
needs:
- precommit
- publish
- test

if: github.ref == 'refs/heads/main'
runs-on:
Expand Down
4 changes: 3 additions & 1 deletion template/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ Chsarpier
.env
.idea
.direnv
.pre-commit-config.yaml
.pre-commit-config.yaml
dotCover.Output/
dotCover.Output.*
23 changes: 23 additions & 0 deletions template/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,32 @@ tasks:
desc: 'Setup the repository'
cmds:
- dotnet restore
- dotnet tool restore
- ./scripts/local/secrets.sh

build:
desc: 'Build the library'
cmds:
- dotnet build

test:
desc: 'Test the library'
cmds:
- dotnet test

test:cover:
desc: 'Test the library with coverage'
silent: true
cmds:
- ./scripts/ci/test.sh
test:cover:html:
desc: 'Test the library with html output'
silent: true
cmds:
- dotnet build --no-incremental > /dev/null
- dotnet dotcover test --dcReportType=HTML --dcFilters=-:testhost
test:clean:
desc: 'Clean the test results'
cmds:
- rm -rf ./dotCover.Output
- rm ./dotCover.Output.*
26 changes: 26 additions & 0 deletions template/UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="FluentAssertions" Version="8.0.0-rc.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\let___svc___\let___svc___.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions template/UnitTest/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using FluentAssertions;
using let___namespace___;

namespace UnitTest;

public class UnitTest1
{
[Fact]
public void Test1()
{
Class1.HelloWorld().Should().Be("Hello World");
}
}
2 changes: 1 addition & 1 deletion template/atomi_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ plugins:
assets:
- Changelog.md
- docs/developer/CommitConventions.md
- lib/lib.csproj
- let___svc___/let___svc___.csproj

# Github Release
- module: '@semantic-release/github'
Expand Down
8 changes: 7 additions & 1 deletion template/lib.sln → template/let___svc___.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lib", "lib\lib.csproj", "{1B2C2CFB-A0EE-4782-B502-A604FB121D4D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "let___svc___", "let___svc___\let___svc___.csproj", "{1B2C2CFB-A0EE-4782-B502-A604FB121D4D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "UnitTest\UnitTest.csproj", "{2770CEC3-5B19-41BA-972B-5796B3EF9DA5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,5 +20,9 @@ Global
{1B2C2CFB-A0EE-4782-B502-A604FB121D4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B2C2CFB-A0EE-4782-B502-A604FB121D4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B2C2CFB-A0EE-4782-B502-A604FB121D4D}.Release|Any CPU.Build.0 = Release|Any CPU
{2770CEC3-5B19-41BA-972B-5796B3EF9DA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2770CEC3-5B19-41BA-972B-5796B3EF9DA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2770CEC3-5B19-41BA-972B-5796B3EF9DA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2770CEC3-5B19-41BA-972B-5796B3EF9DA5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
File renamed without changes
9 changes: 9 additions & 0 deletions template/let___svc___/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace let___namespace___;

public class Class1
{
public static string HelloWorld()
{
return "Hello World";
}
}
File renamed without changes.
6 changes: 0 additions & 6 deletions template/lib/Class1.cs

This file was deleted.

4 changes: 4 additions & 0 deletions template/nix/env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ with packages;
sg
];

sonar = [
jdk
];

releaser = [
sg
];
Expand Down
1 change: 1 addition & 0 deletions template/nix/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let

yq = yq-go;
dotnet = dotnet-sdk_9;
jdk = zulu17;

inherit

Expand Down
20 changes: 15 additions & 5 deletions template/nix/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ pre-commit-lib.run {
enable = false;
};

a-dotnet-lint-lib = {
a-dotnet-lint-let___svc___ = {
enable = true;
name = "Lint .NET 'Lib' Project";
description = "Run formatter for .NET Project 'Lib'";
entry = "${packages.dotnet}/bin/dotnet format style --no-restore --severity info --verify-no-changes -v d ./lib/lib.csproj";
name = "Lint .NET 'let___svc___' Project";
description = "Run formatter for .NET Project 'let___svc___'";
entry = "${packages.dotnet}/bin/dotnet format style --no-restore --severity info --verify-no-changes -v d ./let___svc___/let___svc___.csproj";
language = "system";
pass_filenames = false;
files = "^lib/.*\\.cs$";
files = "^let___svc___/.*\\.cs$";
};

a-dotnet-lint-unit-test = {
enable = true;
name = "Lint .NET 'Unit Test' Project";
description = "Run formatter for .NET Project 'Unit Test'";
entry = "${packages.dotnet}/bin/dotnet format style --no-restore --severity info --verify-no-changes -v d ./UnitTest/UnitTest.csproj";
language = "system";
pass_filenames = false;
files = "^UnitTest/.*\\.cs$";
};

a-infisical = {
Expand Down
4 changes: 4 additions & 0 deletions template/nix/shells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ with env;
buildInputs = system ++ main ++ lint;
inherit shellHook;
};
sonar = pkgs.mkShell {
buildInputs = system ++ main ++ lint ++ sonar;
inherit shellHook;
};
releaser = pkgs.mkShell {
buildInputs = system ++ main ++ lint ++ releaser;
inherit shellHook;
Expand Down
4 changes: 2 additions & 2 deletions template/scripts/ci/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ echo "🪵 Current Branch: $BRANCH"

if [ "${RELEASE}" == "true" ]; then
echo "🔍 Full release detected, building with version"
dotnet pack ./lib/lib.csproj --output nupkgs
dotnet pack ./let___svc___/let___svc___.csproj --output nupkgs
else
echo "🔍 Pre-release detected, building with version suffix, $RELEASE_VERSION"
dotnet pack ./lib/lib.csproj --version-suffix "$RELEASE_VERSION" --output nupkgs
dotnet pack ./let___svc___/let___svc___.csproj --version-suffix "$RELEASE_VERSION" --output nupkgs
fi

echo "📦 Publishing packages..."
Expand Down
26 changes: 26 additions & 0 deletions template/scripts/ci/sonarqube.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

[ "${PLATFORM}" = '' ] && echo "❌ 'PLATFORM' env var not set" && exit 1
[ "${SERVICE}" = '' ] && echo "❌ 'SERVICE' env var not set" && exit 1
[ "${SONAR_TOKEN}" = '' ] && echo "❌ 'SONAR_TOKEN' env var not set" && exit 1
[ "${SONAR_HOST}" = '' ] && echo "❌ 'SONAR_HOST' env var not set" && exit 1
[ "${SONAR_ORG}" = '' ] && echo "❌ 'SONAR_ORG' env var not set" && exit 1

set -eou pipefail

# export path
export PATH="$PATH:$HOME/.dotnet/tools"

echo "⬇️ Installing Dependencies..."
dotnet restore
dotnet tool restore
echo "✅ Done!"

project="AtomiCloud_${PLATFORM}.${SERVICE}"

echo "📡 Starting SonarQube..."
dotnet sonarscanner begin /k:"${project}" /o:"${SONAR_ORG}" /d:sonar.token="${SONAR_TOKEN}" /d:sonar.host.url="${SONAR_HOST}" /d:sonar.cs.dotcover.reportsPaths=dotCover.Output.html
dotnet build --no-incremental
dotnet dotcover test --dcReportType=HTML
dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}"
echo "✅ Done!"
35 changes: 35 additions & 0 deletions template/scripts/ci/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

threshold=${1:-50}

set -eou pipefail

# export path
export PATH="$PATH:$HOME/.dotnet/tools"

echo "⬇️ Installing Dependencies..."
dotnet restore
dotnet tool restore
echo "✅ Done!"

# build the project
echo "🏗️ Building..."
dotnet build --no-incremental
echo "✅ Done!"

# run tests
echo "🧪 Running and Coverage..."
dotnet dotcover test --dcReportType=Json --dcFilters=-:testhost
echo "✅ Done!"

# print coverage
coverage=$(jq -r '.CoveragePercent' ./dotCover.Output.json)

echo "🧪 Current test coverage: ${coverage}%"

if (($(echo "$coverage > $threshold" | bc -l))); then
echo "✅ Coverage threshold of ${threshold}% met"
else
echo "❌ Failed:️ current test coverage is below threshold of ${threshold}%."
exit 1
fi
2 changes: 1 addition & 1 deletion template/scripts/ci/update_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version="$1"

set -eou pipefail

xmlstarlet ed -L -u '//Project/PropertyGroup/VersionPrefix' -v "$version" ./lib/lib.csproj
xmlstarlet ed -L -u '//Project/PropertyGroup/VersionPrefix' -v "$version" ./let___svc___/let___svc___.csproj

0 comments on commit 870db83

Please sign in to comment.