Skip to content

Commit

Permalink
Introduce name config argument (actions#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliobbv authored Dec 11, 2019
1 parent 275ab75 commit a3c6a8c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Runner.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public static class CommandLine
//validArgs array as well present in the CommandSettings.cs
public static class Args
{
public static readonly string Agent = "agent";
public static readonly string Auth = "auth";
public static readonly string MonitorSocketAddress = "monitorsocketaddress";
public static readonly string Name = "name";
public static readonly string Pool = "pool";
public static readonly string SslCACert = "sslcacert";
public static readonly string SslClientCert = "sslclientcert";
Expand Down
20 changes: 10 additions & 10 deletions src/Runner.Listener/CommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public sealed class CommandSettings

private readonly string[] validArgs =
{
Constants.Runner.CommandLine.Args.Agent,
Constants.Runner.CommandLine.Args.Auth,
Constants.Runner.CommandLine.Args.MonitorSocketAddress,
Constants.Runner.CommandLine.Args.Name,
Constants.Runner.CommandLine.Args.Password,
Constants.Runner.CommandLine.Args.Pool,
Constants.Runner.CommandLine.Args.SslCACert,
Expand Down Expand Up @@ -170,15 +170,6 @@ public bool GetAutoLaunchBrowser()
//
// Args.
//
public string GetAgentName()
{
return GetArgOrPrompt(
name: Constants.Runner.CommandLine.Args.Agent,
description: "Enter the name of runner:",
defaultValue: Environment.MachineName ?? "myagent",
validator: Validators.NonEmptyValidator);
}

public string GetAuth(string defaultValue)
{
return GetArgOrPrompt(
Expand Down Expand Up @@ -206,6 +197,15 @@ public string GetPool()
validator: Validators.NonEmptyValidator);
}

public string GetRunnerName()
{
return GetArgOrPrompt(
name: Constants.Runner.CommandLine.Args.Name,
description: "Enter the name of runner:",
defaultValue: Environment.MachineName ?? "myrunner",
validator: Validators.NonEmptyValidator);
}

public string GetToken()
{
return GetArgOrPrompt(
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.Listener/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task ConfigureAsync(CommandSettings command)
TaskAgent agent;
while (true)
{
runnerSettings.AgentName = command.GetAgentName();
runnerSettings.AgentName = command.GetRunnerName();

_term.WriteLine();

Expand Down
46 changes: 22 additions & 24 deletions src/Test/L0/Listener/CommandSettingsL0.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using GitHub.Runner.Listener;
using GitHub.Runner.Listener.Configuration;
using GitHub.Runner.Common.Util;
using Moq;
using System;
using System.Runtime.CompilerServices;
using Xunit;
using GitHub.Runner.Sdk;

namespace GitHub.Runner.Common.Tests
{
Expand All @@ -18,45 +16,45 @@ public sealed class CommandSettingsL0
[Fact]
[Trait("Level", "L0")]
[Trait("Category", nameof(CommandSettings))]
public void GetsArg()
public void GetsNameArg()
{
using (TestHostContext hc = CreateTestContext())
{
// Arrange.
var command = new CommandSettings(hc, args: new string[] { "--agent", "some agent" });
var command = new CommandSettings(hc, args: new string[] { "--name", "some runner" });

// Act.
string actual = command.GetAgentName();
string actual = command.GetRunnerName();

// Assert.
Assert.Equal("some agent", actual);
Assert.Equal("some runner", actual);
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", nameof(CommandSettings))]
public void GetsArgFromEnvVar()
public void GetsNameArgFromEnvVar()
{
using (TestHostContext hc = CreateTestContext())
{
try
{
// Arrange.
Environment.SetEnvironmentVariable("ACTIONS_RUNNER_INPUT_AGENT", "some agent");
Environment.SetEnvironmentVariable("ACTIONS_RUNNER_INPUT_NAME", "some runner");
var command = new CommandSettings(hc, args: new string[0]);

// Act.
string actual = command.GetAgentName();
string actual = command.GetRunnerName();

// Assert.
Assert.Equal("some agent", actual);
Assert.Equal(string.Empty, Environment.GetEnvironmentVariable("ACTIONS_RUNNER_INPUT_AGENT") ?? string.Empty); // Should remove.
Assert.Equal("some agent", hc.SecretMasker.MaskSecrets("some agent"));
Assert.Equal("some runner", actual);
Assert.Equal(string.Empty, Environment.GetEnvironmentVariable("ACTIONS_RUNNER_INPUT_NAME") ?? string.Empty); // Should remove.
Assert.Equal("some runner", hc.SecretMasker.MaskSecrets("some runner"));
}
finally
{
Environment.SetEnvironmentVariable("ACTIONS_RUNNER_INPUT_AGENT", null);
Environment.SetEnvironmentVariable("ACTIONS_RUNNER_INPUT_NAME", null);
}
}
}
Expand Down Expand Up @@ -314,46 +312,46 @@ public void PassesUnattendedToReadValue()
var command = new CommandSettings(hc, args: new string[] { "--unattended" });
_promptManager
.Setup(x => x.ReadValue(
Constants.Runner.CommandLine.Args.Agent, // argName
Constants.Runner.CommandLine.Args.Name, // argName
"Enter the name of runner:", // description
false, // secret
Environment.MachineName, // defaultValue
Validators.NonEmptyValidator, // validator
true)) // unattended
.Returns("some agent");
.Returns("some runner");

// Act.
string actual = command.GetAgentName();
string actual = command.GetRunnerName();

// Assert.
Assert.Equal("some agent", actual);
Assert.Equal("some runner", actual);
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", nameof(CommandSettings))]
public void PromptsForAgent()
public void PromptsForRunnerName()
{
using (TestHostContext hc = CreateTestContext())
{
// Arrange.
var command = new CommandSettings(hc, args: new string[0]);
_promptManager
.Setup(x => x.ReadValue(
Constants.Runner.CommandLine.Args.Agent, // argName
Constants.Runner.CommandLine.Args.Name, // argName
"Enter the name of runner:", // description
false, // secret
Environment.MachineName, // defaultValue
Validators.NonEmptyValidator, // validator
false)) // unattended
.Returns("some agent");
.Returns("some runner");

// Act.
string actual = command.GetAgentName();
string actual = command.GetRunnerName();

// Assert.
Assert.Equal("some agent", actual);
Assert.Equal("some runner", actual);
}
}

Expand Down Expand Up @@ -766,8 +764,8 @@ public void ValidateGoodCommandline()
args: new string[] {
"configure",
"--unattended",
"--agent",
"test agent" });
"--name",
"test runner" });

// Assert.
Assert.True(command.Validate().Count == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public async Task CanEnsureConfigure()
"--acceptteeeula",
#endif
"--url", _expectedServerUrl,
"--agent", _expectedAgentName,
"--name", _expectedAgentName,
"--pool", _expectedPoolName,
"--work", _expectedWorkFolder,
"--auth", _expectedAuthType,
Expand Down

0 comments on commit a3c6a8c

Please sign in to comment.