Ejemplo n.º 1
0
        public int GetOutputStreams(string workingDirectory, string command, string param, out List <string> standardOut, out List <string> standardErr)
        {
            Process process = CreateProcess(workingDirectory, command, param);

            var localStandardOut = new List <string>();
            var localStandardErr = new List <string>();

            process.OutputDataReceived += (sender, e) => localStandardOut.Add(e.Data);
            process.ErrorDataReceived  += (sender, e) => localStandardErr.Add(e.Data);

            try
            {
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                var waiter = new ProcessWaiter(process);
                waiter.WaitForExit();

                standardOut = localStandardOut.Where(s => s != null).ToList();
                standardErr = localStandardErr.Where(s => s != null).ToList();
                return(waiter.ProcessExitCode);
            }
            finally
            {
                process.Dispose();
            }
        }
        public int ExecuteCommandBlocking(string command, string parameters, string workingDir, string pathExtension,
                                          Action <string> reportOutputLine)
        {
            if (reportOutputLine != null)
            {
                throw new ArgumentException(nameof(reportOutputLine));
            }
            if (_processId.HasValue)
            {
                throw new InvalidOperationException();
            }

            IDictionary <string, string> envVariables = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(pathExtension))
            {
                envVariables["PATH"] = Utils.GetExtendedPath(pathExtension);
            }

            _logger.DebugInfo($"Attaching debugger to '{command}' via {DebuggerKind.VsTestFramework} engine");
            if (_printTestOutput)
            {
                _logger.DebugInfo(
                    $"Note that due to restrictions of the VsTest framework, the test executable's output can not be displayed in the test console when debugging tests. Use '{SettingsWrapper.OptionDebuggerKind}' option to overcome this problem.'");
            }

            _processId = _frameworkHandle.LaunchProcessWithDebuggerAttached(command, workingDir, parameters, envVariables);

            ProcessWaiter waiter;

            using (var process = Process.GetProcessById(_processId.Value))
            {
                waiter = new ProcessWaiter(process);
                waiter.WaitForExit();
            }

            _logger.DebugInfo($"Executable {command} returned with exit code {waiter.ProcessExitCode}");
            return(waiter.ProcessExitCode);
        }