From b111d2e6410cc7382d9974c9891fbf3088dad7e6 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Tue, 28 Nov 2023 19:21:56 -0800 Subject: [PATCH 1/7] More coverage --- .../ProgramTests.cs | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index 649e216..c03b221 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -87,19 +87,18 @@ public void CreateNoopBuild() Assert.Empty(reader.ReadAllCompilerCalls()); } - [Theory] - [InlineData("console.sln")] - [InlineData("console.csproj")] - [InlineData("")] - public void CreateSolution(string target) + [Fact] + public void CreateWithBuild() { - RunDotNet("new console --name console -o ."); - RunDotNet("new sln --name console"); - RunDotNet("sln add console.csproj"); - Assert.Equal(0, RunCompLog($"create {target} -o msbuild.complog")); - var complogPath = Path.Combine(RootDirectory, "msbuild.complog"); - using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None); - Assert.Single(reader.ReadAllCompilerCalls()); + RunCore(Fixture.SolutionPath); + RunCore(Fixture.ConsoleProjectPath); + void RunCore(string filePath) + { + Assert.Equal(0, RunCompLog($"create {filePath} -o msbuild.complog")); + var complogPath = Path.Combine(RootDirectory, "msbuild.complog"); + using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None); + Assert.NotEmpty(reader.ReadAllCompilerCalls()); + } } /// @@ -127,16 +126,26 @@ public void CreateOverRemovedProject() Assert.Equal(1, RunCompLog($"create {Fixture.RemovedBinaryLogPath}")); } - /// - /// Don't search for complogs when an explicit log source isn't provided. - /// + [Theory] + [InlineData("-h")] + [InlineData("-help")] + public void CreateHelp(string arg) + { + Assert.Equal(1, RunCompLog($"create {arg}")); + } + [Fact] - public void CreateOtherComplogExists() + public void CreateExistingComplog() { - RunDotNet($"new console --name example --output ."); - RunDotNet("build -bl -nr:false"); - Root.NewFile("other.complog", ""); - Assert.Equal(0, RunCompLog($"create", RootDirectory)); + var complogPath = Path.Combine(RootDirectory, "file.complog"); + File.WriteAllText(complogPath, ""); + Assert.Equal(1, RunCompLog($"create {complogPath}")); + } + + [Fact] + public void CreateExtraArguments() + { + Assert.Equal(1, RunCompLog($"create {Fixture.SolutionBinaryLogPath} extra")); } [Fact] From 3af65897bfae8ed54783366d4ccd6c11b1282c02 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 07:49:55 -0800 Subject: [PATCH 2/7] Analyzer command tests --- .../ProgramTests.cs | 42 ++++++++++++++++++- src/Basic.CompilerLog/Constants.cs | 1 + src/Basic.CompilerLog/Program.cs | 10 +++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index c03b221..df12e40 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -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("
$", 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 action) @@ -148,6 +156,38 @@ public void CreateExtraArguments() Assert.Equal(1, RunCompLog($"create {Fixture.SolutionBinaryLogPath} extra")); } + [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 References() { diff --git a/src/Basic.CompilerLog/Constants.cs b/src/Basic.CompilerLog/Constants.cs index 60b3c79..540cd25 100644 --- a/src/Basic.CompilerLog/Constants.cs +++ b/src/Basic.CompilerLog/Constants.cs @@ -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; } diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index a686d05..1f5cedd 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -7,7 +7,6 @@ using System.Runtime.Loader; using System.Text; using static Constants; -using static System.Console; var (command, rest) = args.Length == 0 ? ("help", Enumerable.Empty()) @@ -15,7 +14,7 @@ // 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(); @@ -43,9 +42,9 @@ } catch (Exception e) { - RunHelp(null); WriteLine("Unexpected error"); WriteLine(e.Message); + RunHelp(null); return ExitFailure; } @@ -125,7 +124,7 @@ int RunAnalyzers(IEnumerable args) if (options.Help) { PrintUsage(); - return ExitFailure; + return ExitSuccess; } using var compilerLogStream = GetOrCreateCompilerLogStream(extra); @@ -723,3 +722,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); From e0f1f6a3466608aebfd28f12a99494a831547ffe Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 08:03:56 -0800 Subject: [PATCH 3/7] Print tests --- .../ProgramTests.cs | 35 +++++++++++++++++++ src/Basic.CompilerLog/Program.cs | 4 +-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index df12e40..dc536aa 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -238,5 +238,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 diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index 1f5cedd..efbc446 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -166,7 +166,7 @@ int RunPrint(IEnumerable args) if (options.Help) { PrintUsage(); - return ExitFailure; + return ExitSuccess; } using var compilerLogStream = GetOrCreateCompilerLogStream(extra); @@ -582,7 +582,7 @@ string GetLogFilePath(List 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); From e4a4a2fefa6581ba203eef02eefb5c19377afcde Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 08:11:08 -0800 Subject: [PATCH 4/7] References tests --- .../ProgramTests.cs | 94 +++++++++++++------ src/Basic.CompilerLog/Program.cs | 4 +- 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index dc536aa..106a26c 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -60,6 +60,39 @@ private void RunWithBoth(Action 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); + } + + [Theory] [InlineData("", "msbuild.complog")] [InlineData("--out custom.complog", "custom.complog")] @@ -157,46 +190,30 @@ public void CreateExtraArguments() } [Fact] - public void AnalyzersSimple() + public void References() { - var (exitCode, output) = RunCompLogEx($"analyzers {Fixture.SolutionBinaryLogPath} -p console.csproj"); - Assert.Equal(0, exitCode); - Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers.dll", output); + RunWithBoth(logPath => + { + Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {logPath}")); + Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "refs"), "*.dll")); + Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "analyzers"), "*.dll", SearchOption.AllDirectories)); + }); } [Fact] - public void AnalyzersHelp() + public void ReferencesHelp() { - var (exitCode, output) = RunCompLogEx($"analyzers -h"); + var (exitCode, output) = RunCompLogEx($"ref -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); + Assert.StartsWith("complog ref [OPTIONS]", output); } [Fact] - public void References() + public void ReferencesBadOption() { - RunWithBoth(logPath => - { - Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {logPath}")); - Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "refs"), "*.dll")); - Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "analyzers"), "*.dll", SearchOption.AllDirectories)); - }); + var (exitCode, output) = RunCompLogEx($"ref --not-an-option"); + Assert.Equal(1, exitCode); + Assert.Contains("complog ref [OPTIONS]", output); } [Fact] @@ -215,6 +232,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")] diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index efbc446..f8c3256 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -209,7 +209,7 @@ int RunReferences(IEnumerable args) if (options.Help) { PrintUsage(); - return ExitFailure; + return ExitSuccess; } using var compilerLogStream = GetOrCreateCompilerLogStream(extra); @@ -521,7 +521,7 @@ passed after --. """); } - return ExitFailure; + return ExitSuccess; } List GetCompilerCalls(List extra, Func? predicate) From 3081d8afa5aa2666597b6a54020c52a09df59c3b Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 08:28:06 -0800 Subject: [PATCH 5/7] Response tests --- .../ProgramTests.cs | 43 +++++++++++++++++++ src/Basic.CompilerLog/Program.cs | 11 ++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index 106a26c..dce4192 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -92,6 +92,13 @@ public void AnalyzerBadOption() 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")] @@ -216,6 +223,42 @@ public void ReferencesBadOption() 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() { diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index f8c3256..cabe944 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -37,7 +37,7 @@ // Older option names "diagnostics" => RunReplay(rest, cts.Token), "emit" => RunReplay(rest, cts.Token), - _ => RunHelp(null) + _ => RunBadCommand(command) }; } catch (Exception e) @@ -347,7 +347,7 @@ int RunResponseFile(IEnumerable args) if (options.Help) { PrintUsage(); - return ExitFailure; + return ExitSuccess; } var compilerCalls = GetCompilerCalls(extra, options.FilterCompilerCalls); @@ -483,6 +483,13 @@ void PrintUsage() } } +int RunBadCommand(string command) +{ + WriteLine(@"""{command}"" is not a valid command"); + _ = RunHelp(null); + return ExitFailure; +} + int RunHelp(IEnumerable? args) { var verbose = false; From 9dc399829d9114dd4b9af93a96a674d50207b033 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 08:32:04 -0800 Subject: [PATCH 6/7] Fixed tests --- src/Basic.CompilerLog/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index cabe944..be12293 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -274,7 +274,7 @@ string GetGroupDirectoryPath() void PrintUsage() { - WriteLine("complog rsp [OPTIONS] msbuild.complog"); + WriteLine("complog ref [OPTIONS] msbuild.complog"); options.WriteOptionDescriptions(Out); } } @@ -485,7 +485,7 @@ void PrintUsage() int RunBadCommand(string command) { - WriteLine(@"""{command}"" is not a valid command"); + WriteLine(@$"""{command}"" is not a valid command"); _ = RunHelp(null); return ExitFailure; } From c82e5b6b270147678f8d819f753517bac46f7f00 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 29 Nov 2023 08:39:02 -0800 Subject: [PATCH 7/7] Fix Linux tests --- src/Basic.CompilerLog.UnitTests/ProgramTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index dce4192..9b41064 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -228,7 +228,7 @@ 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"); + var rsp = Path.Combine(RootDirectory, @".complog", "console", "build.rsp"); Assert.True(File.Exists(rsp)); Assert.Contains("Program.cs", File.ReadAllLines(rsp)); } @@ -238,7 +238,7 @@ public void ResponseAll() { var exitCode = RunCompLog($"rsp {Fixture.SolutionBinaryLogPath}"); Assert.Equal(0, exitCode); - var rsp = Path.Combine(RootDirectory, @".complog\console\build.rsp"); + var rsp = Path.Combine(RootDirectory, @".complog", "console", "build.rsp"); Assert.True(File.Exists(rsp)); Assert.Contains("Program.cs", File.ReadAllLines(rsp)); }