Skip to content

Commit 3753a69

Browse files
committed
Parameterize dotnet version for test project creation
1 parent e421f3f commit 3753a69

File tree

4 files changed

+86
-33
lines changed

4 files changed

+86
-33
lines changed

detect-nuget-inspector/detect-nuget-inspector-tests/shantys-tests/KickOffTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,34 @@ namespace DetectNugetInspectorTests.ShantysTests
66
[TestClass]
77
public class KickOffTests
88
{
9+
910
[TestMethod]
10-
public void TestBasicSetup()
11+
public void TestBasicSetup_InvalidDotNetVersion()
1112
{
1213
var runner = new NITestRunner();
13-
var result = runner.RunBasicSetupTest("6.0.100", "MyTestSolution");
1414

15-
Assert.IsTrue(result.Success, result.Message);
16-
Console.WriteLine(result.Message);
15+
Assert.ThrowsException<InvalidOperationException>(() =>
16+
{
17+
runner.RunBasicSetupTest("99.0.999", "FailureSolution");
18+
});
1719
}
1820

1921
[TestMethod]
20-
public void TestBasicSetup_WithDifferentVersion()
22+
public void TestBasicSetup_DotNet6()
2123
{
2224
var runner = new NITestRunner();
23-
var result = runner.RunBasicSetupTest("6.0.136", "AnotherTestSolution");
25+
var result = runner.RunBasicSetupTest("6.0.428", "MyTestSolution", "dotnet6");
2426

2527
Assert.IsTrue(result.Success, result.Message);
26-
Console.WriteLine(result.Message);
2728
}
2829

29-
[TestMethod]
30-
public void TestBasicSetup_InvalidDotNetVersion()
30+
[TestMethod]
31+
public void TestBasicSetup_DotNet7()
3132
{
3233
var runner = new NITestRunner();
34+
var result = runner.RunBasicSetupTest("7.0.410", "MyTestSolution", "dotnet7");
3335

34-
Assert.ThrowsException<InvalidOperationException>(() =>
35-
{
36-
runner.RunBasicSetupTest("99.0.999", "FailureSolution");
37-
});
36+
Assert.IsTrue(result.Success, result.Message);
3837
}
3938
}
4039
}

