1
1
using System ;
2
- using System . IO ;
3
2
using System . Diagnostics ;
3
+ using System . IO ;
4
4
5
5
namespace DetectNugetInspectorTests . ShantysTests
6
6
{
7
7
public class TestEnvironmentManager
8
8
{
9
9
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 ; }
11
12
public string WorkingDirectory { get ; private set ; }
12
13
13
14
public TestEnvironmentManager SetupEnvironment ( string dotnetVersion , string dotnetCommand = "dotnet" )
14
15
{
15
16
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
17
18
WorkingDirectory = Path . Combine ( Path . GetTempPath ( ) , "NI-Tests" , Guid . NewGuid ( ) . ToString ( ) ) ;
18
19
19
20
Directory . CreateDirectory ( WorkingDirectory ) ;
20
21
21
- // Verify dotnet version is available with the specified command
22
- ValidateDotNetVersion ( dotnetVersion , dotnetCommand ) ;
22
+ // Validate and log .NET version
23
+ ValidateAndLogVersions ( dotnetVersion , DotNetCommand ) ;
23
24
24
25
return this ;
25
26
}
26
27
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 )
28
81
{
29
82
var process = new Process
30
83
{
31
84
StartInfo = new ProcessStartInfo
32
85
{
33
- FileName = command , // Use the specified command
34
- Arguments = "--list-sdks" ,
86
+ FileName = command ,
87
+ Arguments = arguments ,
35
88
UseShellExecute = false ,
36
89
RedirectStandardOutput = true ,
90
+ RedirectStandardError = true ,
37
91
CreateNoWindow = true
38
92
}
39
93
} ;
40
94
41
95
process . Start ( ) ;
42
96
string output = process . StandardOutput . ReadToEnd ( ) ;
97
+ string error = process . StandardError . ReadToEnd ( ) ;
43
98
process . WaitForExit ( ) ;
44
99
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 ) ;
49
101
}
50
102
51
103
public void Cleanup ( )
52
104
{
53
105
if ( Directory . Exists ( WorkingDirectory ) )
54
106
{
55
- Directory . Delete ( WorkingDirectory , recursive : true ) ;
107
+ Directory . Delete ( WorkingDirectory , true ) ;
108
+ Console . WriteLine ( $ "🧹 Cleaned up working directory: { WorkingDirectory } ") ;
56
109
}
110
+
57
111
}
58
112
}
59
113
}
0 commit comments