Skip to content

Commit fd8f2ff

Browse files
committed
Added unit tests to the demo build
1 parent 111e5bb commit fd8f2ff

File tree

5 files changed

+125
-85
lines changed

5 files changed

+125
-85
lines changed
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,36 @@
11
using Cake.Core;
22

3-
public record WebsitePaths(
4-
string CsprojFile,
5-
string OutDir,
6-
string ZipOutDir,
7-
string ZipOutFilePath)
3+
using static AppPaths;
4+
5+
public record AppPaths(
6+
string SlnFile,
7+
string UnitTestsCsProj,
8+
DotNetProject AzFunctionsProject,
9+
DotNetProject WebClientProject)
810
{
9-
public static WebsitePaths LoadFromContext(ICakeContext context, string buildConfiguration, string srcDirectory, string buildArtifactsPath)
11+
public static AppPaths LoadFromContext(ICakeContext context, string buildConfiguration, string srcDirectory, string buildArtifactsPath)
1012
{
11-
var projectName = "FeedbackWebApp";
12-
var projectDir = srcDirectory + $"/{projectName}";
13-
var cprojFile = projectDir + $"/{projectName}.csproj";
14-
var outDir = projectDir + $"/bin/{buildConfiguration}/cake-build-output";
15-
var zipOutDir = buildArtifactsPath;
16-
var zipOutFilePath = zipOutDir + $"/feedback-web-client.zip";
13+
var slnFile = $"{srcDirectory}/Feedback.sln";
14+
var unitTestsCsProj = $"{srcDirectory}/UnitTests/UnitTests.csproj";
1715

18-
return new WebsitePaths(
19-
cprojFile,
20-
outDir,
21-
zipOutDir,
22-
zipOutFilePath);
23-
}
24-
}
16+
var azFunctionsProject = new DotNetProject(
17+
CsprojFile: $"{srcDirectory}/FeedbackFunctionsApp/FeedbackFunctionsApp.csproj",
18+
OutDir: $"{srcDirectory}/FeedbackFunctionsApp/bin/{buildConfiguration}/cake-build-output",
19+
ZipOutDir: buildArtifactsPath,
20+
ZipOutFilePath: $"{buildArtifactsPath}/feedback-web-client.zip");
2521

26-
public record FeedbackFunctionsProjectPaths(
27-
string CsprojFile,
28-
string OutDir,
29-
string ZipOutDir,
30-
string ZipOutPath)
31-
{
32-
public static FeedbackFunctionsProjectPaths LoadFromContext(ICakeContext context, string buildConfiguration, string srcDirectory, string buildArtifactsPath)
33-
{
34-
var projectName = "FeedbackFunctionsApp";
35-
var projectDir = srcDirectory + $"/{projectName}";
36-
var csprojFile = projectDir + $"/{projectName}.csproj";
37-
var outDir = projectDir + $"/bin/{buildConfiguration}/cake-build-output";
38-
var zipOutDir = buildArtifactsPath;
39-
var zipOutFilePath = zipOutDir + $"/feedback-functions.zip";
22+
var webClientProject = new DotNetProject(
23+
CsprojFile: $"{srcDirectory}/FeedbackWebApp/FeedbackWebApp.csproj",
24+
OutDir: $"{srcDirectory}/FeedbackWebApp/bin/{buildConfiguration}/cake-build-output",
25+
ZipOutDir: buildArtifactsPath,
26+
ZipOutFilePath: $"{buildArtifactsPath}/feedback-functions.zip");
4027

41-
return new FeedbackFunctionsProjectPaths(
42-
csprojFile,
43-
outDir,
44-
zipOutDir,
45-
zipOutFilePath);
28+
return new AppPaths(
29+
slnFile,
30+
unitTestsCsProj,
31+
azFunctionsProject,
32+
webClientProject);
4633
}
47-
}
34+
35+
public record DotNetProject(string CsprojFile, string OutDir, string ZipOutDir, string ZipOutFilePath);
36+
}

update-conference-prague-2024/demo-code-feedback-system/build/build/Program.cs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Cake.Common.Tools.DotNet.Build;
1313
using Cake.Common.Tools.DotNet.Publish;
1414
using System.Collections.Generic;
15+
using Cake.Common.Tools.DotNet.Test;
16+
using System.Net;
1517

