Skip to content

Commit

Permalink
Merge pull request #86 from jaredpar/coverage
Browse files Browse the repository at this point in the history
Much better tests for Program
  • Loading branch information
jaredpar authored Nov 29, 2023
2 parents 4129baa + c82e5b6 commit c4a2c95
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 12 deletions.
154 changes: 153 additions & 1 deletion src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ public ProgramTests(ITestOutputHelper testOutputHelper, SolutionFixture fixture)

public int RunCompLog(string args, string? currentDirectory = null)
{
var (exitCode, _) = RunCompLogEx(args, currentDirectory);
return exitCode;
}

public (int ExitCode, string Output) RunCompLogEx(string args, string? currentDirectory = null)
{
var writer = new System.IO.StringWriter();
currentDirectory ??= RootDirectory;
Constants.CurrentDirectory = currentDirectory;
Constants.Out = writer;
var assembly = typeof(FilterOptionSet).Assembly;
var program = assembly.GetType("Program", throwOnError: true);
var main = program!.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);
Assert.NotNull(main);
var ret = main!.Invoke(null, new[] { args.Split(' ', StringSplitOptions.RemoveEmptyEntries) });
return (int)ret!;
return ((int)ret!, writer.ToString());
}

private void RunWithBoth(Action<string> action)
Expand All @@ -52,6 +60,46 @@ private void RunWithBoth(Action<string> action)
action(complogPath);
}

[Fact]
public void AnalyzersSimple()
{
var (exitCode, output) = RunCompLogEx($"analyzers {Fixture.SolutionBinaryLogPath} -p console.csproj");
Assert.Equal(0, exitCode);
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers.dll", output);
}

[Fact]
public void AnalyzersHelp()
{
var (exitCode, output) = RunCompLogEx($"analyzers -h");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog analyzers [OPTIONS]", output);
}

[Fact]
public void AnalyzersError()
{
var (exitCode, output) = RunCompLogEx($"analyzers {Fixture.RemovedBinaryLogPath}");
Assert.NotEqual(0, exitCode);
Assert.StartsWith("Unexpected error", output);
}

[Fact]
public void AnalyzerBadOption()
{
var (exitCode, output) = RunCompLogEx($"analyzers {Fixture.RemovedBinaryLogPath} --not-an-option");
Assert.NotEqual(0, exitCode);
Assert.StartsWith("Extra arguments", output);
}

