Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CommandRunner.cs « ILLink.Tasks.Tests « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc790c09aa069302cf861243c4092beb93c7398c (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Xunit;
using Xunit.Abstractions;

namespace ILLink.Tests
{

	public class CommandHelper
	{
		private ILogger logger;

		public CommandHelper(ILogger logger)
		{
			this.logger = logger;
		}

		public int Dotnet(string args, string workingDir, string additionalPath = null)
		{
			return RunCommand(Path.GetFullPath(TestContext.DotnetToolPath), args,
				workingDir, additionalPath, out string commandOutput);
		}

		public int RunCommand(string command, string args, int timeout = Int32.MaxValue)
		{
			return RunCommand(command, args, null, null, out string commandOutput, timeout);
		}

		public int RunCommand(string command, string args, string workingDir)
		{
			return RunCommand(command, args, workingDir, null, out string commandOutput);
		}

		public int RunCommand(string command, string args, string workingDir, string additionalPath,
			out string commandOutput, int timeout = Int32.MaxValue, string terminatingOutput = null)
		{
			return (new CommandRunner(command, logger))
				.WithArguments(args)
				.WithWorkingDir(workingDir)
				.WithAdditionalPath(additionalPath)
				.WithTimeout(timeout)
				.WithTerminatingOutput(terminatingOutput)
				.Run(out commandOutput);
		}
	}

	public class CommandRunner
	{
		private readonly ILogger logger;

		private string command;
		private string args;
		private string workingDir;
		private string additionalPath;
		private int timeout = Int32.MaxValue;
		private string terminatingOutput;

		private void LogMessage (string message)
		{
			logger.LogMessage (message);
		}

		public CommandRunner(string command, ILogger logger) {
			this.command = command;
			this.logger = logger;
		}

		public CommandRunner WithArguments(string args) {
			this.args = args;
			return this;
		}

		public CommandRunner WithWorkingDir(string workingDir) {
			this.workingDir = workingDir;
			return this;
		}

		public CommandRunner WithAdditionalPath(string additionalPath) {
			this.additionalPath = additionalPath;
			return this;
		}

		public CommandRunner WithTimeout(int timeout) {
			this.timeout = timeout;
			return this;
		}

		public CommandRunner WithTerminatingOutput(string terminatingOutput) {
			this.terminatingOutput = terminatingOutput;
			return this;
		}

		public int Run()
		{
			return Run(out string commandOutputUnused);
		}

		public int Run(out string commandOutput)
		{
			if (String.IsNullOrEmpty(command)) {
				throw new Exception("No command was specified specified.");
			}
			if (logger == null) {
				throw new Exception("No logger present.");
			}
			var psi = new ProcessStartInfo
			{
				FileName = command,
				UseShellExecute = false,
				RedirectStandardOutput = true,
				RedirectStandardError = true,
			};
			LogMessage ($"caller working directory: {Environment.CurrentDirectory}");
			if (!String.IsNullOrEmpty(args)) {
				psi.Arguments = args;
				LogMessage ($"{command} {args}");
			} else {
				LogMessage ($"{command}");
			}
			if (!String.IsNullOrEmpty(workingDir)) {
				LogMessage ($"working directory: {workingDir}");
				psi.WorkingDirectory = workingDir;
			}
			if (!String.IsNullOrEmpty(additionalPath)) {
				string path = psi.Environment["PATH"];
				psi.Environment["PATH"] = path + ";" + additionalPath;
			}
			var process = new Process();
			process.StartInfo = psi;

			// dotnet sets some environment variables that
			// may cause problems in the child process.
			psi.Environment.Remove("MSBuildExtensionsPath");
			psi.Environment.Remove("MSBuildLoadMicrosoftTargetsReadOnly");
			psi.Environment.Remove("MSBuildSDKsPath");
			psi.Environment.Remove("VbcToolExe");
			psi.Environment.Remove("CscToolExe");
			psi.Environment.Remove("MSBUILD_EXE_PATH");

			LogMessage ("environment:");
			foreach (var item in psi.Environment) {
				LogMessage ($"\t{item.Key}={item.Value}");
			}

			StringBuilder processOutput = new StringBuilder();
			DataReceivedEventHandler handler = (sender, e) => {
				processOutput.Append(e.Data);
				processOutput.AppendLine();
			};
			StringBuilder processError = new StringBuilder();
			DataReceivedEventHandler ehandler = (sender, e) => {
				processError.Append(e.Data);
				processError.AppendLine();
			};
			process.OutputDataReceived += handler;
			process.ErrorDataReceived += ehandler;

			// terminate process if output contains specified string
			if (!String.IsNullOrEmpty(terminatingOutput)) {
				DataReceivedEventHandler terminatingOutputHandler = (sender, e) => {
					if (!String.IsNullOrEmpty(e.Data) && e.Data.Contains(terminatingOutput)) {
						process.Kill();
					}
				};
				process.OutputDataReceived += terminatingOutputHandler;
			}

			// start the process
			process.Start();
			process.BeginOutputReadLine();
			process.BeginErrorReadLine();
			if (!process.WaitForExit(timeout)) {
				LogMessage ($"killing process after {timeout} ms");
				process.Kill();
			}
			// WaitForExit with timeout doesn't guarantee
			// that the async output handlers have been
			// called, so WaitForExit needs to be called
			// afterwards.
			process.WaitForExit();
			string processOutputStr = processOutput.ToString();
			string processErrorStr = processError.ToString();
			LogMessage(processOutputStr);
			LogMessage(processErrorStr);
			commandOutput = processOutputStr;
			return process.ExitCode;
		}
	}
}