-
Notifications
You must be signed in to change notification settings - Fork 872
/
ProcessExtensionL0.cs
70 lines (64 loc) · 2.34 KB
/
ProcessExtensionL0.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Xunit;
namespace GitHub.Runner.Common.Tests
{
public sealed class ProcessExtensionL0
{
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public async Task SuccessReadProcessEnv()
{
using (TestHostContext hc = new TestHostContext(this))
{
Tracing trace = hc.GetTrace();
string envName = Guid.NewGuid().ToString();
string envValue = Guid.NewGuid().ToString();
Process sleep = null;
try
{
#if OS_WINDOWS
string node = Path.Combine(TestUtil.GetSrcPath(), @"..\_layout\externals\node16\bin\node");
#else
string node = Path.Combine(TestUtil.GetSrcPath(), @"../_layout/externals/node16/bin/node");
hc.EnqueueInstance<IProcessInvoker>(new ProcessInvokerWrapper());
#endif
var startInfo = new ProcessStartInfo(node, "-e \"setTimeout(function(){{}}, 15 * 1000);\"");
startInfo.Environment[envName] = envValue;
sleep = Process.Start(startInfo);
var timeout = Process.GetProcessById(sleep.Id);
while (timeout == null)
{
await Task.Delay(500);
timeout = Process.GetProcessById(sleep.Id);
}
try
{
trace.Info($"Read env from {timeout.Id}");
var value = timeout.GetEnvironmentVariable(hc, envName);
if (string.Equals(value, envValue, StringComparison.OrdinalIgnoreCase))
{
trace.Info($"Find the env.");
return;
}
}
catch (Exception ex)
{
trace.Error(ex);
}
Assert.True(false, "Fail to retrive process environment variable.");
}
finally
{
sleep?.Kill();
}
}
}
}
}