Skip to content

Commit 642ee28

Browse files
committed
Fixes xunit/xunit#599: VS Runner: UWP/Store apps don't load config
1 parent b34aeaa commit 642ee28

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

common/WinRTFile.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
using Windows.ApplicationModel;
2+
using Windows.Storage;
23

34
namespace System.IO
45
{
5-
internal static class File
6+
static class File
67
{
78
public static bool Exists(string path)
9+
{
10+
return GetStorageFile(path) != null;
11+
}
12+
13+
public static Stream OpenRead(string path)
14+
{
15+
var storageFile = GetStorageFile(path);
16+
if (storageFile == null)
17+
throw new FileNotFoundException("Could not open file for read", path);
18+
19+
return storageFile.OpenStreamForReadAsync().GetAwaiter().GetResult();
20+
}
21+
22+
// Helpers
23+
24+
static StorageFile GetStorageFile(string path)
825
{
926
if (string.IsNullOrWhiteSpace(path))
10-
return false;
27+
return null;
1128

1229
var folder = Package.Current.InstalledLocation;
1330

14-
if (!path.Contains(folder.Path))
15-
return false;
31+
if (Path.GetDirectoryName(path) != string.Empty && !path.Contains(folder.Path))
32+
return null;
1633

1734
var fileName = Path.GetFileName(path);
18-
var fileAsync = folder.GetFileAsync(fileName);
1935

2036
try
2137
{
22-
fileAsync.AsTask().Wait();
23-
return fileAsync.GetResults() != null;
38+
var fileAsync = folder.GetFileAsync(fileName);
39+
return fileAsync.AsTask().GetAwaiter().GetResult();
40+
}
41+
catch
42+
{
43+
// any errors, we juts can't get it
2444
}
25-
catch { }
2645

27-
return false;
46+
return null;
2847
}
2948
}
3049
}

xunit.runner.visualstudio.testadapter/VsTestRunner.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,15 @@ void ITestExecutor.RunTests(IEnumerable<TestCase> tests, IRunContext runContext,
110110
RunTests(
111111
runContext, frameworkHandle, logger,
112112
() => tests.GroupBy(testCase => testCase.Source)
113-
.Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = ConfigReader.Load(group.Key), TestCases = group.ToList() })
113+
.Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = LoadConfiguration(group.Key), TestCases = group.ToList() })
114114
.ToList()
115115
);
116116
}
117117

118118
// Helpers
119119

120120
static bool ContainsAppX(IEnumerable<string> sources)
121-
{
122-
return sources.Any(s => string.Compare(Path.GetExtension(s), ".appx", StringComparison.OrdinalIgnoreCase) == 0);
123-
}
121+
=> sources.Any(s => string.Compare(Path.GetExtension(s), ".appx", StringComparison.OrdinalIgnoreCase) == 0);
124122

125123
static ITestCase Deserialize(LoggerHelper logger, ITestFrameworkExecutor executor, TestCase testCase)
126124
{
@@ -152,7 +150,7 @@ void DiscoverTests<TVisitor>(IEnumerable<string> sources,
152150
foreach (var assemblyFileName in sources)
153151
{
154152
var assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFileName };
155-
var configuration = ConfigReader.Load(assemblyFileName);
153+
var configuration = LoadConfiguration(assemblyFileName);
156154
var fileName = Path.GetFileNameWithoutExtension(assemblyFileName);
157155
var shadowCopy = configuration.ShadowCopyOrDefault;
158156

@@ -227,11 +225,31 @@ void DiscoverTests<TVisitor>(IEnumerable<string> sources,
227225
}
228226
}
229227

230-
static TestProperty GetTestProperty()
228+
static Stream GetConfigurationStreamForAssembly(string assemblyName)
231229
{
232-
return TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));
230+
// See if there's a directory with the assm name. this might be the case for appx
231+
if (Directory.Exists(assemblyName))
232+
{
233+
if (File.Exists(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json")))
234+
return File.OpenRead(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json"));
235+
236+
if (File.Exists(Path.Combine(assemblyName, "xunit.runner.json")))
237+
return File.OpenRead(Path.Combine(assemblyName, "xunit.runner.json"));
238+
}
239+
240+
// Fallback to working dir
241+
if (File.Exists($"{assemblyName}.xunit.runner.json"))
242+
return File.OpenRead($"{assemblyName}.xunit.runner.json");
243+
244+
if (File.Exists("xunit.runner.json"))
245+
return File.OpenRead("xunit.runner.json");
246+
247+
return null;
233248
}
234249

250+
static TestProperty GetTestProperty()
251+
=> TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));
252+
235253
List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger, IRunContext runContext)
236254
{
237255
// For store apps, the files are copied to the AppX dir, we need to load it from there
@@ -264,7 +282,7 @@ List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger,
264282
var runInfo = new AssemblyRunInfo
265283
{
266284
AssemblyFileName = source,
267-
Configuration = ConfigReader.Load(source),
285+
Configuration = LoadConfiguration(source),
268286
TestCases = testCases
269287
};
270288
result.Add(runInfo);
@@ -285,6 +303,16 @@ static bool IsXunitTestAssembly(string assemblyFileName)
285303
|| Directory.GetFiles(assemblyFolder, "xunit.execution.*.dll").Length > 0;
286304
}
287305

306+
static TestAssemblyConfiguration LoadConfiguration(string assemblyName)
307+
{
308+
#if PLATFORM_DOTNET
309+
var stream = GetConfigurationStreamForAssembly(assemblyName);
310+
return stream == null ? new TestAssemblyConfiguration() : ConfigReader.Load(stream);
311+
#else
312+
return ConfigReader.Load(assemblyName);
313+
#endif
314+
}
315+
288316
void RunTests(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHelper logger, Func<List<AssemblyRunInfo>> testCaseAccessor)
289317
{
290318
Guard.ArgumentNotNull("runContext", runContext);

0 commit comments

Comments
 (0)