1618
public static class Program
1719
{
@@ -29,8 +31,7 @@ public class BuildContext : FrostingContext
2931
public string BuildConfiguration { get; }
3032
public string SrcDirectoryPath { get; }
3133
public string BuildArtifactsPath { get; }
32-
public WebsitePaths WebClientPaths { get; }
33-
public FeedbackFunctionsProjectPaths AzFunctionsPaths { get; }
34+
public AppPaths AppPaths { get; }
3435

3536
public BuildContext(ICakeContext context)
3637
: base(context)
@@ -40,8 +41,7 @@ public BuildContext(ICakeContext context)
4041
SrcDirectoryPath = context.Argument<string>("srcDirectoryPath");
4142
BuildArtifactsPath = context.Argument<string>("buildArtifactsPath");
4243

43-
WebClientPaths = WebsitePaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, BuildArtifactsPath);
44-
AzFunctionsPaths = FeedbackFunctionsProjectPaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, BuildArtifactsPath);
44+
AppPaths = AppPaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, BuildArtifactsPath);
4545
}
4646
}
4747

@@ -63,7 +63,8 @@ public sealed class CleanTask : FrostingTask<BuildContext>
6363
{
6464
public override void Run(BuildContext context)
6565
{
66-
context.CleanDirectory(context.WebClientPaths.OutDir);
66+
context.CleanDirectory(context.AppPaths.AzFunctionsProject.OutDir);
67+
context.CleanDirectory(context.AppPaths.WebClientProject.OutDir);
6768
}
6869
}
6970

@@ -75,8 +76,8 @@ public override void Run(BuildContext context)
7576
{
7677
var buildFuncs = new[]
7778
{
78-
() => BuildDotnetApp(context, context.WebClientPaths.CsprojFile),
79-
() => BuildDotnetApp(context, context.AzFunctionsPaths.CsprojFile),
79+
() => BuildDotnetApp(context, context.AppPaths.AzFunctionsProject.CsprojFile),
80+
() => BuildDotnetApp(context, context.AppPaths.WebClientProject.CsprojFile),
8081
};
8182

8283
var runner = Parallel.ForEach(buildFuncs, func => func());
@@ -104,24 +105,23 @@ public sealed class RunUnitTestsTask : FrostingTask<BuildContext>
104105
{
105106
public override void Run(BuildContext context)
106107
{
107-
//TODO: Add some tests
108-
//var testSettings = new DotNetTestSettings()
109-
//{
110-
// Configuration = context.BuildConfiguration,
111-
// NoBuild = true,
112-
// ArgumentCustomization = (args) => args.Append("/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --logger trx")
113-
//};
114-
115-
//var runTestsFuncs = new[]
116-
//{
117-
// () => context.DotNetTest(context.WebClientPaths.UnitTestProj, testSettings),
118-
//};
119-
120-
//var runner = Parallel.ForEach(runTestsFuncs, func => func());
121-
//while (!runner.IsCompleted)
122-
//{
123-
// Thread.Sleep(100);
124-
//}
108+
var testSettings = new DotNetTestSettings()
109+
{
110+
Configuration = context.BuildConfiguration,
111+
NoBuild = true,
112+
ArgumentCustomization = (args) => args.Append("/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --logger trx")
113+
};
114+
115+
var runTestsFuncs = new[]
116+
{
117+
() => context.DotNetTest(context.AppPaths.UnitTestsCsProj, testSettings),
118+
};
119+
120+
var runner = Parallel.ForEach(runTestsFuncs, func => func());
121+
while (!runner.IsCompleted)
122+
{
123+
Thread.Sleep(100);
124+
}
125125
}
126126
}
127127

@@ -133,8 +133,8 @@ public override void Run(BuildContext context)
133133
{
134134
var buildFuncs = new List<Action>
135135
{
136-
() => PublishWebClient(context),
137-
() => PublishAzureFunctionsProject(context, context.AzFunctionsPaths),
136+
() => PublishWebClient(context, context.AppPaths.WebClientProject),
137+
() => PublishAzureFunctionsProject(context, context.AppPaths.AzFunctionsProject),
138138
};
139139

140140
var runner = Parallel.ForEach(buildFuncs, func => func());
@@ -144,59 +144,59 @@ public override void Run(BuildContext context)
144144
}
145145
}
146146

