2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . IO ;
5
6
using System . Linq ;
6
- using System . Text . RegularExpressions ;
7
+ using System . Runtime . InteropServices ;
7
8
using System . Threading . Tasks ;
8
9
using Pulumi . Automation . Commands ;
10
+ using Semver ;
9
11
using Xunit ;
10
12
11
13
namespace Pulumi . Automation . Tests
@@ -15,7 +17,7 @@ public class LocalPulumiCmdTests
15
17
[ Fact ]
16
18
public async Task CheckVersionCommand ( )
17
19
{
18
- var localCmd = new LocalPulumiCmd ( ) ;
20
+ var localCmd = await LocalPulumiCmd . CreateAsync ( ) ;
19
21
var extraEnv = new Dictionary < string , string ? > ( ) ;
20
22
var args = new [ ] { "version" } ;
21
23
@@ -57,6 +59,129 @@ private List<string> Lines(string s)
57
59
. Select ( x => x . Trim ( ) )
58
60
. ToList ( ) ;
59
61
}
62
+
63
+ [ Fact ]
64
+ public async Task InstallDefaultRoot ( )
65
+ {
66
+ var requestedVersion = new SemVersion ( 3 , 102 , 0 ) ;
67
+ await LocalPulumiCmd . Install ( new LocalPulumiCmdOptions { Version = requestedVersion } ) ;
68
+ var home = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
69
+ var pulumiBin = Path . Combine ( home , ".pulumi" , "versions" , requestedVersion . ToString ( ) , "bin" , "pulumi" ) ;
70
+ Assert . True ( File . Exists ( pulumiBin ) ) ;
71
+ }
72
+
73
+ [ Fact ]
74
+ public async Task InstallTwice ( )
75
+ {
76
+ var tempDir = Path . Combine ( Path . GetTempPath ( ) , "automation-test-" + Guid . NewGuid ( ) . ToString ( ) ) ;
77
+ Directory . CreateDirectory ( tempDir ) ;
78
+ try
79
+ {
80
+ var requestedVersion = new SemVersion ( 3 , 102 , 0 ) ;
81
+ await LocalPulumiCmd . Install ( new LocalPulumiCmdOptions { Version = requestedVersion , Root = tempDir } ) ;
82
+ var home = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
83
+ var pulumiBin = Path . Combine ( home , ".pulumi" , "versions" , requestedVersion . ToString ( ) , "bin" , "pulumi" ) ;
84
+ FileInfo fi1 = new FileInfo ( pulumiBin ) ;
85
+ var t1 = fi1 . CreationTime ;
86
+ // Install again with the same options
87
+ await LocalPulumiCmd . Install ( new LocalPulumiCmdOptions { Version = requestedVersion , Root = tempDir } ) ;
88
+ FileInfo fi2 = new FileInfo ( pulumiBin ) ;
89
+ var t2 = fi2 . CreationTime ;
90
+ Assert . Equal ( t1 , t2 ) ;
91
+ }
92
+ finally
93
+ {
94
+ Directory . Delete ( tempDir , true ) ;
95
+ }
96
+
97
+ }
98
+
99
+ [ Fact ]
100
+ public async Task VersionCheck ( )
101
+ {
102
+ var dirPath = Path . Combine ( Path . GetTempPath ( ) , "automation-test-" + Guid . NewGuid ( ) . ToString ( ) ) ;
103
+ var dir = Directory . CreateDirectory ( dirPath ) ;
104
+ try
105
+ {
106
+ // Install an old version
107
+ var installed_version = new SemVersion ( 3 , 99 , 0 ) ;
108
+ await LocalPulumiCmd . Install ( new LocalPulumiCmdOptions { Version = installed_version , Root = dirPath } ) ;
109
+
110
+ // Try to create a command with a more recent version
111
+ var requested_version = new SemVersion ( 3 , 102 , 0 ) ;
112
+ await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => LocalPulumiCmd . CreateAsync ( new LocalPulumiCmdOptions
113
+ {
114
+ Version = requested_version ,
115
+ Root = dirPath
116
+ } ) ) ;
117
+
118
+ // Opting out of the version check works
119
+ await LocalPulumiCmd . CreateAsync ( new LocalPulumiCmdOptions
120
+ {
121
+ Version = requested_version ,
122
+ Root = dirPath ,
123
+ SkipVersionCheck = true
124
+ } ) ;
125
+ }
126
+ finally
127
+ {
128
+ dir . Delete ( true ) ;
129
+ }
130
+ }
131
+
132
+ [ Fact ]
133
+ public void PulumiEnvironment ( )
134
+ {
135
+ var env = new Dictionary < string , string ? > {
136
+ { "PATH" , "/usr/bin" }
137
+ } ;
138
+ var newEnv = LocalPulumiCmd . PulumiEnvironment ( env , "pulumi" , false ) ;
139
+ Assert . Equal ( "/usr/bin" , newEnv [ "PATH" ] ) ;
140
+
141
+ env = new Dictionary < string , string ? > {
142
+ { "PATH" , "/usr/bin" }
143
+ } ;
144
+ newEnv = LocalPulumiCmd . PulumiEnvironment ( env , "/some/install/root/bin/pulumi" , false ) ;
145
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
146
+ {
147
+ Assert . Equal ( "/some/install/root/bin;/usr/bin" , newEnv [ "PATH" ] ) ;
148
+ }
149
+ else
150
+ {
151
+ Assert . Equal ( "/some/install/root/bin:/usr/bin" , newEnv [ "PATH" ] ) ;
152
+ }
153
+ }
154
+
155
+ [ Theory ]
156
+ [ InlineData ( "100.0.0" , true , false ) ]
157
+ [ InlineData ( "1.0.0" , true , false ) ]
158
+ [ InlineData ( "2.22.0" , false , false ) ]
159
+ [ InlineData ( "2.1.0" , true , false ) ]
160
+ [ InlineData ( "2.21.2" , false , false ) ]
161
+ [ InlineData ( "2.21.1" , false , false ) ]
162
+ [ InlineData ( "2.21.0" , true , false ) ]
163
+ // Note that prerelease < release so this case should error
164
+ [ InlineData ( "2.21.1-alpha.1234" , true , false ) ]
165
+ [ InlineData ( "2.20.0" , false , true ) ]
166
+ [ InlineData ( "2.22.0" , false , true ) ]
167
+ // Invalid version check
168
+ [ InlineData ( "invalid" , false , true ) ]
169
+ [ InlineData ( "invalid" , true , false ) ]
170
+ public void ValidVersionTheory ( string currentVersion , bool errorExpected , bool optOut )
171
+ {
172
+ var testMinVersion = new SemVersion ( 2 , 21 , 1 ) ;
173
+
174
+ if ( errorExpected )
175
+ {
176
+ void ValidatePulumiVersion ( ) => LocalPulumiCmd . ParseAndValidatePulumiVersion ( testMinVersion , currentVersion , optOut ) ;
177
+ Assert . Throws < InvalidOperationException > ( ValidatePulumiVersion ) ;
178
+ }
179
+ else
180
+ {
181
+ LocalPulumiCmd . ParseAndValidatePulumiVersion ( testMinVersion , currentVersion , optOut ) ;
182
+ }
183
+ }
184
+
60
185
}
61
186
62
187
}
0 commit comments