Skip to content

Commit a37888b

Browse files
committed
Broke tests out into own file. Clean up.
1 parent 906212f commit a37888b

File tree

9 files changed

+105
-114
lines changed

9 files changed

+105
-114
lines changed

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageDispatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public void Stop()
7979
{
8080
// By disposing the thread we cancel all existing work
8181
// and cause the thread to be destroyed.
82-
this.messageLoopThread.Dispose();
82+
if (this.messageLoopThread != null)
83+
this.messageLoopThread.Dispose();
8384
}
8485

8586
public void SetRequestHandler<TParams, TResult>(

src/PowerShellEditorServices.Protocol/Server/ProtocolServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public void Stop()
7070
this.OnStop();
7171

7272
this.serverChannel.Stop();
73-
this.serverExitedTask.SetResult(true);
73+
if (this.serverExitedTask != null)
74+
this.serverExitedTask.SetResult(true);
7475
}
7576
}
7677

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@
5252
</Reference>
5353
<Reference Include="System" />
5454
<Reference Include="System.Core" />
55-
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
56-
<SpecificVersion>False</SpecificVersion>
57-
<HintPath>..\..\..\..\..\..\..\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
58-
</Reference>
55+
<Reference Include="System.Management.Automation"/>
5956
<Reference Include="System.Xml.Linq" />
6057
<Reference Include="System.Data.DataSetExtensions" />
6158
<Reference Include="Microsoft.CSharp" />

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ private void Initialize(Runspace initialRunspace)
129129
// TODO: Should this be configurable?
130130
this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);
131131

132-
var psVersionTable = this.currentRunspace.SessionStateProxy.GetVariable("PSVersionTable") as Hashtable;
133-
if (psVersionTable != null)
134-
{
135-
Version version;
136-
Version.TryParse(psVersionTable["PSVersion"] as string, out version);
137-
PowerShellVersion = version;
138-
}
132+
PowerShellVersion = GetPowerShellVersion();
139133

140134
#if !PowerShellv3
141135
if (PowerShellVersion > new Version(3,0))
@@ -147,6 +141,26 @@ private void Initialize(Runspace initialRunspace)
147141
this.SessionState = PowerShellContextState.Ready;
148142
}
149143

144+
private Version GetPowerShellVersion()
145+
{
146+
try
147+
{
148+
var psVersionTable = this.currentRunspace.SessionStateProxy.GetVariable("PSVersionTable") as Hashtable;
149+
if (psVersionTable != null)
150+
{
151+
var version = psVersionTable["PSVersion"] as Version;
152+
if (version == null) return new Version(5, 0);
153+
return version;
154+
}
155+
}
156+
catch (Exception ex)
157+
{
158+
Logger.Write(LogLevel.Warning, "Failed to look up PowerShell version. Defaulting to version 5. " + ex.Message);
159+
}
160+
161+
return new Version(5, 0);
162+
}
163+
150164
#endregion
151165

152166
#region Public Methods

test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class LanguageServiceTests : IDisposable
2828
private Workspace workspace;
2929
private LanguageService languageService;
3030
private PowerShellContext powerShellContext;
31-
private DirectoryInfo packageDirectory;
31+
3232

3333
public LanguageServiceTests()
3434
{
@@ -41,11 +41,6 @@ public LanguageServiceTests()
4141
public void Dispose()
4242
{
4343
this.powerShellContext.Dispose();
44-
45-
if (packageDirectory != null && packageDirectory.Exists)
46-
{
47-
packageDirectory.Delete(true);
48-
}
4944
}
5045

5146
[Fact]
@@ -298,95 +293,6 @@ public void LanguageServiceFindsSymbolsInNoSymbolsFile()
298293
Assert.Equal(0, symbolsResult.FoundOccurrences.Count());
299294
}
300295

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, version);
323-
}
324-
finally
325-
{
326-
File.Delete(projectVersion);
327-
}
328-
}
329-
330-
private void Compile(string project, string version)
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 /p:DefineConstants=PowerShellv{2}", project, fi.Directory.Parent.Parent.FullName, version);
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-
}
390296