detect-nuget-inspector/detect-nuget-inspector-tests/shantys-tests/NITestRunner.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public class NITestRunner
88
{
99
private List<string> _executedBranches = new List<string>();
1010

11-
public NIResult RunBasicSetupTest(string dotnetVersion = "6.0.136", string solutionName = "TestSolution")
11+
public NIResult RunBasicSetupTest(string dotnetVersion = "6.0.428", string solutionName = "TestSolution", string dotnetCommand = "dotnet")
1212
{
13-
Console.WriteLine($"Starting basic setup test with .NET version: {dotnetVersion}");
13+
Console.WriteLine($"Starting basic setup test with .NET version: {dotnetVersion} using command: {dotnetCommand}");
1414

1515
// 1. Setup environment
1616
var env = new TestEnvironmentManager();
1717
try
1818
{
19-
env.SetupEnvironment(dotnetVersion);
19+
env.SetupEnvironment(dotnetVersion, dotnetCommand);
2020
Console.WriteLine($"✓ Environment setup successful - Working directory: {env.WorkingDirectory}");
2121
}
2222
catch (Exception ex)
@@ -69,7 +69,7 @@ public NIResult RunBasicSetupTest(string dotnetVersion = "6.0.136", string solut
6969
return new NIResult
7070
{
7171
Success = true,
72-
Message = $"Basic setup test completed successfully for .NET {dotnetVersion}",
72+
Message = $"Basic setup test completed successfully for .NET {dotnetVersion} using {dotnetCommand}",
7373
SolutionPath = solutionPath
7474
};
7575
}
Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,113 @@
11
using System;
2-
using System.IO;
32
using System.Diagnostics;
3+
using System.IO;
44

55
namespace DetectNugetInspectorTests.ShantysTests
66
{
77
public class TestEnvironmentManager
88
{
99
public string DotNetVersion { get; private set; }
10-
public string DotNetCommand { get; private set; } // New property
10+
public string NuGetVersion { get; private set; }
11+
public string DotNetCommand { get; private set; }
1112
public string WorkingDirectory { get; private set; }
1213

1314
public TestEnvironmentManager SetupEnvironment(string dotnetVersion, string dotnetCommand = "dotnet")
1415
{
1516
DotNetVersion = dotnetVersion;
16-
DotNetCommand = dotnetCommand; // Store the command to use
17+
DotNetCommand = ResolveDotNetCommand(dotnetCommand); // Resolve to actual executable path, will need to be changed/generalized so this works in jenkins
1718
WorkingDirectory = Path.Combine(Path.GetTempPath(), "NI-Tests", Guid.NewGuid().ToString());
1819

1920
Directory.CreateDirectory(WorkingDirectory);
2021

21-
// Verify dotnet version is available with the specified command
22-
ValidateDotNetVersion(dotnetVersion, dotnetCommand);
22+
// Validate and log .NET version
23+
ValidateAndLogVersions(dotnetVersion, DotNetCommand);
2324

2425
return this;
2526
}
2627

27-
private void ValidateDotNetVersion(string version, string command)
28+
private string ResolveDotNetCommand(string command)
29+
{
30+
// The build machine has aliases for dotnet 3,5 and 6
31+
switch (command)
32+
{
33+
case "dotnet6":
34+
return "~/.dotnet/dotnet".Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
35+
case "dotnet7":
36+
return "~/.dotnet7/dotnet".Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
37+
default:
38+
return command; // Default dotnet (6)
39+
}
40+
}
41+
42+
private void ValidateAndLogVersions(string expectedVersion, string command)
43+
{
44+
Console.WriteLine($"🔍 Validating environment with command: {command}");
45+
46+
// Check .NET version
47+
var dotnetVersionResult = RunCommand(command, "--version");
48+
if (dotnetVersionResult.ExitCode != 0)
49+
{
50+
throw new InvalidOperationException($"Failed to get .NET version using command '{command}': {dotnetVersionResult.Error}");
51+
}
52+
53+
var actualDotNetVersion = dotnetVersionResult.Output.Trim();
54+
Console.WriteLine($"📋 .NET Version: {actualDotNetVersion}");
55+
56+
// Throw exception if the requested version doesn't match what's available
57+
if (!actualDotNetVersion.StartsWith(expectedVersion))
58+
{
59+
Console.WriteLine($"❌ Version mismatch: Expected {expectedVersion}, but got {actualDotNetVersion}");
60+
throw new InvalidOperationException($"Requested .NET version {expectedVersion} is not available. System returned version {actualDotNetVersion}. Please install the required .NET SDK version or update your test.");
61+
}
62+
63+
// Check NuGet version
64+
var nugetVersionResult = RunCommand(command, "nuget --version");
65+
if (nugetVersionResult.ExitCode == 0)
66+
{
67+
NuGetVersion = nugetVersionResult.Output.Trim();
68+
Console.WriteLine($"📦 NuGet Version: {NuGetVersion}");
69+
}
70+
else
71+
{
72+
Console.WriteLine($"⚠️ Could not determine NuGet version: {nugetVersionResult.Error}");
73+
NuGetVersion = "Unknown";
74+
}
75+
76+
Console.WriteLine($"📁 Working Directory: {WorkingDirectory}");
77+
Console.WriteLine("✅ Environment validation complete");
78+
}
79+
80+
private (int ExitCode, string Output, string Error) RunCommand(string command, string arguments)
2881
{
2982
var process = new Process
3083
{
3184
StartInfo = new ProcessStartInfo
3285
{
33-
FileName = command, // Use the specified command
34-
Arguments = "--list-sdks",
86+
FileName = command,
87+
Arguments = arguments,
3588
UseShellExecute = false,
3689
RedirectStandardOutput = true,
90+
RedirectStandardError = true,
3791
CreateNoWindow = true
3892
}
3993
};
4094

4195
process.Start();
4296
string output = process.StandardOutput.ReadToEnd();
97+
string error = process.StandardError.ReadToEnd();
4398
process.WaitForExit();
4499

45-
if (!output.Contains(version))
46-
{
47-
throw new InvalidOperationException($"Required .NET SDK version {version} is not available with command '{command}'.");
48-
}
100+
return (process.ExitCode, output, error);
49101
}
50102

51103
public void Cleanup()
52104
{
53105
if (Directory.Exists(WorkingDirectory))
54106
{
55-
Directory.Delete(WorkingDirectory, recursive: true);
107+
Directory.Delete(WorkingDirectory, true);
108+
Console.WriteLine($"🧹 Cleaned up working directory: {WorkingDirectory}");
56109
}
110+
57111
}
58112
}
59113
}

detect-nuget-inspector/detect-nuget-inspector-tests/shantys-tests/TestProjectBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public TestProjectBuilder CreateSimpleSolution(string solutionName = "TestSoluti
2626
{
2727
StartInfo = new ProcessStartInfo
2828
{
29-
FileName = "dotnet",
29+
FileName = _environment.DotNetCommand, // Use the environment's command
3030
Arguments = $"new sln -n {solutionName}",
3131
WorkingDirectory = _solutionDirectory,
3232
UseShellExecute = false,
@@ -49,7 +49,7 @@ public TestProjectBuilder CreateSimpleSolution(string solutionName = "TestSoluti
4949
{
5050
StartInfo = new ProcessStartInfo
5151
{
52-
FileName = "dotnet",
52+
FileName = _environment.DotNetCommand, // Use the environment's command
5353
Arguments = "new console -n Project1",
5454
WorkingDirectory = _solutionDirectory,
5555
UseShellExecute = false,
@@ -72,7 +72,7 @@ public TestProjectBuilder CreateSimpleSolution(string solutionName = "TestSoluti
7272
{
7373
StartInfo = new ProcessStartInfo
7474
{
75-
FileName = "dotnet",
75+
FileName = _environment.DotNetCommand, // Use the environment's command
7676
Arguments = "sln add Project1/Project1.csproj",
7777
WorkingDirectory = _solutionDirectory,
7878
UseShellExecute = false,

0 commit comments

Comments
 (0)