Skip to content

Commit

Permalink
[.NET] Updating ApiView to handle input/output files (#5310)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinelski authored Mar 22, 2023
1 parent 1691d40 commit 89f2b2d
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 26 deletions.
27 changes: 20 additions & 7 deletions src/dotnet/APIView/APIView/Program.cs
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);
}
}
}
16 changes: 5 additions & 11 deletions src/dotnet/APIView/APIViewUnitTests/CodeFileBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using ApiView;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ApiView;
using Microsoft.CodeAnalysis;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -51,14 +50,9 @@ public async Task VerifyFormatted(string name)

private async Task AssertFormattingAsync(string code, string formatted)
{
var project = DiagnosticProject.Create(typeof(CodeFileBuilderTests).Assembly, LanguageVersion.Latest, new[] { code });
project = project.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

var compilation = await project.GetCompilationAsync();
Assert.Empty(compilation.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Warning));
using var memoryStream = new MemoryStream();

var memoryStream = new MemoryStream();
compilation.Emit(memoryStream);
await Common.BuildDllAsync(memoryStream, code);
memoryStream.Position = 0;

var compilationFromDll = CompilationFactory.GetCompilation(memoryStream, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ApiView;
using APIViewWeb.Models;
using Xunit;
Expand Down
7 changes: 0 additions & 7 deletions src/dotnet/APIView/APIViewUnitTests/CodeFileTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using ApiView;
using APIView;
using APIView.Model;
using Xunit;

namespace APIViewUnitTests
Expand Down
23 changes: 23 additions & 0 deletions src/dotnet/APIView/APIViewUnitTests/Common.cs
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);
}
}
}
101 changes: 101 additions & 0 deletions src/dotnet/APIView/APIViewUnitTests/ProgramTests.cs
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);
}
}
}

0 comments on commit 89f2b2d

Please sign in to comment.