Skip to content

Commit

Permalink
Fix a couple console output issues
Browse files Browse the repository at this point in the history
closes #171, #132
  • Loading branch information
jaredpar committed Nov 6, 2024
1 parent 4c3a129 commit 7b48af6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ public void CreateExtraArguments()
Assert.Equal(Constants.ExitFailure, RunCompLog($"create {Fixture.SolutionBinaryLogPath} extra"));
}

[Fact]
public void CreateFilePathOutput()
{
var complogFilePath = Path.Combine(RootDirectory, "file.complog");
var (exitCode, output) = RunCompLogEx($"create {Fixture.ClassLibProjectPath} -o {complogFilePath}");
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.Contains($"Wrote {complogFilePath}", output);
}

[Fact]
public void CreateMultipleFiles()
{
File.Copy(Fixture.ConsoleWithDiagnosticsBinaryLogPath, Path.Combine(RootDirectory, "console1.binlog"));
File.Copy(Fixture.ConsoleWithDiagnosticsBinaryLogPath, Path.Combine(RootDirectory, "console2.binlog"));
var (exitCode, output) = RunCompLogEx($"create");
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.Contains($"Found multiple log files in {RootDirectory}", output);
}

[Fact]
public void References()
{
Expand Down
26 changes: 17 additions & 9 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ int RunCreate(IEnumerable<string> args)
return ExitFailure;
}

WriteLine($"Wrote {complogFilePath}");

return convertResult.Succeeded ? ExitSuccess : ExitFailure;
}
catch (OptionException e)
Expand Down Expand Up @@ -701,12 +703,13 @@ static void CheckCompilerLogReader(CompilerLogReader reader, bool checkVersion)
string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
{
string? logFilePath;
bool foundMultiple = false;
IEnumerable<string> args = Array.Empty<string>();
string baseDirectory = CurrentDirectory;
var printFile = false;
if (extra.Count == 0)
{
logFilePath = FindLogFilePath(baseDirectory, includeCompilerLogs);
(logFilePath, foundMultiple) = FindLogFilePath(baseDirectory, includeCompilerLogs);
printFile = true;
}
else
Expand All @@ -716,7 +719,7 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)) && Directory.Exists(logFilePath))
{
baseDirectory = logFilePath;
logFilePath = FindLogFilePath(baseDirectory, includeCompilerLogs);
(logFilePath, foundMultiple) = FindLogFilePath(baseDirectory, includeCompilerLogs);
printFile = true;
}
}
Expand All @@ -729,6 +732,11 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
// If the file wasn't explicitly specified let the user know what file we are using
if (printFile)
{
if (foundMultiple)
{
WriteLine($"Found multiple log files in {baseDirectory}");
}

WriteLine($"Using {logFilePath}");
}

Expand All @@ -750,7 +758,7 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
throw new OptionException($"Not a valid log file {logFilePath}", "log");
}

static string? FindLogFilePath(string baseDirectory, bool includeCompilerLogs = true) =>
static (string? FilePath, bool FoundMultiple) FindLogFilePath(string baseDirectory, bool includeCompilerLogs = true) =>
includeCompilerLogs
? FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog", "*.sln", "*.csproj", ".vbproj")
: FindFirstFileWithPattern(baseDirectory, "*.binlog", "*.sln", "*.csproj", ".vbproj");
Expand Down Expand Up @@ -778,21 +786,21 @@ static string GetLogFilePathAfterBuild(string appDataDirectory, string baseDirec
static OptionException CreateOptionException() => new("Need a file to analyze", "log");
}

static string? FindFirstFileWithPattern(string baseDirectory, params string[] patterns)
static (string? FilePath, bool FoundMultiple) FindFirstFileWithPattern(string baseDirectory, params string[] patterns)
{
foreach (var pattern in patterns)
{
var path = Directory
using var e = Directory
.EnumerateFiles(baseDirectory, pattern)
.OrderBy(x => Path.GetFileName(x), PathUtil.Comparer)
.FirstOrDefault();
if (path is not null)
.GetEnumerator();
if (e.MoveNext())
{
return path;
return (e.Current, e.MoveNext());
}
}

return null;
return (null, false);
}

string GetBaseOutputPath(string? baseOutputPath, string? directoryName = null)
Expand Down

0 comments on commit 7b48af6

Please sign in to comment.