|
12 | 12 | using System.Text.Json; |
13 | 13 | using System.Threading; |
14 | 14 | using System.Threading.Channels; |
15 | | - |
| 15 | +using Mono.Options; |
16 | 16 | using Xunit; |
17 | 17 | using Xunit.Abstractions; |
18 | 18 |
|
19 | | -int ExitFailure = 1; |
20 | | -int ExitSuccess = 0; |
| 19 | +const int ExitFailure = 1; |
| 20 | +const int ExitSuccess = 0; |
| 21 | + |
| 22 | +string? assemblyFilePath = null; |
| 23 | +string? outputFilePath = null; |
21 | 24 |
|
22 | | -if (args.Length != 1) |
| 25 | +var options = new OptionSet |
23 | 26 | { |
24 | | - return ExitFailure; |
25 | | -} |
| 27 | + { "assembly=", "The assembly file to process.", v => assemblyFilePath = v }, |
| 28 | + { "out=", "The output file name.", v => outputFilePath = v } |
| 29 | +}; |
26 | 30 |
|
27 | 31 | try |
28 | 32 | { |
29 | | - using var pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0]); |
30 | | - using var sr = new StreamReader(pipeClient); |
31 | | - string? output; |
| 33 | + List<string> extra = options.Parse(args); |
32 | 34 |
|
33 | | - // Wait for 'sync message' from the server. |
34 | | - do |
| 35 | + if (assemblyFilePath is null) |
35 | 36 | { |
36 | | - output = await sr.ReadLineAsync().ConfigureAwait(false); |
| 37 | + Console.WriteLine("Must pass an assembly file name."); |
| 38 | + return ExitFailure; |
37 | 39 | } |
38 | | - while (!(output?.StartsWith("ASSEMBLY", StringComparison.OrdinalIgnoreCase) == true)); |
39 | 40 |
|
40 | | - if ((output = await sr.ReadLineAsync().ConfigureAwait(false)) is not null) |
| 41 | + if (extra.Count > 0) |
41 | 42 | { |
42 | | - var assemblyFileName = output; |
| 43 | + Console.WriteLine($"Unknown arguments: {string.Join(" ", extra)}"); |
| 44 | + return ExitFailure; |
| 45 | + } |
| 46 | + |
| 47 | + if (outputFilePath is null) |
| 48 | + { |
| 49 | + outputFilePath = Path.Combine(Path.GetDirectoryName(assemblyFilePath)!, "testlist.json"); |
| 50 | + } |
43 | 51 |
|
44 | | -#if NET6_0_OR_GREATER |
45 | | - var resolver = new System.Runtime.Loader.AssemblyDependencyResolver(assemblyFileName); |
46 | | - System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += (context, assemblyName) => |
| 52 | +#if NET |
| 53 | + var resolver = new System.Runtime.Loader.AssemblyDependencyResolver(assemblyFilePath); |
| 54 | + System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += (context, assemblyName) => |
| 55 | + { |
| 56 | + var assemblyPath = resolver.ResolveAssemblyToPath(assemblyName); |
| 57 | + if (assemblyPath is not null) |
47 | 58 | { |
48 | | - var assemblyPath = resolver.ResolveAssemblyToPath(assemblyName); |
49 | | - if (assemblyPath is not null) |
50 | | - { |
51 | | - return context.LoadFromAssemblyPath(assemblyPath); |
52 | | - } |
| 59 | + return context.LoadFromAssemblyPath(assemblyPath); |
| 60 | + } |
53 | 61 |
|
54 | | - return null; |
55 | | - }; |
| 62 | + return null; |
| 63 | + }; |
56 | 64 | #endif |
57 | 65 |
|
58 | | - string testDescriptor = Path.GetFileName(assemblyFileName); |
| 66 | + string assemblyFileName = Path.GetFileName(assemblyFilePath); |
59 | 67 | #if NET |
60 | | - testDescriptor += " (.NET Core)"; |
| 68 | + string tfm = "(.NET Core)"; |
61 | 69 | #else |
62 | | - testDescriptor += " (.NET Framework)"; |
| 70 | + string tfm = "(.NET Framework)"; |
63 | 71 | #endif |
64 | 72 |
|
65 | | - await Console.Out.WriteLineAsync($"Discovering tests in {testDescriptor}...").ConfigureAwait(false); |
| 73 | + Console.Write($"Discovering tests in {tfm} {assemblyFileName} ... "); |
66 | 74 |
|
67 | | - using var xunit = new XunitFrontController(AppDomainSupport.IfAvailable, assemblyFileName, shadowCopy: false); |
68 | | - var configuration = ConfigReader.Load(assemblyFileName, configFileName: null); |
69 | | - var sink = new Sink(); |
70 | | - xunit.Find(includeSourceInformation: false, |
71 | | - messageSink: sink, |
72 | | - discoveryOptions: TestFrameworkOptions.ForDiscovery(configuration)); |
| 75 | + using var xunit = new XunitFrontController(AppDomainSupport.IfAvailable, assemblyFilePath, shadowCopy: false); |
| 76 | + var configuration = ConfigReader.Load(assemblyFileName, configFileName: null); |
| 77 | + var sink = new Sink(); |
| 78 | + xunit.Find(includeSourceInformation: false, |
| 79 | + messageSink: sink, |
| 80 | + discoveryOptions: TestFrameworkOptions.ForDiscovery(configuration)); |
73 | 81 |
|
74 | | - var testsToWrite = new HashSet<string>(); |
75 | | - await foreach (var fullyQualifiedName in sink.GetTestCaseNamesAsync()) |
76 | | - { |
77 | | - testsToWrite.Add(fullyQualifiedName); |
78 | | - } |
79 | | - |
80 | | - if (sink.AnyWriteFailures) |
81 | | - { |
82 | | - await Console.Error.WriteLineAsync($"Channel failed to write for '{assemblyFileName}'").ConfigureAwait(false); |
83 | | - return ExitFailure; |
84 | | - } |
85 | | - |
86 | | -#if NET6_0_OR_GREATER |
87 | | - await Console.Out.WriteLineAsync($"Discovered {testsToWrite.Count} tests in {testDescriptor}").ConfigureAwait(false); |
88 | | -#else |
89 | | - await Console.Out.WriteLineAsync($"Discovered {testsToWrite.Count} tests in {testDescriptor}").ConfigureAwait(false); |
90 | | -#endif |
| 82 | + var testsToWrite = new HashSet<string>(); |
| 83 | + await foreach (var fullyQualifiedName in sink.GetTestCaseNamesAsync()) |
| 84 | + { |
| 85 | + testsToWrite.Add(fullyQualifiedName); |
| 86 | + } |
91 | 87 |
|
92 | | - var directory = Path.GetDirectoryName(assemblyFileName); |
93 | | - using var fileStream = File.Create(Path.Combine(directory!, "testlist.json")); |
94 | | - await JsonSerializer.SerializeAsync(fileStream, testsToWrite).ConfigureAwait(false); |
95 | | - return ExitSuccess; |
| 88 | + if (sink.AnyWriteFailures) |
| 89 | + { |
| 90 | + Console.WriteLine($"Channel failed to write for '{assemblyFileName}'"); |
| 91 | + return ExitFailure; |
96 | 92 | } |
97 | 93 |
|
| 94 | + Console.WriteLine($"{testsToWrite.Count} found"); |
| 95 | + |
| 96 | + using var fileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); |
| 97 | + await JsonSerializer.SerializeAsync(fileStream, testsToWrite.OrderBy(x => x)).ConfigureAwait(false); |
| 98 | + return ExitSuccess; |
| 99 | +} |
| 100 | +catch (OptionException e) |
| 101 | +{ |
| 102 | + Console.WriteLine(e.Message); |
| 103 | + options.WriteOptionDescriptions(Console.Out); |
98 | 104 | return ExitFailure; |
99 | 105 | } |
100 | 106 | catch (Exception ex) |
101 | 107 | { |
102 | 108 | // Write the exception details to stderr so the host process can pick it up. |
103 | | - await Console.Error.WriteLineAsync(ex.ToString()).ConfigureAwait(false); |
104 | | - return 1; |
| 109 | + Console.WriteLine(ex.ToString()); |
| 110 | + return ExitFailure; |
105 | 111 | } |
106 | 112 |
|
107 | 113 | file class Sink : IMessageSink |
@@ -151,3 +157,4 @@ private void OnTestDiscovered(ITestCaseDiscoveryMessage testCaseDiscovered) |
151 | 157 | } |
152 | 158 | } |
153 | 159 | } |
| 160 | + |
0 commit comments