10
10
using Microsoft . PowerShell . EditorServices . Test . Shared . References ;
11
11
using Microsoft . PowerShell . EditorServices . Test . Shared . SymbolDetails ;
12
12
using Microsoft . PowerShell . EditorServices . Test . Shared . Symbols ;
13
+ using Microsoft . Win32 ;
13
14
using System ;
15
+ using System . Diagnostics ;
14
16
using System . IO ;
15
17
using System . Linq ;
16
18
using System . Management . Automation . Runspaces ;
17
19
using System . Threading ;
18
20
using System . Threading . Tasks ;
21
+ using System . Xml . Linq ;
19
22
using Xunit ;
20
23
21
24
namespace Microsoft . PowerShell . EditorServices . Test . Language
@@ -25,6 +28,7 @@ public class LanguageServiceTests : IDisposable
25
28
private Workspace workspace ;
26
29
private LanguageService languageService ;
27
30
private PowerShellContext powerShellContext ;
31
+ private DirectoryInfo packageDirectory ;
28
32
29
33
public LanguageServiceTests ( )
30
34
{
@@ -37,6 +41,11 @@ public LanguageServiceTests()
37
41
public void Dispose ( )
38
42
{
39
43
this . powerShellContext . Dispose ( ) ;
44
+
45
+ if ( packageDirectory != null && packageDirectory . Exists )
46
+ {
47
+ packageDirectory . Delete ( true ) ;
48
+ }
40
49
}
41
50
42
51
[ Fact ]
@@ -289,6 +298,96 @@ public void LanguageServiceFindsSymbolsInNoSymbolsFile()
289
298
Assert . Equal ( 0 , symbolsResult . FoundOccurrences . Count ( ) ) ;
290
299
}
291
300
301
+ [ Theory ]
302
+ [ InlineData ( "3" ) ]
303
+ [ InlineData ( "4" ) ]
304
+ [ InlineData ( "5" ) ]
305
+ public void CompilesWithPowerShellVersion ( string version )
306
+ {
307
+ var assemblyPath = InstallPackage ( string . Format ( "Microsoft.PowerShell.{0}.ReferenceAssemblies" , version ) , "1.0.0" ) ;
308
+ var projectPath = @"..\..\..\..\src\PowerShellEditorServices\PowerShellEditorServices.csproj" ;
309
+ FileInfo fi = new FileInfo ( projectPath ) ;
310
+ var projectVersion = Path . Combine ( fi . DirectoryName , version + ".PowerShellEditorServices.csproj" ) ;
311
+
312
+ var doc = XDocument . Load ( projectPath ) ;
313
+ var references = doc . Root . Descendants ( ) . Where ( m => m . Name . LocalName == "Reference" ) ;
314
+ var reference = references . First ( m => m . Attribute ( "Include" ) . Value . StartsWith ( "System.Management.Automation" ) ) ;
315
+ var hintPath = reference . Descendants ( ) . First ( m => m . Name . LocalName == "HintPath" ) ;
316
+ hintPath . Value = assemblyPath ;
317
+
318
+ doc . Save ( projectVersion ) ;
319
+
320
+ try
321
+ {
322
+ Compile ( projectVersion ) ;
323
+ }
324
+ finally
325
+ {
326
+ File . Delete ( projectVersion ) ;
327
+ }
328
+ }
329
+
330
+ private void Compile ( string project )
331
+ {
332
+ string msbuild ;
333
+ using ( var key = Registry . LocalMachine . OpenSubKey ( @"SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" ) )
334
+ {
335
+ var root = key . GetValue ( "MSBuildToolsPath" ) as string ;
336
+ msbuild = Path . Combine ( root , "MSBuild.exe" ) ;
337
+ }
338
+
339
+ FileInfo fi = new FileInfo ( project ) ;
340
+
341
+ var p = new Process ( ) ;
342
+ p . StartInfo . FileName = msbuild ;
343
+ p . StartInfo . Arguments = string . Format ( @" {0} /p:Configuration=Debug /t:Build /fileLogger /flp1:logfile=errors.txt;errorsonly /p:SolutionDir={1} /p:SolutionName=PowerShellEditorServices" , project , fi . Directory . Parent . Parent . FullName ) ;
344
+ p . StartInfo . UseShellExecute = false ;
345
+ p . StartInfo . CreateNoWindow = true ;
346
+ p . Start ( ) ;
347
+ p . WaitForExit ( 60000 ) ;
348
+ if ( ! p . HasExited )
349
+ {
350
+ p . Kill ( ) ;
351
+ throw new Exception ( "Compilation didn't complete in 60 seconds." ) ;
352
+ }
353
+
354
+ if ( p . ExitCode != 0 )
355
+ {
356
+ var errors = File . ReadAllText ( "errors.txt" ) ;
357
+ throw new Exception ( errors ) ;
358
+ }
359
+ }
360
+
361
+ public string InstallPackage ( string packageName , string packageVersion )
362
+ {
363
+ var packageDir = Path . Combine ( Path . GetTempPath ( ) , "PowerShellPackages" ) ;
364
+ packageDirectory = new DirectoryInfo ( packageDir ) ;
365
+ packageDirectory . Create ( ) ;
366
+
367
+ var nuget = Path . Combine ( Environment . CurrentDirectory , "NuGet.exe" ) ;
368
+ ProcessStartInfo si = new ProcessStartInfo ( ) ;
369
+
370
+ var p = new Process ( ) ;
371
+ p . StartInfo . FileName = nuget ;
372
+ p . StartInfo . Arguments = string . Format ( "install {0} -o {1} -Version {2}" , packageName , packageDir , packageVersion ) ;
373
+ p . StartInfo . RedirectStandardOutput = true ;
374
+ p . StartInfo . RedirectStandardError = true ;
375
+ p . StartInfo . UseShellExecute = false ;
376
+ p . StartInfo . CreateNoWindow = true ;
377
+ p . Start ( ) ;
378
+ p . WaitForExit ( 10000 ) ;
379
+ if ( ! p . HasExited )
380
+ {
381
+ p . Kill ( ) ;
382
+ throw new Exception ( "Failed to download PowerShell NuGet packages required for this test." ) ;
383
+ }
384
+
385
+ var packageFolder = packageName + "." + packageVersion ;
386
+
387
+ var assemblyPath = Path . Combine ( packageDir , packageFolder ) ;
388
+ return Path . Combine ( assemblyPath , @"lib\net4\System.Management.Automation.dll" ) ;
389
+ }
390
+
292
391
private ScriptFile GetScriptFile ( ScriptRegion scriptRegion )
293
392
{
294
393
const string baseSharedScriptPath =
0 commit comments