Skip to content

Commit fe8a985

Browse files
EvangelinkCopilot
andauthored
Reduce redundant acceptance test preparation (#12197)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent fc7525d commit fe8a985

4 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AssemblyCleanupTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed class TestAssetFixture() : TestAssetFixtureBase()
3434

3535
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
3636
SourceCode
37-
.PatchTargetFrameworks(TargetFrameworks.All)
37+
.PatchTargetFrameworks([TargetFrameworks.NetCurrent])
3838
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
3939

4040
private const string SourceCode = """

‎test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/AnsiOptionTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
283283

284284
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (AssetName, AssetName,
285285
TestCode
286-
.PatchTargetFrameworks(TargetFrameworks.All)
286+
.PatchTargetFrameworks([TargetFrameworks.NetCurrent])
287287
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion));
288288
}
289289
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;
5+
6+
[TestClass]
7+
public sealed class TestAssetTests
8+
{
9+
public TestContext TestContext { get; set; } = null!;
10+
11+
[TestMethod]
12+
public async Task GenerateAssetAsync_WithoutDirectory_PreparesOnlyTheOwnedRoot()
13+
{
14+
string assetName = $"{nameof(GenerateAssetAsync_WithoutDirectory_PreparesOnlyTheOwnedRoot)}_{Guid.NewGuid():N}";
15+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
16+
assetName,
17+
"""
18+
#file Data.txt
19+
asset
20+
""");
21+
22+
string[] preparedRoots = [.. Directory.EnumerateDirectories(TempDirectory.TestSuiteDirectory)
23+
.Where(root => Directory.Exists(Path.Combine(root, assetName)))];
24+
25+
Assert.HasCount(1, preparedRoots);
26+
Assert.AreEqual(Path.GetDirectoryName(testAsset.TargetAssetPath), preparedRoots[0]);
27+
Assert.AreEqual(assetName, testAsset.AssetId);
28+
Assert.AreSequenceEqual(
29+
["asset"],
30+
await File.ReadAllLinesAsync(Path.Combine(testAsset.TargetAssetPath, "Data.txt"), TestContext.CancellationToken));
31+
Assert.IsTrue(File.Exists(Path.Combine(testAsset.TargetAssetPath, "NuGet.config")));
32+
}
33+
34+
[TestMethod]
35+
public async Task GenerateAssetAsync_WithSameName_KeepsAssetsIndependent()
36+
{
37+
string assetName = $"{nameof(GenerateAssetAsync_WithSameName_KeepsAssetsIndependent)}_{Guid.NewGuid():N}";
38+
using TestAsset first = await TestAsset.GenerateAssetAsync(
39+
assetName,
40+
"""
41+
#file Data.txt
42+
first
43+
""",
44+
addDefaultNuGetConfigFile: false);
45+
using TestAsset second = await TestAsset.GenerateAssetAsync(
46+
assetName,
47+
"""
48+
#file Data.txt
49+
second
50+
""",
51+
addDefaultNuGetConfigFile: false);
52+
53+
Assert.AreNotEqual(first.TargetAssetPath, second.TargetAssetPath);
54+
Assert.AreSequenceEqual(
55+
["first"],
56+
await File.ReadAllLinesAsync(Path.Combine(first.TargetAssetPath, "Data.txt"), TestContext.CancellationToken));
57+
Assert.AreSequenceEqual(
58+
["second"],
59+
await File.ReadAllLinesAsync(Path.Combine(second.TargetAssetPath, "Data.txt"), TestContext.CancellationToken));
60+
}
61+
62+
[TestMethod]
63+
public async Task Dispose_WithBorrowedDirectory_LeavesCallerOwnedFiles()
64+
{
65+
using TempDirectory tempDirectory = new();
66+
await TempDirectory.WriteFileAsync(tempDirectory.Path, "Caller.txt", "caller");
67+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
68+
nameof(Dispose_WithBorrowedDirectory_LeavesCallerOwnedFiles),
69+
"""
70+
#file Data.txt
71+
asset
72+
""",
73+
tempDirectory,
74+
addDefaultNuGetConfigFile: false);
75+
76+
testAsset.Dispose();
77+
78+
Assert.AreSequenceEqual(
79+
["caller"],
80+
await File.ReadAllLinesAsync(Path.Combine(tempDirectory.Path, "Caller.txt"), TestContext.CancellationToken));
81+
Assert.AreSequenceEqual(
82+
["asset"],
83+
await File.ReadAllLinesAsync(Path.Combine(testAsset.TargetAssetPath, "Data.txt"), TestContext.CancellationToken));
84+
}
85+
}

‎test/Utilities/Microsoft.Testing.TestInfrastructure/TestAsset.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public TestAsset(string assetId, string assetCode, TempDirectory tempDirectory)
2727
}
2828

2929
public TestAsset(string assetName, string assetCode)
30-
: this(assetName, assetCode, new TempDirectory(subDirectory: null))
3130
{
3231
AssetId = assetName;
3332
_assetCode = assetCode;

0 commit comments

Comments
 (0)