Skip to content

Commit

Permalink
Fixes xunit/xunit#599: VS Runner: UWP/Store apps don't load config
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Sep 27, 2015
1 parent b34aeaa commit 642ee28
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
37 changes: 28 additions & 9 deletions common/WinRTFile.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
44 changes: 36 additions & 8 deletions xunit.runner.visualstudio.testadapter/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,15 @@ void ITestExecutor.RunTests(IEnumerable<TestCase> 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()
);
}

// Helpers

static bool ContainsAppX(IEnumerable<string> 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)
{
Expand Down Expand Up @@ -152,7 +150,7 @@ void DiscoverTests<TVisitor>(IEnumerable<string> 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;

Expand Down Expand Up @@ -227,11 +225,31 @@ void DiscoverTests<TVisitor>(IEnumerable<string> 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<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger, IRunContext runContext)
{
// For store apps, the files are copied to the AppX dir, we need to load it from there
Expand Down Expand Up @@ -264,7 +282,7 @@ List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger,
var runInfo = new AssemblyRunInfo
{
AssemblyFileName = source,
Configuration = ConfigReader.Load(source),
Configuration = LoadConfiguration(source),
TestCases = testCases
};
result.Add(runInfo);
Expand All @@ -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<List<AssemblyRunInfo>> testCaseAccessor)
{
Guard.ArgumentNotNull("runContext", runContext);
Expand Down

0 comments on commit 642ee28

Please # to comment.