Skip to content
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 src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public static void Main(string[] args)
{
Console.WriteLine("Hello world");
Console.WriteLine($"MyCoolEnvironmentVariableKey={Environment.GetEnvironmentVariable("MyCoolEnvironmentVariableKey")}");
if (args.Length > 0)
{
Console.WriteLine(args[0]);
}
if (args.Length > 1)
{
Console.WriteLine(args[1]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"TestAppWithLaunchSettings": {
"commandName": "Project",
"dotnetRunMessages": "true",
"commandLineArgs": "TestAppCommandLineArguments SecondTestAppCommandLineArguments",
"environmentVariables": {
"MyCoolEnvironmentVariableKey": "MyCoolEnvironmentVariableValue"
}
Expand Down
4 changes: 4 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,9 @@ public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false,
{
throw new NotImplementedException();
}
public ICommand SetCommandArgs(string commandArgs)
{
throw new NotImplementedException();
}
}
}
6 changes: 6 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public ICommand OnErrorLine(Action<string> handler)

public string CommandArgs => _process.StartInfo.Arguments;

public ICommand SetCommandArgs(string commandArgs)
{
_process.StartInfo.Arguments = commandArgs;
return this;
}

private string FormatProcessInfo(ProcessStartInfo info)
{
if (string.IsNullOrWhiteSpace(info.Arguments))
Expand Down
2 changes: 2 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface ICommand

ICommand OnErrorLine(Action<string> handler);

ICommand SetCommandArgs(string commandArgs);

string CommandName { get; }

string CommandArgs { get; }
Expand Down
4 changes: 4 additions & 0 deletions src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public int Execute()
//NOTE: MSBuild variables are not expanded like they are in VS
targetCommand.EnvironmentVariable(entry.Key, value);
}
if (String.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null)
{
targetCommand.SetCommandArgs(launchSettings.CommandLineArgs);
}
}

// Ignore Ctrl-C for the remainder of the command's execution
Expand Down
40 changes: 40 additions & 0 deletions src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,5 +688,45 @@ public void ItIncludesEnvironmentVariablesSpecifiedInLaunchSettings()
.And
.HaveStdOutContaining(expectedValue);
}

[Fact]
public void ItIncludesCommandArgumentsSpecifiedInLaunchSettings()
{
var expectedValue = "TestAppCommandLineArguments";
var secondExpectedValue = "SecondTestAppCommandLineArguments";
var testAppName = "TestAppWithLaunchSettings";
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
.WithSource();

new DotnetCommand(Log, "run")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining(expectedValue)
.And
.HaveStdOutContaining(secondExpectedValue);
}

[Fact]
public void ItCLIArgsOverrideCommandArgumentsSpecifiedInLaunchSettings()
{
var expectedValue = "TestAppCommandLineArguments";
var secondExpectedValue = "SecondTestAppCommandLineArguments";
var testAppName = "TestAppWithLaunchSettings";
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
.WithSource();

new DotnetCommand(Log, "run", "-- test")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should()
.Pass()
.And
.NotHaveStdOutContaining(expectedValue)
.And
.NotHaveStdOutContaining(secondExpectedValue);
}
}
}