Skip to content

Commit

Permalink
ci: Preparing library for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed Oct 17, 2023
1 parent 7534106 commit f53c1ce
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 3 deletions.
119 changes: 119 additions & 0 deletions .github/workflows/build-library.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Build library

on:
push:
branches:
- main
- develop
#- feature/*
paths:
- 'src/SvR.ContentPrep/**'
- '.github/workflows/build-library.yml'
pull_request:
branches:
- main
- devevelop
paths:
- 'src/SvR.ContentPrep/**'
- '.github/workflows/build-library.yml'
workflow_dispatch:

jobs:
test:
name: 🛠️ Build and Test
runs-on: ubuntu-latest
permissions:
contents: read
issues: read
checks: write
steps:
- name: 👨‍💻 Check-out code
uses: actions/checkout@v3

- name: 👨‍🔧 Setup .NET Core SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.x

- name: 🔍 Enable problem matchers
run: echo "::add-matcher::.github/matchers/dotnet.json"

- name: 🦸‍♂️ Restore steriods
uses: actions/cache@v3
with:
path: ~/.nuget/packages
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget
- name: 🎒 Load packages
run: dotnet restore

- name: 🛠️ Build code
run: dotnet build --configuration Release --no-restore

- name: ✔️ Testing code
run: dotnet test --configuration Release -v minimal --no-build --logger GitHubActions '/p:CollectCoverage=true;CoverletOutputFormat="json,lcov,cobertura";MergeWith=${{github.workspace}}/coverage.json;CoverletOutput=${{github.workspace}}/coverage' -- RunConfiguration.CollectSourceInformation=true

