Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #91

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/devcontainers/dotnet:1-8.0-bookworm

RUN wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
sudo dpkg -i packages-microsoft-prod.deb && \
rm packages-microsoft-prod.deb

RUN sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-7.0
16 changes: 13 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
{
"name": "C# (.NET)",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/dotnet:1-8.0-bookworm",
// "image": "mcr.microsoft.com/devcontainers/dotnet:1-8.0-bookworm",
"build": {
"dockerfile": "Dockerfile"
},
"features": {
"ghcr.io/devcontainers-contrib/features/bash-command:1": {}
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
Expand All @@ -23,7 +26,14 @@
// "postCreateCommand": "dotnet restore",

// Configure tool-specific properties.
// "customizations": {},
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csharp",
"ms-dotnettools.csdevkit"
]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
Expand Down
26 changes: 26 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,32 @@ public void ResponseAll()
Assert.Contains("Program.cs", File.ReadAllLines(rsp));
}

[Fact]
public void ResponseMultiTarget()
{
var exitCode = RunCompLog($"rsp {Fixture.ClassLibMultiProjectPath}");
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.True(File.Exists(Path.Combine(RootDirectory, @".complog", "classlibmulti-net6.0", "build.rsp")));
Assert.True(File.Exists(Path.Combine(RootDirectory, @".complog", "classlibmulti-net7.0", "build.rsp")));
}

[Fact]
public void ResponseNoLogArgument()
{
var (exitCode, output) = RunCompLogEx($"rsp -o {RootDirectory}", Path.GetDirectoryName(Fixture.ConsoleProjectPath)!);
TestOutputHelper.WriteLine(output);
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.True(File.Exists(Path.Combine(RootDirectory, "console", "build.rsp")));
}

[Fact]
public void ResponseNoLogAvailable()
{
var dir = Root.NewDirectory("empty");
var exitCode = RunCompLog($"rsp", dir);
Assert.Equal(Constants.ExitFailure, exitCode);
}

[Fact]
public void ResponseHelp()
{
Expand Down
22 changes: 20 additions & 2 deletions src/Basic.CompilerLog.UnitTests/SolutionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public sealed class SolutionFixture : FixtureBase, IDisposable

internal string ClassLibProjectPath { get; }

internal string ClassLibMultiProjectPath { get; }

internal string ConsoleWithDiagnosticsBinaryLogPath { get; }

internal string ConsoleWithDiagnosticsProjectPath { get; }
Expand Down Expand Up @@ -71,6 +73,22 @@ public SolutionFixture(IMessageSink messageSink)
return Path.Combine(dir, "classlib.csproj");
});

ClassLibMultiProjectPath = WithProject("classlibmulti", string (string dir) =>
{
RunDotnetCommand("new classlib --name classlibmulti -o .", dir);
var projectFileContent = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
""";
File.WriteAllText(Path.Combine(dir, "classlibmulti.csproj"), projectFileContent, TestBase.DefaultEncoding);
return Path.Combine(dir, "classlibmulti.csproj");
});

string WithProject(string name, Func<string, string> func)
{
var dir = Path.Combine(StorageDirectory, name);
Expand All @@ -83,7 +101,7 @@ string WithProject(string name, Func<string, string> func)

ProjectPaths = builder.ToImmutableArray();
SolutionBinaryLogPath = Path.Combine(binlogDir, "msbuild.binlog");
DotnetUtil.CommandOrThrow($"dotnet build -bl:{SolutionBinaryLogPath} -nr:false", StorageDirectory);
RunDotnetCommand($"build -bl:{SolutionBinaryLogPath} -nr:false", StorageDirectory);

(RemovedConsoleProjectPath, RemovedBinaryLogPath) = CreateRemovedProject();
(ConsoleWithDiagnosticsProjectPath, ConsoleWithDiagnosticsBinaryLogPath) = CreateConsoleWithDiagnosticsProject();
Expand All @@ -96,7 +114,7 @@ string WithProject(string name, Func<string, string> func)
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);
RunDotnetCommand($"build -bl:{binlogFilePath} -nr:false", dir);
Directory.Delete(dir, recursive: true);
return (projectPath, binlogFilePath);
}
Expand Down
14 changes: 13 additions & 1 deletion src/Basic.CompilerLog.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ protected void RunDotNet(string command, string? workingDirectory = null)

protected void AddProjectProperty(string property, string? workingDirectory = null)
{
DotnetUtil.AddProjectProperty(property, workingDirectory ?? RootDirectory);
workingDirectory ??= RootDirectory;
var projectFile = Directory.EnumerateFiles(workingDirectory, "*proj").Single();
var lines = File.ReadAllLines(projectFile);
using var writer = new StreamWriter(projectFile, append: false);
foreach (var line in lines)
{
if (line.Contains("</PropertyGroup>"))
{
writer.WriteLine(property);
}

writer.WriteLine(line);
}
}

protected string GetBinaryLogFullPath(string? workingDirectory = null) =>
Expand Down
1 change: 1 addition & 0 deletions src/Basic.CompilerLog/Basic.CompilerLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared\DotnetUtil.cs" Link="DotnetUtil.cs" />
<Compile Include="..\Shared\PathUtil.cs" Link="PathUtil.cs" />
<Compile Include="..\Shared\ProcessUtil.cs" Link="ProcessUtil.cs" />
<PackageReference Include="Mono.Options" Version="6.12.0.148" />
Expand Down
4 changes: 2 additions & 2 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,10 @@ static string GetLogFilePathAfterBuild(string baseDirectory, string? buildFileNa
}

var tag = buildArgs.Any() ? "" : "-t:Rebuild";
var args = $"build {path} -bl:build.binlog {tag} {string.Join(' ', buildArgs)}";
var args = $"build {path} -bl:build.binlog -nr:false {tag} {string.Join(' ', buildArgs)}";
WriteLine($"Building {path}");
WriteLine($"dotnet {args}");
var result = ProcessUtil.Run("dotnet", args, baseDirectory);
var result = DotnetUtil.Command(args, baseDirectory);
WriteLine(result.StandardOut);
WriteLine(result.StandardError);
if (!result.Succeeded)
Expand Down
24 changes: 0 additions & 24 deletions src/Shared/DotnetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,4 @@ internal static ProcessResult Command(string args, string? workingDirectory = nu
workingDirectory: workingDirectory,
environment: env);
}

internal static void CommandOrThrow(string args, string? workingDirectory = null)
{
if (!Command(args, workingDirectory).Succeeded)
{
throw new Exception("Command failed");
}
}

internal static void AddProjectProperty(string property, string workingDirectory)
{
var projectFile = Directory.EnumerateFiles(workingDirectory, "*proj").Single();
var lines = File.ReadAllLines(projectFile);
using var writer = new StreamWriter(projectFile, append: false);
foreach (var line in lines)
{
if (line.Contains("</PropertyGroup>"))
{
writer.WriteLine(property);
}

writer.WriteLine(line);
}
}
}
Loading