Skip to content

Commit 2ae3b59

Browse files
authored
Fix a couple console output issues (#173)
closes #171, #132
1 parent 4c3a129 commit 2ae3b59

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/Basic.CompilerLog.UnitTests/ProgramTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ public void CreateExtraArguments()
270270
Assert.Equal(Constants.ExitFailure, RunCompLog($"create {Fixture.SolutionBinaryLogPath} extra"));
271271
}
272272

273+
[Fact]
274+
public void CreateFilePathOutput()
275+
{
276+
var complogFilePath = Path.Combine(RootDirectory, "file.complog");
277+
var (exitCode, output) = RunCompLogEx($"create {Fixture.ClassLibProjectPath} -o {complogFilePath}");
278+
Assert.Equal(Constants.ExitSuccess, exitCode);
279+
Assert.Contains($"Wrote {complogFilePath}", output);
280+
}
281+
282+
[Fact]
283+
public void CreateMultipleFiles()
284+
{
285+
File.Copy(Fixture.ConsoleWithDiagnosticsBinaryLogPath, Path.Combine(RootDirectory, "console1.binlog"));
286+
File.Copy(Fixture.ConsoleWithDiagnosticsBinaryLogPath, Path.Combine(RootDirectory, "console2.binlog"));
287+
var (exitCode, output) = RunCompLogEx($"create");
288+
Assert.Equal(Constants.ExitSuccess, exitCode);
289+
Assert.Contains($"Found multiple log files in {RootDirectory}", output);
290+
}
291+
273292
[Fact]
274293
public void References()
275294
{

src/Basic.CompilerLog/Program.cs

+17-9
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ int RunCreate(IEnumerable<string> args)
101101
return ExitFailure;
102102
}
103103

104+
WriteLine($"Wrote {complogFilePath}");
105+
104106
return convertResult.Succeeded ? ExitSuccess : ExitFailure;
105107
}
106108
catch (OptionException e)
@@ -701,12 +703,13 @@ static void CheckCompilerLogReader(CompilerLogReader reader, bool checkVersion)
701703
string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
702704
{
703705
string? logFilePath;
706+
bool foundMultiple = false;
704707
IEnumerable<string> args = Array.Empty<string>();
705708
string baseDirectory = CurrentDirectory;
706709
var printFile = false;
707710
if (extra.Count == 0)
708711
{
709-
logFilePath = FindLogFilePath(baseDirectory, includeCompilerLogs);
712+
(logFilePath, foundMultiple) = FindLogFilePath(baseDirectory, includeCompilerLogs);
710713
printFile = true;
711714
}
712715
else
@@ -716,7 +719,7 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
716719
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)) && Directory.Exists(logFilePath))
717720
{
718721
baseDirectory = logFilePath;
719-
logFilePath = FindLogFilePath(baseDirectory, includeCompilerLogs);
722+
(logFilePath, foundMultiple) = FindLogFilePath(baseDirectory, includeCompilerLogs);
720723
printFile = true;
721724
}
722725
}
@@ -729,6 +732,11 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
729732
// If the file wasn't explicitly specified let the user know what file we are using
730733
if (printFile)
731734
{
735+
if (foundMultiple)
736+
{
737+
WriteLine($"Found multiple log files in {baseDirectory}");
738+
}
739+
732740
WriteLine($"Using {logFilePath}");
733741
}
734742

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

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

781-
static string? FindFirstFileWithPattern(string baseDirectory, params string[] patterns)
789+
static (string? FilePath, bool FoundMultiple) FindFirstFileWithPattern(string baseDirectory, params string[] patterns)
782790
{
783791
foreach (var pattern in patterns)
784792
{
785-
var path = Directory
793+
using var e = Directory
786794
.EnumerateFiles(baseDirectory, pattern)
787795
.OrderBy(x => Path.GetFileName(x), PathUtil.Comparer)
788-
.FirstOrDefault();
789-
if (path is not null)
796+
.GetEnumerator();
797+
if (e.MoveNext())
790798
{
791-
return path;
799+
return (e.Current, e.MoveNext());
792800
}
793801
}
794802

795-
return null;
803+
return (null, false);
796804
}
797805

798806
string GetBaseOutputPath(string? baseOutputPath, string? directoryName = null)

0 commit comments

Comments
 (0)