- name: 📝 Code Coverage report
run: |
dotnet tool install --global dotnet-reportgenerator-globaltool --version 5.1.23
reportgenerator -reports:${{github.workspace}}/coverage.cobertura.xml -targetdir:${{github.workspace}}/report -reporttypes:MarkdownSummaryGithub -filefilters:-*.g.cs "-classfilters:-WixSharp.*;-WingetIntune.Os.*;-WingetIntune.Internal.MsStore.*" -verbosity:Warning
sed -i 's/# Summary/## 📝 Code Coverage/g' ${{github.workspace}}/report/SummaryGithub.md
sed -i 's/## Coverage/### 📝 Code Coverage details/g' ${{github.workspace}}/report/SummaryGithub.md
cat ${{github.workspace}}/report/*.md >> $GITHUB_STEP_SUMMARY
- name: 📃 Check code formatting
if: always()
run: dotnet format --verify-no-changes

publish:
name: 📦 Publish nuget library
runs-on: ubuntu-latest
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/dev') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
needs: [test]
steps:
- name: 👨‍💻 Check-out code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: 👨‍🔧 Setup .NET Core SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.x

- name: 🛠️ Compute version with GitVersion
run: |
dotnet tool install --global GitVersion.Tool --version 5.*
echo "## 📦 Calculated version" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
dotnet-gitversion /updateprojectfiles /output buildserver /nofetch
dotnet-gitversion /nofetch /output json /showvariable NuGetVersionV2 >> $GITHUB_STEP_SUMMARY
- name: 🔍 Enable problem matchers
run: echo "::add-matcher::.github/matchers/dotnet.json"

- name: 🦸‍♂️ Restore steriods
uses: actions/cache@v3
with:
path: ~/.nuget/packages
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget
- name: 🎒 Load packages
run: dotnet restore

- name: 🛠️ Build code
run: dotnet build --configuration Release --no-restore ./src/SvR.ContentPrep/SvR.ContentPrep.csproj

- name: 📦 Pack library
run: dotnet pack --configuration Release --no-build --no-restore ./src/SvR.ContentPrep/SvR.ContentPrep.csproj

- name: ✈️ Publish SvR.ContentPrep to nuget.org
run: dotnet nuget push ./src/SvR.ContentPrep/bin/Release/SvRooij.ContentPrep.*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,7 @@ MigrationBackup/
FodyWeavers.xsd

# Dist folder
dist/
dist/

# Live unit testing files
*.lutconfig
10 changes: 10 additions & 0 deletions GitVersion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
assembly-versioning-scheme: MajorMinorPatch
mode: Mainline
branches:
feature:
tag: beta
ignore:
sha: []
major-version-bump-message: "^(build|ci|feat|fix|perf|refactor|revert|style|test)(\\([\\w\\s-]*\\))?(!:|:.*\\n\\n((.+\\n)+\\n)?BREAKING CHANGE:\\s.+)"
minor-version-bump-message: "^(feat)(\\([\\w\\s-]*\\))?:"
patch-version-bump-message: "^(build|ci|fix|perf|refactor|revert|style|test)(\\([\\w\\s-]*\\))?:"
11 changes: 11 additions & 0 deletions SvR.ContentPrep.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvR.ContentPrep.Cmdlet", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvR.ContentPrep", "src\SvR.ContentPrep\SvR.ContentPrep.csproj", "{5AC53639-49F9-433C-BAE3-7E954D25CEBA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{6F02D0D4-B300-40A6-9111-CCD20863C7C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SvR.ContentPrep.Tests", "tests\SvR.ContentPrep.Tests\SvR.ContentPrep.Tests.csproj", "{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,5 +28,12 @@ Global
{5AC53639-49F9-433C-BAE3-7E954D25CEBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AC53639-49F9-433C-BAE3-7E954D25CEBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AC53639-49F9-433C-BAE3-7E954D25CEBA}.Release|Any CPU.Build.0 = Release|Any CPU
{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D90E8B52-F816-43E6-A3D5-0C9E86CD57A2} = {6F02D0D4-B300-40A6-9111-CCD20863C7C2}
EndGlobalSection
EndGlobal
23 changes: 21 additions & 2 deletions src/SvR.ContentPrep/Packager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ public async Task CreatePackage(
ApplicationDetails? applicationDetails = null,
CancellationToken cancellationToken = default)
{
CheckCreateParamsOrThrow(folder, setupFile, outputFolder);
logger.LogInformation("Creating package for {SetupFile} in {Folder} to {OutputFolder}", setupFile, folder, outputFolder);
string tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
string outputFileName = GetOutputFileName(setupFile, outputFolder);
try
{
CheckParamsOrThrow(folder, setupFile, outputFolder);
//CheckParamsOrThrow(folder, setupFile, outputFolder);
string packageFolder = Path.Combine(tempFolder, "IntuneWinPackage");
string packageContentFolder = Path.Combine(packageFolder, "Contents");
string encryptedPackageLocation = Path.Combine(packageContentFolder, EncryptedPackageFileName);
Expand Down Expand Up @@ -123,6 +124,7 @@ public async Task Unpack(
string outputFolder,
CancellationToken cancellationToken = default)
{
CheckUnpackParamsOrThrow(packageFile, outputFolder);
logger.LogInformation("Unpacking intunewin at {PackageFile} to {OutputFolder}", packageFile, outputFolder);
string tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
try
Expand Down Expand Up @@ -199,12 +201,29 @@ private static void CheckSetupFileExistsAndInSetupFolder(string setupFolder, str
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Setup file '{0}' should be in folder '{1}'", setupFile, setupFolder));
}

private static void CheckParamsOrThrow(string folder, string setupFile, string outputFolder)
private static void CheckCreateParamsOrThrow(string folder, string setupFile, string outputFolder)
{
if (string.IsNullOrEmpty(folder))
throw new ArgumentNullException(nameof(folder));
if (string.IsNullOrEmpty(setupFile))
throw new ArgumentNullException(nameof(setupFile));
if (string.IsNullOrEmpty(outputFolder))
throw new ArgumentNullException(nameof(outputFolder));
if (!Directory.Exists(folder))
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Folder '{0}' can not be found", folder));
CheckSetupFileExistsAndInSetupFolder(folder, setupFile);
TryWritingToFolder(outputFolder);
}

private static void CheckUnpackParamsOrThrow(string packageFile, string outputFolder)
{
if (string.IsNullOrEmpty(packageFile))
throw new ArgumentNullException(nameof(packageFile));
if (string.IsNullOrEmpty(outputFolder))
throw new ArgumentNullException(nameof(outputFolder));
if (!File.Exists(packageFile))
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "File '{0}' can not be found", packageFile));
TryWritingToFolder(outputFolder);
}
}
}
125 changes: 125 additions & 0 deletions tests/SvR.ContentPrep.Tests/Packager.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
namespace SvRooij.ContentPrep.Tests;

[TestClass]
public class PackagerTests
{
[TestMethod]
public void CreatePackage_NullFolder_ThrowsArgumentNull()
{
// Arrange
var packager = new Packager();

Assert.ThrowsExceptionAsync<ArgumentNullException>(() => packager.CreatePackage(null, null, null));
}

[TestMethod]
public void CreatePackage_NullSetupFile_ThrowsArgumentNull()
{
// Arrange
var packager = new Packager();

Assert.ThrowsExceptionAsync<ArgumentNullException>(() => packager.CreatePackage("test", null, null));
}

[TestMethod]
public void CreatePackage_NullOutput_ThrowsArgumentNull()
{
// Arrange
var packager = new Packager();

Assert.ThrowsExceptionAsync<ArgumentNullException>(() => packager.CreatePackage("test", "test", null));
}

[TestMethod]
public void CreatePackage_NonExistingFolder_ThrowsDirectoryNotFound()
{
// Arrange
var packager = new Packager();
var inputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());


Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(() => packager.CreatePackage(inputFolder, "test", "test"));
}

[TestMethod]
public void CreatePackage_NonExistingSetup_ThrowsFileNotFound()
{
// Arrange
var packager = new Packager();
var inputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(inputFolder);
var setup = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".msi");

Assert.ThrowsExceptionAsync<FileNotFoundException>(() => packager.CreatePackage(inputFolder, setup, "test"));
Directory.Delete(inputFolder);
}

[TestMethod]
public void CreatePackage_ExistingSetupWrongFolder_ThrowsArgumentException()
{
// Arrange
var packager = new Packager();
var inputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(inputFolder);
var setup = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".msi");
File.CreateText(setup).Close();

Assert.ThrowsExceptionAsync<ArgumentException>(() => packager.CreatePackage(inputFolder, setup, "test"));
Directory.Delete(inputFolder, true);
}

[TestMethod]
public void CreatePackage_NoOutputDir_ThrowsDirectoryNotFound()
{
// Arrange
var packager = new Packager();
var inputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var outputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(inputFolder);
var setup = Path.Combine(inputFolder, Guid.NewGuid().ToString() + ".msi");
File.CreateText(setup).Close();

Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(() => packager.CreatePackage(inputFolder, setup, outputFolder));
Directory.Delete(inputFolder, true);
//Directory.Delete(outputFolder, true);
}

[TestMethod]
public void Unpack_NullPackage_ThrowsArgumentNull()
{
// Arrange
var packager = new Packager();

Assert.ThrowsExceptionAsync<ArgumentNullException>(() => packager.Unpack(null, null));
}

[TestMethod]
public void Unpack_NullOutput_ThrowsArgumentNull()
{
// Arrange
var packager = new Packager();

Assert.ThrowsExceptionAsync<ArgumentNullException>(() => packager.Unpack("test", null));
}

[TestMethod]
public void Unpack_NonExistingPackage_ThrowsFileNotFound()
{
// Arrange
var packager = new Packager();
var package = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".msi");

Assert.ThrowsExceptionAsync<FileNotFoundException>(() => packager.Unpack(package, "test"));
}

[TestMethod]
public void Unpack_NonExistingOutput_ThrowsDirectoryNotFound()
{
var packager = new Packager();
var package = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".msi");
File.CreateText(package).Close();
var outputFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(() => packager.Unpack(package, outputFolder));
}
}
25 changes: 25 additions & 0 deletions tests/SvR.ContentPrep.Tests/SvR.ContentPrep.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DefaultNamespace>SvRooij.ContentPrep.Tests</DefaultNamespace>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<NoWarn>CS8625</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SvR.ContentPrep\SvR.ContentPrep.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions tests/SvR.ContentPrep.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;

0 comments on commit f53c1ce

Please sign in to comment.