391297
private ScriptFile GetScriptFile(ScriptRegion scriptRegion)
392298
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Xml.Linq;
7+
using Xunit;
8+
9+
namespace Microsoft.PowerShell.EditorServices.Test.Language
10+
{
11+
public class PowerShellVersionTests
12+
{
13+
[Theory]
14+
[InlineData("3")]
15+
[InlineData("4")]
16+
[InlineData("5")]
17+
public void CompilesWithPowerShellVersion(string version)
18+
{
19+
var assemblyPath = string.Format(@"..\..\..\..\packages\Microsoft.PowerShell.{0}.ReferenceAssemblies.1.0.0\lib\net4\System.Management.Automation.dll", version);
20+
var projectPath = @"..\..\..\..\src\PowerShellEditorServices\PowerShellEditorServices.csproj";
21+
FileInfo fi = new FileInfo(projectPath);
22+
var projectVersion = Path.Combine(fi.DirectoryName, version + ".PowerShellEditorServices.csproj");
23+
24+
var doc = XDocument.Load(projectPath);
25+
var references = doc.Root.Descendants().Where(m => m.Name.LocalName == "Reference");
26+
var reference = references.First(m => m.Attribute("Include").Value.StartsWith("System.Management.Automation"));
27+
reference.Add(new XElement("{http://schemas.microsoft.com/developer/msbuild/2003}HintPath", assemblyPath));
28+
29+
doc.Save(projectVersion);
30+
31+
try
32+
{
33+
Compile(projectVersion, version);
34+
}
35+
finally
36+
{
37+
File.Delete(projectVersion);
38+
}
39+
}
40+
41+
private void Compile(string project, string version)
42+
{
43+
string msbuild;
44+
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0"))
45+
{
46+
var root = key.GetValue("MSBuildToolsPath") as string;
47+
msbuild = Path.Combine(root, "MSBuild.exe");
48+
}
49+
50+
FileInfo fi = new FileInfo(project);
51+
var outPath = Path.GetTempPath();
52+
53+
var p = new Process();
54+
p.StartInfo.FileName = msbuild;
55+
p.StartInfo.Arguments = string.Format(@" {0} /p:Configuration=Debug /t:Build /fileLogger /flp1:logfile=errors.txt;errorsonly /p:SolutionDir={1} /p:SolutionName=PowerShellEditorServices /p:DefineConstants=PowerShellv{2} /p:OutDir={3}", project, fi.Directory.Parent.Parent.FullName, version, outPath);
56+
p.StartInfo.UseShellExecute = false;
57+
p.StartInfo.CreateNoWindow = true;
58+
p.Start();
59+
p.WaitForExit(60000);
60+
if (!p.HasExited)
61+
{
62+
p.Kill();
63+
throw new Exception("Compilation didn't complete in 60 seconds.");
64+
}
65+
66+
if (p.ExitCode != 0)
67+
{
68+
var errors = File.ReadAllText("errors.txt");
69+
throw new Exception(errors);
70+
}
71+
}
72+
}
73+
}
-1.59 MB
Binary file not shown.

test/PowerShellEditorServices.Test/PowerShellEditorServices.Test.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
</Reference>
5252
<Reference Include="System" />
5353
<Reference Include="System.Core" />
54+
<Reference Include="System.Management.Automation" />
5455
<Reference Include="System.Xml.Linq" />
5556
<Reference Include="System.Data.DataSetExtensions" />
5657
<Reference Include="Microsoft.CSharp" />
5758
<Reference Include="System.Data" />
5859
<Reference Include="System.Xml" />
59-
<Reference Include="System.Management.Automation" />
6060
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
6161
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
6262
<Private>True</Private>
@@ -79,6 +79,7 @@
7979
<Compile Include="Console\TestConsoleHost.cs" />
8080
<Compile Include="Debugging\DebugServiceTests.cs" />
8181
<Compile Include="Language\LanguageServiceTests.cs" />
82+
<Compile Include="Language\PowerShellVersionTests.cs" />
8283
<Compile Include="Properties\AssemblyInfo.cs" />
8384
<Compile Include="Console\ConsoleServiceTests.cs" />
8485
<Compile Include="Session\ScriptFileTests.cs" />
@@ -104,11 +105,6 @@
104105
<ItemGroup>
105106
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
106107
</ItemGroup>
107-
<ItemGroup>
108-
<Content Include="NuGet.exe">
109-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
110-
</Content>
111-
</ItemGroup>
112108
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
113109
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
114110
<PropertyGroup>

test/PowerShellEditorServices.Test/packages.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.PowerShell.3.ReferenceAssemblies" version="1.0.0" targetFramework="net45" />
4+
<package id="Microsoft.PowerShell.4.ReferenceAssemblies" version="1.0.0" targetFramework="net45" />
5+
<package id="Microsoft.PowerShell.5.ReferenceAssemblies" version="1.0.0" targetFramework="net45" />
36
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net45" />
47
<package id="xunit" version="2.1.0" targetFramework="net45" />
58
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />

0 commit comments

Comments
 (0)