chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using NUnit.Framework;
using OpenTap.Cli;
using OpenTap.Plugins.BasicSteps;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
 
namespace OpenTap.UnitTests
{
    [Display("print", Groups: new[] { "test", "envvariables" }, Description: "Prints environment variables.")]
    public class PrintEnvVarAction : ICliAction
    {
        public int Execute(CancellationToken cancellationToken)
        {
            var log = Log.CreateSource("Environment variables");
            var variables = Environment.GetEnvironmentVariables();
            log.Info("Environment variables:");
            foreach (DictionaryEntry variable in variables)
            {
                log.Info($"\t{variable.Key} = {variable.Value}");
            }
 
            return (int)ExitCodes.Success;
        }
    }
    
    [Display("fail", Groups: new[] { "test" }, Description: "Fails a cli action")]
    public class FailCliAction : ICliAction
    {
        [CommandLineArgument("error")]
        public int ExitCode { get; set; } = 1;
        public int Execute(CancellationToken cancellationToken)
        {
            var log = Log.CreateSource("cli");
            log.Info("Failing with exit code {0}", ExitCode);
           
            return ExitCode;
        }
    }
 
    [TestFixture]
    public class ProcessStepTest
    {
        // Test single env variable.
        [TestCase(Verdict.Pass, "Ping = Pong", "Ping=Pong")]
        // Test multiple env variables.
        [TestCase(Verdict.Pass, "Ping = Pong", "Ping=Pong", "Test=test123")]
        [TestCase(Verdict.Pass, "Test = test123", "Ping=Pong", "Test=test123")]
        [TestCase(Verdict.Pass, "(Ping = Pong|Test = test123)", "Ping=Pong", "Test=test123")]
        // Test duplicate environment variable.
        [TestCase(Verdict.Error, "", "Ping=Pong", "Ping=Pong")]
        [TestCase(Verdict.Error, "", "Ping=Pong", "Ping=Ping")]
        public void ProcessStepSetEnvironmentVariables(Verdict expectedVerdict, string regex, params string[] variables)
        {
            var plan = new TestPlan();
            var processStep = new ProcessStep()
            {
                Application = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "tap.exe"),
                WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                Arguments = "test envvariables print",
                RegularExpressionPattern = new Enabled<string>()
                {
                    IsEnabled = true,
                    Value = regex,
                },
            };
            foreach (var variable in variables)
            {
                string[] strs = variable.Split('=');
                processStep.EnvironmentVariables.Add(new ProcessStep.EnvironmentVariable { Name = strs[0], Value = strs[1] });
            }
            plan.Steps.Add(processStep);
 
            var result = plan.Execute();
            Assert.AreEqual(expectedVerdict, result.Verdict);
        }
 
        [Test]
        public void ProcessStepOutputs()
        {
            int exitCode = 5;
            var plan = new TestPlan();
            var processStep = new ProcessStep()
            {
                Application = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "tap.exe"),
                WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                Arguments = $"test fail --error {exitCode}",
            };
            plan.Steps.Add(processStep);
            var result = plan.Execute();
            Assert.AreEqual(exitCode, processStep.ExitCode);
            Assert.IsTrue(processStep.Output.Contains($"Failing with exit code {exitCode}"));
        }
 
        [Test]
        public void ProcessStepTimeoutTest()
        {
            var plan = new TestPlan();
            var processStep = new ProcessStep()
            {
                Application = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "tap.exe"),
                WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                Arguments = $"test fail",
                Timeout = 50, // ms
                WaitForEnd = true,
            };
            plan.ChildTestSteps.Add(processStep);
            var rl = new RecordAllResultListener();
            var planRun = plan.Execute([rl]);
            Assert.AreEqual(Verdict.Fail, planRun.Verdict);
            foreach (var line in rl.planLogs.First().Value.Split('\n'))
            {
                Assert.IsTrue(line.Split(';').ElementAtOrDefault(2)?.Trim() != "Error");
            }
            Assert.IsTrue(rl.planLogs.First().Value.Contains("Timed out while waiting for process to end"));
        }
 
        [Test]
        public void ProcessStepPass()
        {
            var plan = new TestPlan();
            var processStep = new ProcessStep()
            {
                Application = "tap.exe",
                WorkingDirectory = "",
                Arguments = $"test fail --error 0",
                Timeout = 50000, // ms
                WaitForEnd = true,
                CheckExitCode = true
            };
            plan.ChildTestSteps.Add(processStep);
            var planRun = plan.Execute();
            Assert.AreEqual(Verdict.Pass, planRun.Verdict);
        }
    }
}