Skip to content

Commit 06f9a1e

Browse files
committed
Tests for v3/v4/v5 support
1 parent 995b5b5 commit 06f9a1e

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@
4949
</Reference>
5050
<Reference Include="System" />
5151
<Reference Include="System.Core" />
52+
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<SpecificVersion>False</SpecificVersion>
54+
<HintPath>..\..\..\..\..\..\..\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
55+
</Reference>
5256
<Reference Include="System.Xml.Linq" />
5357
<Reference Include="System.Data.DataSetExtensions" />
5458
<Reference Include="Microsoft.CSharp" />
5559
<Reference Include="System.Data" />
5660
<Reference Include="System.Xml" />
57-
<Reference Include="System.Management.Automation" />
5861
</ItemGroup>
5962
<ItemGroup>
6063
<Compile Include="Analysis\AnalysisOutputWriter.cs" />

test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
using Microsoft.PowerShell.EditorServices.Test.Shared.References;
1111
using Microsoft.PowerShell.EditorServices.Test.Shared.SymbolDetails;
1212
using Microsoft.PowerShell.EditorServices.Test.Shared.Symbols;
13+
using Microsoft.Win32;
1314
using System;
15+
using System.Diagnostics;
1416
using System.IO;
1517
using System.Linq;
1618
using System.Management.Automation.Runspaces;
1719
using System.Threading;
1820
using System.Threading.Tasks;
21+
using System.Xml.Linq;
1922
using Xunit;
2023

2124
namespace Microsoft.PowerShell.EditorServices.Test.Language
@@ -25,6 +28,7 @@ public class LanguageServiceTests : IDisposable
2528
private Workspace workspace;
2629
private LanguageService languageService;
2730
private PowerShellContext powerShellContext;
31+
private DirectoryInfo packageDirectory;
2832

2933
public LanguageServiceTests()
3034
{
@@ -37,6 +41,11 @@ public LanguageServiceTests()
3741
public void Dispose()
3842
{
3943
this.powerShellContext.Dispose();
44+
45+
if (packageDirectory != null && packageDirectory.Exists)
46+
{
47+
packageDirectory.Delete(true);
48+
}
4049
}
4150

4251
[Fact]
@@ -289,6 +298,96 @@ public void LanguageServiceFindsSymbolsInNoSymbolsFile()
289298
Assert.Equal(0, symbolsResult.FoundOccurrences.Count());
290299
}
291300

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+
292391
private ScriptFile GetScriptFile(ScriptRegion scriptRegion)
293392
{
294393
const string baseSharedScriptPath =
1.59 MB
Binary file not shown.

test/PowerShellEditorServices.Test/PowerShellEditorServices.Test.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@
104104
<ItemGroup>
105105
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
106106
</ItemGroup>
107-
<ItemGroup />
107+
<ItemGroup>
108+
<Content Include="NuGet.exe">
109+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
110+
</Content>
111+
</ItemGroup>
108112
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
109113
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
110114
<PropertyGroup>

0 commit comments

Comments
 (0)