[Fact]
public void BadCommand()
{
var (exitCode, output) = RunCompLogEx("invalid");
Assert.NotEqual(0, exitCode);
Assert.Contains(@"""invalid"" is not a valid command", output);
}

[Theory]
[InlineData("", "msbuild.complog")]
[InlineData("--out custom.complog", "custom.complog")]
Expand Down Expand Up @@ -159,6 +207,58 @@ public void References()
});
}

[Fact]
public void ReferencesHelp()
{
var (exitCode, output) = RunCompLogEx($"ref -h");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog ref [OPTIONS]", output);
}

[Fact]
public void ReferencesBadOption()
{
var (exitCode, output) = RunCompLogEx($"ref --not-an-option");
Assert.Equal(1, exitCode);
Assert.Contains("complog ref [OPTIONS]", output);
}

[Fact]
public void ResponseSingle()
{
var exitCode = RunCompLog($"rsp {Fixture.SolutionBinaryLogPath} -p console.csproj");
Assert.Equal(0, exitCode);
var rsp = Path.Combine(RootDirectory, @".complog", "console", "build.rsp");
Assert.True(File.Exists(rsp));
Assert.Contains("Program.cs", File.ReadAllLines(rsp));
}

[Fact]
public void ResponseAll()
{
var exitCode = RunCompLog($"rsp {Fixture.SolutionBinaryLogPath}");
Assert.Equal(0, exitCode);
var rsp = Path.Combine(RootDirectory, @".complog", "console", "build.rsp");
Assert.True(File.Exists(rsp));
Assert.Contains("Program.cs", File.ReadAllLines(rsp));
}

[Fact]
public void ResponseHelp()
{
var (exitCode, output) = RunCompLogEx($"rsp -h");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog rsp [OPTIONS]", output);
}

[Fact]
public void ResponseBadOption()
{
var (exitCode, output) = RunCompLogEx($"rsp --not-an-option");
Assert.Equal(1, exitCode);
Assert.Contains("complog rsp [OPTIONS]", output);
}

[Fact]
public void ExportCompilerLog()
{
Expand All @@ -175,6 +275,23 @@ public void ExportCompilerLog()
});
}

[Fact]
public void Help()
{
var (exitCode, output) = RunCompLogEx($"help");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog [command] [args]", output);
}

[Fact]
public void HelpVerbose()
{
var (exitCode, output) = RunCompLogEx($"help -v");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog [command] [args]", output);
Assert.Contains("Commands can be passed a .complog, ", output);
}

[Theory]
[InlineData("")]
[InlineData("-none")]
Expand All @@ -198,5 +315,40 @@ void AssertOutput(string relativePath)
Assert.True(File.Exists(filePath));
}
}

[Fact]
public void PrintAll()
{
var (exitCode, output) = RunCompLogEx($"print {Fixture.SolutionBinaryLogPath}");
Assert.Equal(0, exitCode);
Assert.Contains("console.csproj (net7.0)", output);
Assert.Contains("classlib.csproj (net7.0)", output);
}

[Fact]
public void PrintOne()
{
var (exitCode, output) = RunCompLogEx($"print {Fixture.SolutionBinaryLogPath} -p classlib.csproj");
Assert.Equal(0, exitCode);
Assert.DoesNotContain("console.csproj (net7.0)", output);
Assert.Contains("classlib.csproj (net7.0)", output);
}

[Fact]
public void PrintHelp()
{
var (exitCode, output) = RunCompLogEx($"print -h");
Assert.Equal(0, exitCode);
Assert.StartsWith("complog print [OPTIONS]", output);
}

[Fact]
public void PrintError()
{
var (exitCode, output) = RunCompLogEx($"print --not-an-option");
Assert.Equal(1, exitCode);
Assert.Contains("complog print [OPTIONS]", output);
}

}
#endif
1 change: 1 addition & 0 deletions src/Basic.CompilerLog/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ internal static class Constants
internal const int ExitSuccess = 0;

internal static string CurrentDirectory { get; set; } = Environment.CurrentDirectory;
internal static TextWriter Out { get; set; } = Console.Out;
}
31 changes: 20 additions & 11 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
using System.Runtime.Loader;
using System.Text;
using static Constants;
using static System.Console;

var (command, rest) = args.Length == 0
? ("help", Enumerable.Empty<string>())
: (args[0], args.Skip(1));
// a CancellationToken that is canceled when the user hits Ctrl+C.
var cts = new CancellationTokenSource();

CancelKeyPress += (s, e) =>
Console.CancelKeyPress += (s, e) =>
{
WriteLine("Canceling...");
cts.Cancel();
Expand All @@ -38,14 +37,14 @@
// Older option names
"diagnostics" => RunReplay(rest, cts.Token),
"emit" => RunReplay(rest, cts.Token),
_ => RunHelp(null)
_ => RunBadCommand(command)
};
}
catch (Exception e)
{
RunHelp(null);
WriteLine("Unexpected error");
WriteLine(e.Message);
RunHelp(null);
return ExitFailure;
}

Expand Down Expand Up @@ -125,7 +124,7 @@ int RunAnalyzers(IEnumerable<string> args)
if (options.Help)
{
PrintUsage();
return ExitFailure;
return ExitSuccess;
}

using var compilerLogStream = GetOrCreateCompilerLogStream(extra);
Expand Down Expand Up @@ -167,7 +166,7 @@ int RunPrint(IEnumerable<string> args)
if (options.Help)
{
PrintUsage();
return ExitFailure;
return ExitSuccess;
}

using var compilerLogStream = GetOrCreateCompilerLogStream(extra);
Expand Down Expand Up @@ -210,7 +209,7 @@ int RunReferences(IEnumerable<string> args)
if (options.Help)
{
PrintUsage();
return ExitFailure;
return ExitSuccess;
}

using var compilerLogStream = GetOrCreateCompilerLogStream(extra);
Expand Down Expand Up @@ -275,7 +274,7 @@ string GetGroupDirectoryPath()

void PrintUsage()
{
WriteLine("complog rsp [OPTIONS] msbuild.complog");
WriteLine("complog ref [OPTIONS] msbuild.complog");
options.WriteOptionDescriptions(Out);
}
}
Expand Down Expand Up @@ -348,7 +347,7 @@ int RunResponseFile(IEnumerable<string> args)
if (options.Help)
{
PrintUsage();
return ExitFailure;
return ExitSuccess;
}

var compilerCalls = GetCompilerCalls(extra, options.FilterCompilerCalls);
Expand Down Expand Up @@ -484,6 +483,13 @@ void PrintUsage()
}
}

int RunBadCommand(string command)
{
WriteLine(@$"""{command}"" is not a valid command");
_ = RunHelp(null);
return ExitFailure;
}

int RunHelp(IEnumerable<string>? args)
{
var verbose = false;
Expand Down Expand Up @@ -522,7 +528,7 @@ passed after --.
""");
}

return ExitFailure;
return ExitSuccess;
}

List<CompilerCall> GetCompilerCalls(List<string> extra, Func<CompilerCall, bool>? predicate)
Expand Down Expand Up @@ -583,7 +589,7 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
{
logFilePath = extra[0];
args = extra.Skip(1);
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)))
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)) && Directory.Exists(logFilePath))
{
baseDirectory = logFilePath;
logFilePath = FindLogFilePath(baseDirectory, includeCompilerLogs);
Expand Down Expand Up @@ -723,3 +729,6 @@ static string GetResolvedPath(string baseDirectory, string path)

return Path.Combine(baseDirectory, path);
}

static void Write(string str) => Constants.Out.Write(str);
static void WriteLine(string line) => Constants.Out.WriteLine(line);

0 comments on commit c4a2c95

Please sign in to comment.