Skip to content

Commit 0c27bcc

Browse files
committed
Add more tests
1 parent a1ee32b commit 0c27bcc

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"author": "Test Asset",
3+
"classifications": [ "Test Asset" ],
4+
"name": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.AddNewSection",
5+
"generatorVersions": "[1.0.0.0-*)",
6+
"groupIdentity": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.AddNewSection",
7+
"precedence": "100",
8+
"identity": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.AddNewSection",
9+
"shortName": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.AddNewSection",
10+
"sourceName": "AddNewSection",
11+
"primaryOutputs": [
12+
{
13+
"path": "AddNewSection.csproj"
14+
}
15+
],
16+
"postActions": [
17+
{
18+
"description": "Add dotnet.config",
19+
"manualInstructions": [ { "text": "Create dotnet.config manually." } ],
20+
"args": {
21+
"section": "dotnet.test.runner",
22+
"key": "name",
23+
"value": "Microsoft.Testing.Platform"
24+
},
25+
"actionId": "597E7933-0D87-452C-B094-8FA0EEF7FD97",
26+
"continueOnError": true
27+
}
28+
]
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Basic
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Console.WriteLine("Hello World!");
11+
string result = JsonConvert.SerializeObject(new { a = "test" });
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[existing-section]
2+
mykey = "myvalue"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"author": "Test Asset",
3+
"classifications": [ "Test Asset" ],
4+
"name": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.CreateNonExisting",
5+
"generatorVersions": "[1.0.0.0-*)",
6+
"groupIdentity": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.CreateNonExisting",
7+
"precedence": "100",
8+
"identity": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.CreateNonExisting",
9+
"shortName": "TestAssets.PostActions.CreateOrUpdateDotnetConfig.CreateNonExisting",
10+
"sourceName": "CreateNonExisting",
11+
"primaryOutputs": [
12+
{
13+
"path": "CreateNonExisting.csproj"
14+
}
15+
],
16+
"postActions": [
17+
{
18+
"description": "Add dotnet.config",
19+
"manualInstructions": [ { "text": "Create dotnet.config manually." } ],
20+
"args": {
21+
"section": "dotnet.test.runner",
22+
"key": "name",
23+
"value": "Microsoft.Testing.Platform"
24+
},
25+
"actionId": "597E7933-0D87-452C-B094-8FA0EEF7FD97",
26+
"continueOnError": true
27+
}
28+
]
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Basic
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Console.WriteLine("Hello World!");
11+
string result = JsonConvert.SerializeObject(new { a = "test" });
12+
}
13+
}
14+
}

test/dotnet-new.IntegrationTests/PostActionTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Text.Json.Nodes;
55
using Microsoft.DotNet.Cli.Utils;
6+
using Microsoft.TemplateEngine.TestHelper;
67
using Microsoft.TemplateEngine.Utils;
78

89
namespace Microsoft.DotNet.Cli.New.IntegrationTests
@@ -1215,5 +1216,72 @@ public void AddJsonProperty_FailsWhenJsonFileNotFoundInEligableDirectories()
12151216

12161217
File.Delete(jsonFileLocation);
12171218
}
1219+
1220+
[Fact]
1221+
public void CreateOrUpdateDotnetConfig_CreateNonExisting()
1222+
{
1223+
string templateLocation = _testAssetsManager.CopyTestAsset("PostActions/CreateOrUpdateDotnetConfig/CreateNonExisting", testAssetSubdirectory: DotnetNewTestTemplatesBasePath).WithSource().Path;
1224+
string expectedTemplateName = "TestAssets.PostActions.CreateOrUpdateDotnetConfig.CreateNonExisting";
1225+
string home = CreateTemporaryFolder(folderName: "Home");
1226+
string workingDirectory = CreateTemporaryFolder();
1227+
string outputDirectory = TestUtils.CreateTemporaryFolder();
1228+
InstallTestTemplate(templateLocation, _log, home, workingDirectory);
1229+
1230+
var commandResult = new DotnetNewCommand(_log, expectedTemplateName, "-n", "MyProject", "-o", outputDirectory)
1231+
.WithCustomHive(home)
1232+
.WithWorkingDirectory(workingDirectory)
1233+
.Execute();
1234+
1235+
commandResult
1236+
.Should()
1237+
.ExitWith(0)
1238+
.And.NotHaveStdErr()
1239+
.And.HaveStdOutContaining($"The template \"{expectedTemplateName}\" was created successfully.")
1240+
.And.HaveStdOutContaining("Successfully created 'dotnet.config' file.")
1241+
.And.NotHaveStdOutContaining("Manual instructions");
1242+
1243+
Assert.Equal(
1244+
"""
1245+
[dotnet.test.runner]
1246+
name = "Microsoft.Testing.Platform"
1247+
1248+
""",
1249+
File.ReadAllText(Path.Combine(outputDirectory, "dotnet.config")));
1250+
}
1251+
1252+
[Fact]
1253+
public void CreateOrUpdateDotnetConfig_AddNewSection()
1254+
{
1255+
string templateLocation = _testAssetsManager.CopyTestAsset("PostActions/CreateOrUpdateDotnetConfig/AddNewSection", testAssetSubdirectory: DotnetNewTestTemplatesBasePath).WithSource().Path;
1256+
string expectedTemplateName = "TestAssets.PostActions.CreateOrUpdateDotnetConfig.AddNewSection";
1257+
string home = CreateTemporaryFolder(folderName: "Home");
1258+
string workingDirectory = CreateTemporaryFolder();
1259+
string outputDirectory = TestUtils.CreateTemporaryFolder();
1260+
InstallTestTemplate(templateLocation, _log, home, workingDirectory);
1261+
1262+
var commandResult = new DotnetNewCommand(_log, expectedTemplateName, "-n", "MyProject", "-o", outputDirectory)
1263+
.WithCustomHive(home)
1264+
.WithWorkingDirectory(workingDirectory)
1265+
.Execute();
1266+
1267+
commandResult
1268+
.Should()
1269+
.ExitWith(0)
1270+
.And.NotHaveStdErr()
1271+
.And.HaveStdOutContaining($"The template \"{expectedTemplateName}\" was created successfully.")
1272+
.And.HaveStdOutContaining("Created new section in 'dotnet.config' file")
1273+
.And.NotHaveStdOutContaining("Manual instructions");
1274+
1275+
Assert.Equal(
1276+
"""
1277+
[existing-section]
1278+
mykey = "myvalue"
1279+
1280+
[dotnet.test.runner]
1281+
name = "Microsoft.Testing.Platform"
1282+
1283+
""",
1284+
File.ReadAllText(Path.Combine(outputDirectory, "dotnet.config")));
1285+
}
12181286
}
12191287
}

0 commit comments

Comments
 (0)