|
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.IO; |
| 6 | +using System.Linq; |
6 | 7 |
|
7 | 8 | namespace Microsoft.NodejsTools.TestFrameworks |
8 | 9 | { |
9 | 10 | internal class TestFrameworkDirectories |
10 | 11 | { |
11 | 12 | public const string ExportRunnerFramework = "ExportRunner"; |
12 | 13 | private const string TestFrameworksDirectory = "TestFrameworks"; |
| 14 | + private const string TestAdapterDirectory = "TestAdapter"; |
13 | 15 |
|
14 | | - private readonly Dictionary<string, string> _frameworkDirectories; |
| 16 | + private readonly Dictionary<string, string> frameworkDirectories; |
15 | 17 |
|
16 | 18 | public TestFrameworkDirectories() |
17 | 19 | { |
18 | | - this._frameworkDirectories = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
19 | | - foreach (var directory in Directory.GetDirectories(GetBaseTestframeworkFolder())) |
| 20 | + this.frameworkDirectories = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 21 | + |
| 22 | + var testFrameworkRoot = GetTestframeworkFolderRoot(); |
| 23 | + if(!Directory.Exists(testFrameworkRoot)) |
| 24 | + { |
| 25 | + throw new InvalidOperationException("Unable to find test framework folder"); |
| 26 | + } |
| 27 | + |
| 28 | + foreach (var directory in Directory.GetDirectories(testFrameworkRoot)) |
20 | 29 | { |
21 | 30 | var name = Path.GetFileName(directory); |
22 | | - this._frameworkDirectories.Add(name, directory); |
| 31 | + this.frameworkDirectories.Add(name, directory); |
23 | 32 | } |
24 | | - string defaultFx; |
25 | | - this._frameworkDirectories.TryGetValue(ExportRunnerFramework, out defaultFx); |
26 | | - if (defaultFx == null) |
| 33 | + |
| 34 | + if (!this.frameworkDirectories.TryGetValue(ExportRunnerFramework, out var defaultFx) || string.IsNullOrEmpty(defaultFx)) |
27 | 35 | { |
28 | 36 | throw new InvalidOperationException("Missing generic test framework"); |
29 | 37 | } |
30 | 38 | } |
31 | 39 |
|
32 | | - public List<string> GetFrameworkNames() |
33 | | - { |
34 | | - return new List<string>(this._frameworkDirectories.Keys); |
35 | | - } |
| 40 | + public List<string> GetFrameworkNames() => this.frameworkDirectories.Keys.ToList(); |
36 | 41 |
|
37 | | - public List<string> GetFrameworkDirectories() |
38 | | - { |
39 | | - return new List<string>(this._frameworkDirectories.Values); |
40 | | - } |
| 42 | + public List<string> GetFrameworkDirectories() => this.frameworkDirectories.Values.ToList(); |
41 | 43 |
|
42 | | - private static string GetBaseTestframeworkFolder() |
| 44 | + private static string GetTestframeworkFolderRoot() |
43 | 45 | { |
44 | | - var installFolder = GetExecutingAssemblyPath(); |
45 | | - var baseDirectory = Path.Combine(installFolder, TestFrameworksDirectory); |
| 46 | + // This class is used in 2 different assemblies, installed in 2 locations: |
| 47 | + // |
| 48 | + // "C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\Extensions\Microsoft\NodeJsTools\NodeJsTools\Microsoft.NodejsTools.dll" |
| 49 | + // and |
| 50 | + // "C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter\Microsoft.NodejsTools.TestAdapter.dll" |
| 51 | + // |
| 52 | + // However in both cases, we should just go up a folder to the nodejstools root, and then into the TestAdapter folder. |
| 53 | + |
| 54 | + var currentAssembly = typeof(TestFrameworkDirectories).Assembly; |
| 55 | + var currentAssemblyFolder = Path.GetDirectoryName(currentAssembly.Location); |
| 56 | + var nodejsRootFolder = Path.GetDirectoryName(currentAssemblyFolder); |
| 57 | + |
| 58 | + var baseDirectory = Path.Combine(nodejsRootFolder, TestAdapterDirectory, TestFrameworksDirectory); |
46 | 59 | #if DEBUG |
47 | 60 | // To allow easier debugging of the test adapter, try to use the local directory as a fallback. |
48 | 61 | baseDirectory = Directory.Exists(baseDirectory) ? baseDirectory : Path.Combine(Directory.GetCurrentDirectory(), TestFrameworksDirectory); |
49 | 62 | #endif |
50 | 63 | return baseDirectory; |
51 | 64 | } |
52 | | - |
53 | | - private static string GetExecutingAssemblyPath() |
54 | | - { |
55 | | - var codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; |
56 | | - var uri = new UriBuilder(codeBase); |
57 | | - var path = Uri.UnescapeDataString(uri.Path); |
58 | | - return Path.GetDirectoryName(path); |
59 | | - } |
60 | 65 | } |
61 | 66 | } |
0 commit comments