147-
private void PublishWebClient(BuildContext context)
147+
private void PublishWebClient(BuildContext context, AppPaths.DotNetProject webClient)
148148
{
149149
var settings = new DotNetPublishSettings()
150150
{
151151
NoRestore = true,
152152
NoBuild = true,
153153
Configuration = context.BuildConfiguration,
154-
OutputDirectory = context.WebClientPaths.OutDir,
154+
OutputDirectory = webClient.OutDir,
155155
};
156156

157-
context.DotNetPublish(context.WebClientPaths.CsprojFile, settings);
157+
context.DotNetPublish(webClient.CsprojFile, settings);
158158

159159
//Now that the code is published, create the compressed folder
160-
if (!Directory.Exists(context.WebClientPaths.ZipOutDir))
160+
if (!Directory.Exists(webClient.ZipOutDir))
161161
{
162-
_ = Directory.CreateDirectory(context.WebClientPaths.ZipOutDir);
162+
_ = Directory.CreateDirectory(webClient.ZipOutDir);
163163
}
164164

165-
if (File.Exists(context.WebClientPaths.ZipOutFilePath))
165+
if (File.Exists(webClient.ZipOutFilePath))
166166
{
167-
File.Delete(context.WebClientPaths.ZipOutFilePath);
167+
File.Delete(webClient.ZipOutFilePath);
168168
}
169169

170-
ZipFile.CreateFromDirectory(context.WebClientPaths.OutDir, context.WebClientPaths.ZipOutFilePath);
171-
context.Log.Information($"Output web app zip file to: {context.WebClientPaths.ZipOutFilePath}");
170+
ZipFile.CreateFromDirectory(webClient.OutDir, webClient.ZipOutFilePath);
171+
context.Log.Information($"Output web app zip file to: {webClient.ZipOutFilePath}");
172172
}
173173

174-
private void PublishAzureFunctionsProject(BuildContext context, FeedbackFunctionsProjectPaths apiPaths)
174+
private void PublishAzureFunctionsProject(BuildContext context, AppPaths.DotNetProject funcsProj)
175175
{
176176
var settings = new DotNetPublishSettings()
177177
{
178178
NoRestore = false,
179179
NoBuild = false,
180180
Configuration = context.BuildConfiguration,
181-
OutputDirectory = apiPaths.OutDir,
181+
OutputDirectory = funcsProj.OutDir,
182182
Runtime = "linux-x64",
183183
};
184184

185-
context.DotNetPublish(apiPaths.CsprojFile, settings);
185+
context.DotNetPublish(funcsProj.CsprojFile, settings);
186186

187187
//Now that the code is published, create the compressed folder
188-
if (!Directory.Exists(apiPaths.ZipOutDir))
188+
if (!Directory.Exists(funcsProj.ZipOutDir))
189189
{
190-
_ = Directory.CreateDirectory(apiPaths.ZipOutDir);
190+
_ = Directory.CreateDirectory(funcsProj.ZipOutDir);
191191
}
192192

193-
if (File.Exists(apiPaths.ZipOutPath))
193+
if (File.Exists(funcsProj.ZipOutFilePath))
194194
{
195-
File.Delete(apiPaths.ZipOutPath);
195+
File.Delete(funcsProj.ZipOutFilePath);
196196
}
197197

198-
ZipFile.CreateFromDirectory(apiPaths.OutDir, apiPaths.ZipOutPath);
199-
context.Log.Information($"Output functions zip file to: {apiPaths.ZipOutPath}");
198+
ZipFile.CreateFromDirectory(funcsProj.OutDir, funcsProj.ZipOutFilePath);
199+
context.Log.Information($"Output functions zip file to: {funcsProj.ZipOutFilePath}");
200200
}
201201
}
202202

update-conference-prague-2024/demo-code-feedback-system/src/FeedbackApp.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ VisualStudioVersion = 17.6.33801.468
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FeedbackFunctionsApp", "FeedbackFunctionsApp\FeedbackFunctionsApp.csproj", "{96D2E501-AEAC-4274-BDED-41E15426DDED}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeedbackWebApp", "FeedbackWebApp\FeedbackWebApp.csproj", "{20617955-1BDA-4561-AA45-15BC612BDA28}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FeedbackWebApp", "FeedbackWebApp\FeedbackWebApp.csproj", "{20617955-1BDA-4561-AA45-15BC612BDA28}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{55D0AED2-CEA1-4629-BA5D-9A968D7A0C93}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{20617955-1BDA-4561-AA45-15BC612BDA28}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{20617955-1BDA-4561-AA45-15BC612BDA28}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{20617955-1BDA-4561-AA45-15BC612BDA28}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{55D0AED2-CEA1-4629-BA5D-9A968D7A0C93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{55D0AED2-CEA1-4629-BA5D-9A968D7A0C93}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{55D0AED2-CEA1-4629-BA5D-9A968D7A0C93}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{55D0AED2-CEA1-4629-BA5D-9A968D7A0C93}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace UnitTests;
2+
3+
public class UnitTest1
4+
{
5+
[Fact]
6+
public void Test1()
7+
{
8+
Assert.True(true);
9+
}
10+
11+
[Fact]
12+
public void Test2()
13+
{
14+
Assert.False(false);
15+
}
16+
17+
[Fact]
18+
public void Test3()
19+
{
20+
Assert.True(!false);
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="xunit" Version="2.5.3" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Using Include="Xunit" />
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)