-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathAssemblyResolutionTests.cs
More file actions
164 lines (137 loc) · 6.86 KB
/
AssemblyResolutionTests.cs
File metadata and controls
164 lines (137 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
namespace MSTest.Acceptance.IntegrationTests;
[TestClass]
public sealed class AssemblyResolutionTests : AcceptanceTestBase<AssemblyResolutionTests.TestAssetFixture>
{
[TestMethod]
public async Task AssemblyResolution_WhenNotSpecified_TestFails()
{
TestHostResult testHostResult = await AssetFixture.TestHost.ExecuteAsync(cancellationToken: TestContext.CancellationToken);
// Assert
testHostResult.AssertExitCodeIs(2);
testHostResult.AssertOutputContains($"System.IO.FileNotFoundException: Could not load file or assembly '{TestAssetFixture.ProjectName}");
testHostResult.AssertOutputContainsSummary(failed: 1, passed: 0, skipped: 0);
}
[TestMethod]
public async Task AssemblyResolution_WhenSpecified_TestSucceeds()
{
// Arrange
string runSettingsFilePath = Path.Combine(AssetFixture.TestHost.DirectoryName, ".runsettings");
File.WriteAllText(runSettingsFilePath, $"""
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
</RunConfiguration>
<MSTestV2>
<AssemblyResolution>
<Directory path="{AssetFixture.MainDllFolder.Path}" />
</AssemblyResolution>
</MSTestV2>
</RunSettings>
""");
// Act
TestHostResult testHostResult = await AssetFixture.TestHost.ExecuteAsync($"--settings {runSettingsFilePath}", cancellationToken: TestContext.CancellationToken);
// Assert
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 1, skipped: 0);
testHostResult.AssertOutputDoesNotContain("System.IO.FileNotFoundException: Could not load file or assembly 'MSTest.Extensibility.Samples");
}
public sealed class TestAssetFixture : ITestAssetFixture
{
public const string ProjectName = "AssemblyResolution.Main";
private const string TestProjectName = "AssemblyResolution.Test";
private static readonly string TargetFramework = TargetFrameworks.NetCurrent;
private readonly TempDirectory _testAssetDirectory = new();
public TestHost TestHost { get; private set; } = null!;
public TempDirectory MainDllFolder { get; private set; } = null!;
public void Dispose()
{
_testAssetDirectory.Dispose();
MainDllFolder?.Dispose();
}
public async Task InitializeAsync(CancellationToken cancellationToken)
{
VSSolution solution = CreateTestAsset();
DotnetMuxerResult result = await DotnetCli.RunAsync($"build {solution.SolutionFile} -c Release", cancellationToken: cancellationToken);
result.AssertExitCodeIs(0);
TestHost = TestHost.LocateFrom(solution.Projects.Skip(1).Single().FolderPath, TestProjectName, TargetFramework);
MainDllFolder = MoveMainDllToDifferentTempDirectory();
}
private VSSolution CreateTestAsset()
{
VSSolution solution = new(Path.Combine(_testAssetDirectory.Path, "MSTestSolution"), "MSTestSolution");
solution.AddOrUpdateFileContent("NuGet.config", TestAsset.GetNuGetConfig(false, false));
CSharpProject mainProject = solution.CreateCSharpProject(ProjectName, TargetFramework);
mainProject.AddOrUpdateFileContent(mainProject.ProjectFile, $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>{TargetFramework}</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseAppHost>true</UseAppHost>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
""");
mainProject.AddOrUpdateFileContent("Class.cs", """
namespace AssemblyResolution.Main;
public class Class1
{
public int Add(int a, int b) => a + b;
}
""");
CSharpProject testProject = solution.CreateCSharpProject(TestProjectName, TargetFramework);
testProject.AddProjectReference(mainProject.ProjectFile);
testProject.AddOrUpdateFileContent(testProject.ProjectFile, $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>{TargetFramework}</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<UseAppHost>true</UseAppHost>
<LangVersion>preview</LangVersion>
<EnableMSTestRunner>true</EnableMSTestRunner>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="{MicrosoftTestingPlatformVersion}" />
<PackageReference Include="MSTest" Version="{MSTestVersion}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="{mainProject.ProjectFile}" />
</ItemGroup>
</Project>
""");
testProject.AddOrUpdateFileContent("UnitTest1.cs", $$"""
using {{ProjectName}};
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AssemblyResolution.Test;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(3, new Class1().Add(1, 2));
}
}
""");
return solution;
}
private TempDirectory MoveMainDllToDifferentTempDirectory()
{
string sampleDllFilePath = Path.Combine(TestHost.DirectoryName, $"{ProjectName}.dll");
Assert.IsTrue(File.Exists(sampleDllFilePath));
TempDirectory tempDirectory2 = new();
string newSampleDllFilePath = tempDirectory2.CopyFile(sampleDllFilePath);
Assert.IsTrue(File.Exists(newSampleDllFilePath));
File.Delete(sampleDllFilePath);
Assert.IsFalse(File.Exists(sampleDllFilePath));
return tempDirectory2;
}
}
public TestContext TestContext { get; set; }
}