summaryrefslogtreecommitdiff
path: root/src/internal/WixBuildTools.TestSupport/ExternalExecutable.cs
blob: eb07aa132406dba415c15caf396d360456e986f6 (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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixBuildTools.TestSupport
{
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Text;

    public abstract class ExternalExecutable
    {
        private readonly string exePath;

        protected ExternalExecutable(string exePath)
        {
            this.exePath = exePath;
        }

        protected ExternalExecutableResult Run(string args, bool mergeErrorIntoOutput = false, string workingDirectory = null)
        {
            var startInfo = new ProcessStartInfo(this.exePath, args)
            {
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                WorkingDirectory = workingDirectory ?? Path.GetDirectoryName(this.exePath),
            };

            using (var process = Process.Start(startInfo))
            {
                // This implementation of merging the streams does not guarantee that lines are retrieved in the same order that they were written.
                // If the process is simultaneously writing to both streams, this is impossible to do anyway.
                var standardOutput = new ConcurrentQueue<string>();
                var standardError = mergeErrorIntoOutput ? standardOutput : new ConcurrentQueue<string>();

                process.ErrorDataReceived += (s, e) => { if (e.Data != null) { standardError.Enqueue(e.Data); } };
                process.OutputDataReceived += (s, e) => { if (e.Data != null) { standardOutput.Enqueue(e.Data); } };

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();

                process.WaitForExit();

                return new ExternalExecutableResult
                {
                    ExitCode = process.ExitCode,
                    StandardError = mergeErrorIntoOutput ? null : standardError.ToArray(),
                    StandardOutput = standardOutput.ToArray(),
                    StartInfo = startInfo,
                };
            }
        }

        // This is internal because it assumes backslashes aren't used as escape characters and there aren't any double quotes.
        internal static string CombineArguments(IEnumerable<string> arguments)
        {
            if (arguments == null)
            {
                return null;
            }

            var sb = new StringBuilder();

            foreach (var arg in arguments)
            {
                if (sb.Length > 0)
                {
                    sb.Append(' ');
                }

                if (arg.IndexOf(' ') > -1)
                {
                    sb.Append("\"");
                    sb.Append(arg);
                    sb.Append("\"");
                }
                else
                {
                    sb.Append(arg);
                }
            }

            return sb.ToString();
        }
    }
}