aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/windows/MockSubprocess.java
blob: 7e2d90cef6cbeb463e137783bd7042185e56a4e6 (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
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.windows;

import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * Mock subprocess to be used for testing Windows process management. Command line usage:
 *
 * <ul>
 *   <li><code>I&lt;register&gt;&lt;count&gt;</code>: Read count bytes to the specified register
 *   <li><code>O-&lt;string&gt;</code>: Write a string to stdout</li>
 *   <li><code>E-&lt;string&gt;</code>: Write a string to stderr</li>
 *   <li><code>O$&lt;variable&gt;</code>: Write an environment variable to stdout</li>
 *   <li><code>E$&lt;variable&gt;</code>: Write an environment variable to stderr</li>
 *   <li><code>O&lt;register&gt;</code>: Write the contents of a register to stdout</li>
 *   <li><code>E&lt;register&gt;</code>: Write the contents of a register to stderr</li>
 *   <li><code>X&lt;exit code%gt;</code>: Exit with the specified exit code</li>
 * </ul>
 *
 * <p>Registers are single characters. Each command line argument is interpreted as a single
 * operation. Example:
 *
 * <code>
 *   Ia10 Oa Oa Ea E-OVER X42
 * </code>
 *
 * Means: read 10 bytes from stdin, write them back twice to stdout and once to stderr, write
 * the string "OVER" to stderr then exit with exit code 42.
 */
public class MockSubprocess {
  private static Map<Character, byte[]> registers = new HashMap<>();

  private static void writeBytes(PrintStream stream, String arg) throws Exception {
    byte[] buf;
    switch (arg.charAt(1)) {
      case '-':
        // Immediate string
        buf = arg.substring(2).getBytes(Charset.forName("UTF-8"));
        break;

      case '$':
        // Environment variable
        buf = System.getenv(arg.substring(2)).getBytes(Charset.forName("UTF-8"));
        break;

      default:
        buf = registers.get(arg.charAt(1));
        break;
    }

    stream.write(buf, 0, buf.length);
}

  public static void main(String[] args) throws Exception {
    for (String arg : args) {
      switch (arg.charAt(0)) {
        case 'I':
          char register = arg.charAt(1);
          int length = Integer.parseInt(arg.substring(2));
          byte[] buf = new byte[length];
          registers.put(register, buf);
          System.in.read(buf, 0, length);
          break;

        case 'E':
          writeBytes(System.err, arg);
          break;

        case 'O':
          writeBytes(System.out, arg);
          break;

        case 'X':
          System.exit(Integer.parseInt(arg.substring(1)));
      }
    }
  }
}