diff --git a/common/WinRTFile.cs b/common/WinRTFile.cs index b24eac0f..18cd0ad5 100644 --- a/common/WinRTFile.cs +++ b/common/WinRTFile.cs @@ -1,30 +1,49 @@ using Windows.ApplicationModel; +using Windows.Storage; namespace System.IO { - internal static class File + static class File { public static bool Exists(string path) + { + return GetStorageFile(path) != null; + } + + public static Stream OpenRead(string path) + { + var storageFile = GetStorageFile(path); + if (storageFile == null) + throw new FileNotFoundException("Could not open file for read", path); + + return storageFile.OpenStreamForReadAsync().GetAwaiter().GetResult(); + } + + // Helpers + + static StorageFile GetStorageFile(string path) { if (string.IsNullOrWhiteSpace(path)) - return false; + return null; var folder = Package.Current.InstalledLocation; - if (!path.Contains(folder.Path)) - return false; + if (Path.GetDirectoryName(path) != string.Empty && !path.Contains(folder.Path)) + return null; var fileName = Path.GetFileName(path); - var fileAsync = folder.GetFileAsync(fileName); try { - fileAsync.AsTask().Wait(); - return fileAsync.GetResults() != null; + var fileAsync = folder.GetFileAsync(fileName); + return fileAsync.AsTask().GetAwaiter().GetResult(); + } + catch + { + // any errors, we juts can't get it } - catch { } - return false; + return null; } } } diff --git a/xunit.runner.visualstudio.testadapter/VsTestRunner.cs b/xunit.runner.visualstudio.testadapter/VsTestRunner.cs index 313d9a13..4163f213 100644 --- a/xunit.runner.visualstudio.testadapter/VsTestRunner.cs +++ b/xunit.runner.visualstudio.testadapter/VsTestRunner.cs @@ -110,7 +110,7 @@ void ITestExecutor.RunTests(IEnumerable tests, IRunContext runContext, RunTests( runContext, frameworkHandle, logger, () => tests.GroupBy(testCase => testCase.Source) - .Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = ConfigReader.Load(group.Key), TestCases = group.ToList() }) + .Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = LoadConfiguration(group.Key), TestCases = group.ToList() }) .ToList() ); } @@ -118,9 +118,7 @@ void ITestExecutor.RunTests(IEnumerable tests, IRunContext runContext, // Helpers static bool ContainsAppX(IEnumerable sources) - { - return sources.Any(s => string.Compare(Path.GetExtension(s), ".appx", StringComparison.OrdinalIgnoreCase) == 0); - } + => sources.Any(s => string.Compare(Path.GetExtension(s), ".appx", StringComparison.OrdinalIgnoreCase) == 0); static ITestCase Deserialize(LoggerHelper logger, ITestFrameworkExecutor executor, TestCase testCase) { @@ -152,7 +150,7 @@ void DiscoverTests(IEnumerable sources, foreach (var assemblyFileName in sources) { var assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFileName }; - var configuration = ConfigReader.Load(assemblyFileName); + var configuration = LoadConfiguration(assemblyFileName); var fileName = Path.GetFileNameWithoutExtension(assemblyFileName); var shadowCopy = configuration.ShadowCopyOrDefault; @@ -227,11 +225,31 @@ void DiscoverTests(IEnumerable sources, } } - static TestProperty GetTestProperty() + static Stream GetConfigurationStreamForAssembly(string assemblyName) { - return TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner)); + // See if there's a directory with the assm name. this might be the case for appx + if (Directory.Exists(assemblyName)) + { + if (File.Exists(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json"))) + return File.OpenRead(Path.Combine(assemblyName, $"{assemblyName}.xunit.runner.json")); + + if (File.Exists(Path.Combine(assemblyName, "xunit.runner.json"))) + return File.OpenRead(Path.Combine(assemblyName, "xunit.runner.json")); + } + + // Fallback to working dir + if (File.Exists($"{assemblyName}.xunit.runner.json")) + return File.OpenRead($"{assemblyName}.xunit.runner.json"); + + if (File.Exists("xunit.runner.json")) + return File.OpenRead("xunit.runner.json"); + + return null; } + static TestProperty GetTestProperty() + => TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner)); + List GetTests(IEnumerable sources, LoggerHelper logger, IRunContext runContext) { // For store apps, the files are copied to the AppX dir, we need to load it from there @@ -264,7 +282,7 @@ List GetTests(IEnumerable sources, LoggerHelper logger, var runInfo = new AssemblyRunInfo { AssemblyFileName = source, - Configuration = ConfigReader.Load(source), + Configuration = LoadConfiguration(source), TestCases = testCases }; result.Add(runInfo); @@ -285,6 +303,16 @@ static bool IsXunitTestAssembly(string assemblyFileName) || Directory.GetFiles(assemblyFolder, "xunit.execution.*.dll").Length > 0; } + static TestAssemblyConfiguration LoadConfiguration(string assemblyName) + { +#if PLATFORM_DOTNET + var stream = GetConfigurationStreamForAssembly(assemblyName); + return stream == null ? new TestAssemblyConfiguration() : ConfigReader.Load(stream); +#else + return ConfigReader.Load(assemblyName); +#endif + } + void RunTests(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHelper logger, Func> testCaseAccessor) { Guard.ArgumentNotNull("runContext", runContext);