-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.NET] Updating ApiView to handle input/output files (#5310)
- Loading branch information
Showing
6 changed files
with
149 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
using System; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ApiView | ||
{ | ||
class Program | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
async static Task Main(string[] args) | ||
{ | ||
try | ||
{ | ||
var assemblySymbol = CompilationFactory.GetCompilation(args[0]); | ||
var renderer = new CodeFileRenderer(); | ||
var codeNode = new CodeFileBuilder().Build(assemblySymbol, false, null); | ||
Console.WriteLine(renderer.Render(codeNode)); | ||
await RunAsync(args); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e.ToString()); | ||
} | ||
} | ||
|
||
public async static Task RunAsync(string[] args) | ||
{ | ||
if (args.Length != 2) | ||
{ | ||
throw new ArgumentException("usage: ApiView [input-path] [output-path]", nameof(args)); | ||
} | ||
|
||
var assemblySymbol = CompilationFactory.GetCompilation(args[0]); | ||
var codeNode = new CodeFileBuilder().Build(assemblySymbol, false, null); | ||
|
||
using var fileStream = new FileStream(args[1], FileMode.Create, FileAccess.Write); | ||
await codeNode.SerializeAsync(fileStream); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
|
||
namespace APIViewUnitTests | ||
{ | ||
internal static class Common | ||
{ | ||
public static async Task BuildDllAsync(Stream stream, string code) | ||
{ | ||
var project = DiagnosticProject.Create(typeof(CodeFileBuilderTests).Assembly, LanguageVersion.Latest, new[] { code }) | ||
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
||
var compilation = await project.GetCompilationAsync(); | ||
Assert.Empty(compilation.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Warning)); | ||
|
||
compilation.Emit(stream); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ApiView; | ||
using Microsoft.TeamFoundation.Build.WebApi; | ||
using Xunit; | ||
|
||
namespace APIViewUnitTests | ||
{ | ||
public class ProgramTests | ||
{ | ||
public static IEnumerable<object[]> ExactFormattingFiles | ||
{ | ||
get | ||
{ | ||
var assembly = typeof(CodeFileBuilderTests).Assembly; | ||
return assembly.GetManifestResourceNames() | ||
.Where(r => r.Contains("ExactFormatting")) | ||
.Select(r => new object[] { r }) | ||
.ToArray(); | ||
} | ||
} | ||
|
||
private string InputPath => Path.Combine(Path.GetTempPath(), "input.dll"); | ||
|
||
private string OutputPath => Path.Combine(Path.GetTempPath(), "output.json"); | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(3)] | ||
public void RunAsyncThrowsWithIncorrectNumberOfArgs(int length) | ||
{ | ||
var args = new string[length]; | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
args[i] = InputPath; | ||
} | ||
|
||
Assert.ThrowsAsync<ArgumentException>(async () => await Program.RunAsync(args)); | ||
} | ||
|
||
[Fact] | ||
public void RunAsyncThrowsWithMissingInputFile() | ||
{ | ||
var args = new string[] | ||
{ | ||
"missing_file.dll", | ||
OutputPath | ||
}; | ||
|
||
Assert.ThrowsAsync<FileNotFoundException>(async () => await Program.RunAsync(args)); | ||
} | ||
|
||
[Fact] | ||
public void RunAsyncThrowsWithMissingOutputFolder() | ||
{ | ||
var args = new string[] | ||
{ | ||
InputPath, | ||
Path.Combine("missing_folder", "output.json") | ||
}; | ||
|
||
Assert.ThrowsAsync<FolderNotFoundException>(async () => await Program.RunAsync(args)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ExactFormattingFiles))] | ||
public async Task RunAsyncGeneratesOutputFile(string name) | ||
{ | ||
var manifestResourceStream = typeof(CodeFileBuilderTests).Assembly.GetManifestResourceStream(name); | ||
using var streamReader = new StreamReader(manifestResourceStream); | ||
var code = streamReader.ReadToEnd(); | ||
|
||
using (var fileStream = new FileStream(InputPath, FileMode.Create, FileAccess.Write)) | ||
{ | ||
await Common.BuildDllAsync(fileStream, code); | ||
} | ||
|
||
var args = new string[] { InputPath, OutputPath }; | ||
|
||
await Program.RunAsync(args); | ||
|
||
var output = await File.ReadAllTextAsync(OutputPath); | ||
|
||
var assemblySymbol = CompilationFactory.GetCompilation(InputPath); | ||
var codeNode = new CodeFileBuilder().Build(assemblySymbol, false, null); | ||
using var stream = new MemoryStream(); | ||
|
||
await codeNode.SerializeAsync(stream); | ||
|
||
var expectedOutput = Encoding.UTF8.GetString(stream.ToArray()); | ||
|
||
Assert.Equal(expectedOutput, output); | ||
} | ||
} | ||
} |