Skip to content

Commit d846077

Browse files
committed
Add tests
1 parent 23a5ab8 commit d846077

File tree

8 files changed

+117
-5
lines changed

8 files changed

+117
-5
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37-
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\" />
37+
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\" Visible="false" />
3838
</ItemGroup>
3939

4040
</Project>

DotNetProjectStarter.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
<Folder Name="/src/">
1818
<Project Path="src/DotNetProjectStarter/DotNetProjectStarter.csproj" />
1919
</Folder>
20+
<Folder Name="/tests/">
21+
<Project Path="tests/DotNetProjectStarter.Tests/DotNetProjectStarter.Tests.csproj" />
22+
</Folder>
2023
</Solution>

src/DotNetProjectStarter/LibraryTemplateConstants.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@
233233
234234
""";
235235

236+
public const string TestProjectAssemblyInfoFile = """
237+
using Microsoft.VisualStudio.TestTools.UnitTesting;
238+
239+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel, Workers = 0)]
240+
241+
""";
242+
236243
public const string EditorConfigFile = """
237244
# top-most EditorConfig file
238245
root = true
@@ -1112,8 +1119,8 @@ Temporary Items
11121119
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11131120
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
11141121
<DebugType>embedded</DebugType>
1115-
<SignAssembly>true</SignAssembly>
1116-
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)key.snk</AssemblyOriginatorKeyFile>
1122+
<AssemblyOriginatorKeyFile Condition="Exists('$(MSBuildThisFileDirectory)key.snk')">$(MSBuildThisFileDirectory)key.snk</AssemblyOriginatorKeyFile>
1123+
<SignAssembly Condition="$(AssemblyOriginatorKeyFile) != ''">true</SignAssembly>
11171124
<PackageReadmeFile>README.md</PackageReadmeFile>
11181125
11191126
<!-- By default, NuGet doesn't include portable PDBs in nupkg. -->
@@ -1135,7 +1142,7 @@ Temporary Items
11351142
</ItemGroup>
11361143
11371144
<ItemGroup>
1138-
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\" />
1145+
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\" Visible="false" />
11391146
</ItemGroup>
11401147
11411148
</Project>
@@ -1240,4 +1247,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
12401247
</packageSources>
12411248
</configuration>
12421249
""";
1243-
}
1250+
}

src/DotNetProjectStarter/LibraryTemplateGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public bool Generate(TemplateGenerationOptions options)
3333
Directory.CreateDirectory(testsProjectDirectory);
3434
File.WriteAllText(Path.Combine(testsDirectory, $"Directory.Build.props"), LibraryTemplateConstants.TestDirectoryBuildProps);
3535
File.WriteAllText(Path.Combine(testsProjectDirectory, $"{projectName}.Tests.csproj"), LibraryTemplateConstants.TestProjectFile);
36+
File.WriteAllText(Path.Combine(testsProjectDirectory, "AssemblyInfo.cs"), LibraryTemplateConstants.TestProjectAssemblyInfoFile);
3637

3738
File.WriteAllText(Path.Combine(outputDirectory, ".editorconfig"), LibraryTemplateConstants.EditorConfigFile);
3839
File.WriteAllText(Path.Combine(outputDirectory, ".gitattributes"), LibraryTemplateConstants.GitAttributesFile);

tests/Directory.Build.props

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
3+
<Import Condition="Exists($([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../')))" Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
4+
5+
<PropertyGroup>
6+
<NoWarn>$(NoWarn);CS1591</NoWarn>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel, Workers = 0)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="MSTest.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\DotNetProjectStarter\DotNetProjectStarter.csproj" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace DotNetProjectStarter.Tests;
8+
9+
[TestClass]
10+
public sealed class LibraryTemplateGeneratorEndToEndTests
11+
{
12+
private readonly string _outputDirectory;
13+
14+
public LibraryTemplateGeneratorEndToEndTests()
15+
=> _outputDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
16+
17+
[TestCleanup]
18+
public void TestCleanup()
19+
// TODO: Consider not deleting on failure, and adding to attachments?
20+
=> Directory.Delete(_outputDirectory, recursive: true);
21+
22+
[TestMethod]
23+
public async Task CreateProjectShouldSucceed()
24+
{
25+
var result = await RunToolWithArgumentsAsync(["--name", "CreateProjectShouldSucceedProjectName", "--output-directory", _outputDirectory]);
26+
Assert.AreEqual(0, result.ExitCode);
27+
28+
var buildResult = await RunDotNetBuildWithArgumentsAsync([], Path.Combine(_outputDirectory, "CreateProjectShouldSucceedProjectName"));
29+
Assert.AreEqual(0, buildResult.ExitCode);
30+
}
31+
32+
[TestMethod]
33+
public async Task CreateProjectShouldSucceedWithTreatWarningsAsErrors()
34+
{
35+
var result = await RunToolWithArgumentsAsync(["--name", "CreateProjectShouldSucceedWithTreatWarningsAsErrorsProjectName", "--output-directory", _outputDirectory]);
36+
Assert.AreEqual(0, result.ExitCode);
37+
38+
var buildResult = await RunDotNetBuildWithArgumentsAsync(["-p:TreatWarningsAsErrors=true"], Path.Combine(_outputDirectory, "CreateProjectShouldSucceedWithTreatWarningsAsErrorsProjectName"));
39+
Assert.AreEqual(0, buildResult.ExitCode);
40+
}
41+
42+
private static async Task<ToolResult> RunToolWithArgumentsAsync(string[] arguments, string? workingDirectory = null)
43+
=> await RunProcessWithArgumentsAsync(
44+
Path.Combine(Directory.GetCurrentDirectory(), "DotNetProjectStarter" + (OperatingSystem.IsWindows() ? ".exe" : string.Empty)),
45+
arguments,
46+
workingDirectory: workingDirectory ?? Directory.GetCurrentDirectory());
47+
48+
private static async Task<ToolResult> RunDotNetBuildWithArgumentsAsync(string[] arguments, string? workingDirectory = null)
49+
=> await RunProcessWithArgumentsAsync(
50+
"dotnet",
51+
["build", .. arguments],
52+
workingDirectory: workingDirectory ?? Directory.GetCurrentDirectory());
53+
54+
private static async Task<ToolResult> RunProcessWithArgumentsAsync(string filePath, string[] arguments, string workingDirectory)
55+
{
56+
var processStartInfo = new ProcessStartInfo(filePath, arguments)
57+
{
58+
UseShellExecute = false,
59+
RedirectStandardError = true,
60+
RedirectStandardOutput = true,
61+
WorkingDirectory = workingDirectory
62+
};
63+
64+
using var process = Process.Start(processStartInfo);
65+
Assert.IsNotNull(process);
66+
67+
var stdoutTask = Task.Factory.StartNew(() => process.StandardOutput.ReadToEnd());
68+
var stderrTask = Task.Factory.StartNew(() => process.StandardError.ReadToEnd());
69+
70+
var standardOutputAndError = await Task.WhenAll(stdoutTask, stderrTask).ConfigureAwait(false);
71+
72+
await process.WaitForExitAsync();
73+
74+
return new ToolResult(process.ExitCode, standardOutputAndError[0], standardOutputAndError[1]);
75+
}
76+
77+
private sealed record ToolResult(int ExitCode, string StandardOutput, string StandardError);
78+
}

0 commit comments

Comments
 (0)