Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkin

private void Info()
{
var res = dotnetCliInvoker.RunCommand("--info", silent: false);
if (!res)
var exitCode = dotnetCliInvoker.RunCommandExitCode("--info", silent: false);
switch (exitCode)
{
throw new Exception($"{dotnetCliInvoker.Exec} --info failed.");
case 0:
break;
case 143:
logger.LogWarning("Running 'dotnet --info' failed with exit code 143.");
break;
default:
throw new Exception($"{dotnetCliInvoker.Exec} --info failed with exit code {exitCode}.");
}

}

private string GetRestoreArgs(RestoreSettings restoreSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto
return startInfo;
}

private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
private int RunCommandExitCodeAux(string args, string? workingDirectory, out IList<string> output, out string dirLog, bool silent)
{
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
var pi = MakeDotnetStartInfo(args, workingDirectory);
var threadId = Environment.CurrentManagedThreadId;
void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
void onError(string s) => logger.LogError(s, threadId);
logger.LogInfo($"Running '{Exec} {args}'{dirLog}");
var exitCode = pi.ReadOutput(out output, onOut, onError);
return exitCode;
}

private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
{
var exitCode = RunCommandExitCodeAux(args, workingDirectory, out output, out var dirLog, silent);
if (exitCode != 0)
{
logger.LogError($"Command '{Exec} {args}'{dirLog} failed with exit code {exitCode}");
Expand All @@ -77,6 +83,9 @@ private bool RunCommandAux(string args, string? workingDirectory, out IList<stri
public bool RunCommand(string args, bool silent = true) =>
RunCommandAux(args, null, out _, silent);

public int RunCommandExitCode(string args, bool silent = true) =>
RunCommandExitCodeAux(args, null, out _, out _, silent);

public bool RunCommand(string args, out IList<string> output, bool silent = true) =>
RunCommandAux(args, null, out output, silent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ internal interface IDotNetCliInvoker
/// </summary>
bool RunCommand(string args, bool silent = true);

/// <summary>
/// Execute `dotnet <paramref name="args"/>` and return the exit code.
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
/// </summary>
int RunCommandExitCode(string args, bool silent = true);

/// <summary>
/// Execute `dotnet <paramref name="args"/>` and return true if the command succeeded, otherwise false.
/// The output of the command is returned in `output`.
Expand Down
25 changes: 23 additions & 2 deletions csharp/extractor/Semmle.Extraction.Tests/DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class DotNetCliInvokerStub : IDotNetCliInvoker
private string lastArgs = "";
public string WorkingDirectory { get; private set; } = "";
public bool Success { get; set; } = true;
public int ExitCode { get; set; } = 0;

public DotNetCliInvokerStub(IList<string> output)
{
Expand All @@ -26,6 +27,12 @@ public bool RunCommand(string args, bool silent)
return Success;
}

public int RunCommandExitCode(string args, bool silent)
{
lastArgs = args;
return ExitCode;
}

public bool RunCommand(string args, out IList<string> output, bool silent)
{
lastArgs = args;
Expand Down Expand Up @@ -83,7 +90,7 @@ public void TestDotnetInfo()
public void TestDotnetInfoFailure()
{
// Setup
var dotnetCliInvoker = new DotNetCliInvokerStub(new List<string>()) { Success = false };
var dotnetCliInvoker = new DotNetCliInvokerStub(new List<string>()) { ExitCode = 1 };

// Execute
try
Expand All @@ -94,12 +101,26 @@ public void TestDotnetInfoFailure()
// Verify
catch (Exception e)
{
Assert.Equal("dotnet --info failed.", e.Message);
Assert.Equal("dotnet --info failed with exit code 1.", e.Message);
return;
}
Assert.Fail("Expected exception");
}

[Fact]
public void TestDotnetInfoExitCode143()
{
// Setup
var dotnetCliInvoker = new DotNetCliInvokerStub(new List<string>()) { ExitCode = 143 };

// Execute
_ = MakeDotnet(dotnetCliInvoker);

// Verify
var lastArgs = dotnetCliInvoker.GetLastArgs();
Assert.Equal("--info", lastArgs);
}

[Fact]
public void TestDotnetRestoreProjectToDirectory1()
{
Expand Down