Skip to content

Commit

Permalink
cover scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Nov 29, 2023
1 parent aa85a5b commit b518c33
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public int RunCompLog(string args, string? currentDirectory = null)
private void RunWithBoth(Action<string> action)
{
// Run with the binary log
action(Fixture.BinaryLogPath);
action(Fixture.SolutionBinaryLogPath);

// Now create a compiler log
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
var diagnostics = CompilerLogUtil.ConvertBinaryLog(Fixture.BinaryLogPath, complogPath);
var diagnostics = CompilerLogUtil.ConvertBinaryLog(Fixture.SolutionBinaryLogPath, complogPath);
Assert.Empty(diagnostics);
action(complogPath);
}
Expand All @@ -58,7 +58,7 @@ private void RunWithBoth(Action<string> action)
[InlineData("-o custom.complog", "custom.complog")]
public void Create(string extra, string fileName)
{
Assert.Equal(0, RunCompLog($"create {extra} -p {Fixture.ConsoleProjectName} {Fixture.BinaryLogPath}"));
Assert.Equal(0, RunCompLog($"create {extra} -p {Fixture.ConsoleProjectName} {Fixture.SolutionBinaryLogPath}"));
var complogPath = Path.Combine(RootDirectory, fileName);
Assert.True(File.Exists(complogPath));
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public void CreateSolution(string target)
[Fact]
public void CreateEmpty()
{
var result = RunCompLog($"create -p does-not-exist.csproj {Fixture.BinaryLogPath}");
var result = RunCompLog($"create -p does-not-exist.csproj {Fixture.SolutionBinaryLogPath}");
Assert.NotEqual(0, result);
}

Expand All @@ -121,6 +121,12 @@ public void CreateFullPath()
Assert.Equal(0, RunCompLog($"create {GetBinaryLogFullPath()}", RootDirectory));
}

[Fact]
public void CreateOverRemovedProject()
{
Assert.Equal(1, RunCompLog($"create {Fixture.RemovedBinaryLogPath}"));
}

/// <summary>
/// Don't search for complogs when an explicit log source isn't provided.
/// </summary>
Expand Down Expand Up @@ -166,7 +172,7 @@ public void ExportCompilerLog()
public void ReplayConsoleWithEmit(string arg)
{
using var emitDir = new TempDir();
RunCompLog($"replay {arg} -emit -o {emitDir.DirectoryPath} {Fixture.BinaryLogPath}");
RunCompLog($"replay {arg} -emit -o {emitDir.DirectoryPath} {Fixture.SolutionBinaryLogPath}");

AssertOutput(@"console\emit\console.dll");
AssertOutput(@"console\emit\console.pdb");
Expand Down
32 changes: 29 additions & 3 deletions src/Basic.CompilerLog.UnitTests/SolutionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@ public sealed class SolutionFixture : FixtureBase, IDisposable

internal string SolutionPath { get; }

internal string BinaryLogPath { get; }
internal string SolutionBinaryLogPath { get; }

internal string ConsoleProjectPath { get; }

internal string ConsoleProjectName => Path.GetFileName(ConsoleProjectPath);

internal string ClassLibProjectPath { get; }

internal string RemovedBinaryLogPath { get; }

/// <summary>
/// This project is deleted off of disk after the binary log is created. This means subsequent calls
/// to create a compiler log over it will fail. Useful for testing error cases.
/// </summary>
internal string RemovedConsoleProjectPath { get; }

internal string RemovedConsoleProjectName => Path.GetFileName(RemovedConsoleProjectPath);

public SolutionFixture(IMessageSink messageSink)
: base(messageSink)
{
StorageDirectory = Path.Combine(Path.GetTempPath(), nameof(CompilerLogFixture), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(StorageDirectory);
SolutionPath = Path.Combine(StorageDirectory, "Solution.sln");
var binlogDir = Path.Combine(StorageDirectory, "binlogs");
Directory.CreateDirectory(binlogDir);

RunDotnetCommand("new globaljson --sdk-version 7.0.400", StorageDirectory);
RunDotnetCommand("dotnet new sln -n Solution", StorageDirectory);
Expand Down Expand Up @@ -64,8 +76,22 @@ string WithProject(string name, Func<string, string> func)
};
ProjectPaths = builder.ToImmutableArray();
DotnetUtil.CommandOrThrow("dotnet build -bl -nr:false", StorageDirectory);
BinaryLogPath = Path.Combine(StorageDirectory, "msbuild.binlog");
SolutionBinaryLogPath = Path.Combine(binlogDir, "msbuild.binlog");
DotnetUtil.CommandOrThrow($"dotnet build -bl:{SolutionBinaryLogPath} -nr:false", StorageDirectory);
(RemovedConsoleProjectPath, RemovedBinaryLogPath) = CreateRemovedProject();

(string, string) CreateRemovedProject()
{
var dir = Path.Combine(StorageDirectory, "removed");
Directory.CreateDirectory(dir);
RunDotnetCommand("new console --name removed-console -o .", dir);
var projectPath = Path.Combine(dir, "removed-console.csproj");
var binlogFilePath = Path.Combine(binlogDir, "removed-console.binlog");

DotnetUtil.CommandOrThrow($"dotnet build -bl:{binlogFilePath} -nr:false", dir);
Directory.Delete(dir, recursive: true);
return (projectPath, binlogFilePath);
}
}

public void Dispose()
Expand Down

0 comments on commit b518c33

Please sign in to comment.