Skip to content

Commit 1618a37

Browse files
Copilotdsplaisted
andcommitted
Add test coverage for framework incompatibility error message
Added test GivenAToolWithHigherFrameworkItShowsAppropriateErrorMessage that verifies the improved error message when installing a tool that targets a higher .NET version than is installed. The test creates a mock tool package with net99.0 framework and validates that the error message includes helpful information about the version mismatch. Co-authored-by: dsplaisted <[email protected]>
1 parent a612275 commit 1618a37

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/Microsoft.DotNet.PackageInstall.Tests/ToolPackageDownloaderTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,5 +965,68 @@ public ToolPackageDownloaderTests(ITestOutputHelper log, TestToolBuilder toolBui
965965
{
966966
ToolBuilder = toolBuilder;
967967
}
968+
969+
[Fact]
970+
public void GivenAToolWithHigherFrameworkItShowsAppropriateErrorMessage()
971+
{
972+
// Create a mock tool package with net99.0 framework to simulate a tool requiring a higher .NET version
973+
var testDir = _testAssetsManager.CreateTestDirectory();
974+
var fileSystem = new FileSystemWrapper();
975+
var packageId = new PackageId("test.tool.higher.framework");
976+
var packageVersion = new NuGetVersion("1.0.0");
977+
var packageRoot = new DirectoryPath(testDir.Path).WithSubDirectories(".store", packageId.ToString(), packageVersion.ToNormalizedString());
978+
979+
// Create the package directory structure with net99.0 framework
980+
var toolsPath = Path.Combine(packageRoot.Value, "tools", "net99.0", "any");
981+
fileSystem.Directory.CreateDirectory(toolsPath);
982+
983+
// Create DotnetToolSettings.xml
984+
var settingsContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
985+
<DotNetCliTool Version=""1"">
986+
<Commands>
987+
<Command Name=""test-tool"" EntryPoint=""test.dll"" Runner=""dotnet"" />
988+
</Commands>
989+
</DotNetCliTool>";
990+
fileSystem.File.WriteAllText(Path.Combine(toolsPath, "DotnetToolSettings.xml"), settingsContent);
991+
992+
// Create a dummy assembly file
993+
fileSystem.File.WriteAllText(Path.Combine(toolsPath, "test.dll"), "dummy");
994+
995+
// Create an empty asset file (simulating NuGet restore with no compatible frameworks)
996+
var assetFilePath = Path.Combine(packageRoot.Value, "project.assets.json");
997+
var currentFramework = $"net{Environment.Version.Major}.{Environment.Version.Minor}";
998+
var assetFileContents = $$"""
999+
{
1000+
"version": 3,
1001+
"targets": {
1002+
"{{currentFramework}}/{{RuntimeInformation.RuntimeIdentifier}}": {
1003+
"{{packageId}}/{{packageVersion}}": {
1004+
"type": "package",
1005+
"tools": {
1006+
}
1007+
}
1008+
}
1009+
},
1010+
"libraries": {},
1011+
"projectFileDependencyGroups": {}
1012+
}
1013+
""";
1014+
fileSystem.File.WriteAllText(assetFilePath, assetFileContents);
1015+
1016+
// Try to create a ToolPackageInstance, which should throw an informative error
1017+
Action action = () =>
1018+
{
1019+
_ = new ToolPackageInstance(
1020+
packageId,
1021+
packageVersion,
1022+
new DirectoryPath(testDir.Path).WithSubDirectories(".store"),
1023+
packageRoot,
1024+
fileSystem);
1025+
};
1026+
1027+
action.Should().Throw<ToolConfigurationException>()
1028+
.WithMessage("*requires a higher version of .NET*")
1029+
.WithMessage("*.NET 99*");
1030+
}
9681031
}
9691032
}

0 commit comments

Comments
 (0)