Skip to content

Commit

Permalink
split by regex (#2333)
Browse files Browse the repository at this point in the history
* split by regex

* pr fix

* adding tests

* test fix
  • Loading branch information
ruvceskistefan committed Dec 20, 2022
1 parent 29a28a8 commit 72830cf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Runner.Sdk/ProcessInvoker.cs
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Channels;
using System.Threading.Tasks;
using GitHub.Runner.Sdk;
using System.Text.RegularExpressions;

namespace GitHub.Runner.Sdk
{
Expand Down Expand Up @@ -265,8 +266,8 @@ public ProcessInvoker(ITraceWriter trace)
foreach (KeyValuePair<string, string> kvp in environment)
{
#if OS_WINDOWS
string tempKey = String.IsNullOrWhiteSpace(kvp.Key) ? kvp.Key : kvp.Key.Split('\0')[0];
string tempValue = String.IsNullOrWhiteSpace(kvp.Value) ? kvp.Value : kvp.Value.Split('\0')[0];
string tempKey = String.IsNullOrWhiteSpace(kvp.Key) ? kvp.Key : Regex.Split(kvp.Key, @"\p{C}")[0];
string tempValue = String.IsNullOrWhiteSpace(kvp.Value) ? kvp.Value : Regex.Split(kvp.Value, @"\p{C}")[0];
if(!String.IsNullOrWhiteSpace(tempKey))
{
_proc.StartInfo.Environment[tempKey] = tempValue;
Expand Down
68 changes: 68 additions & 0 deletions src/Test/L0/ProcessInvokerL0.cs
Expand Up @@ -164,6 +164,40 @@ public async Task SetTestEnvWithNullInKey()
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public async Task SetTestEnvWithTabInKey()
{
using (TestHostContext hc = new(this))
{
Tracing trace = hc.GetTrace();

Int32 exitCode = -1;
var processInvoker = new ProcessInvokerWrapper();
processInvoker.Initialize(hc);
var stdout = new List<string>();
var stderr = new List<string>();
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stdout.Add(e.Data);
};
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stderr.Add(e.Data);
};

exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %TEST%\"", new Dictionary<string, string>() { { "TEST\u0009second", "first" } }, CancellationToken.None);

trace.Info("Exit Code: {0}", exitCode);
Assert.Equal(0, exitCode);
Assert.Equal("first", stdout.First(x => !string.IsNullOrWhiteSpace(x)));

}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
Expand Down Expand Up @@ -197,6 +231,40 @@ public async Task SetTestEnvWithNullInValue()

}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public async Task SetTestEnvWithTabInValue()
{
using (TestHostContext hc = new(this))
{
Tracing trace = hc.GetTrace();

Int32 exitCode = -1;
var processInvoker = new ProcessInvokerWrapper();
processInvoker.Initialize(hc);
var stdout = new List<string>();
var stderr = new List<string>();
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stdout.Add(e.Data);
};
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
{
trace.Info(e.Data);
stderr.Add(e.Data);
};

exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %TEST%\"", new Dictionary<string, string>() { { "TEST", "first\u0009second" } }, CancellationToken.None);

trace.Info("Exit Code: {0}", exitCode);
Assert.Equal(0, exitCode);
Assert.Equal("first", stdout.First(x => !string.IsNullOrWhiteSpace(x)));

}
}
#endif
[Fact]
[Trait("Level", "L0")]
Expand Down

0 comments on commit 72830cf

Please sign in to comment.