Add native process management for Windows and its Java bindings (without a sane Java API for now)
--
MOS_MIGRATED_REVID=126306559
diff --git a/src/test/java/com/google/devtools/build/lib/windows/MockSubprocess.java b/src/test/java/com/google/devtools/build/lib/windows/MockSubprocess.java
new file mode 100644
index 0000000..7e2d90c
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/windows/MockSubprocess.java
@@ -0,0 +1,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<register><count></code>: Read count bytes to the specified register
+ * <li><code>O-<string></code>: Write a string to stdout</li>
+ * <li><code>E-<string></code>: Write a string to stderr</li>
+ * <li><code>O$<variable></code>: Write an environment variable to stdout</li>
+ * <li><code>E$<variable></code>: Write an environment variable to stderr</li>
+ * <li><code>O<register></code>: Write the contents of a register to stdout</li>
+ * <li><code>E<register></code>: Write the contents of a register to stderr</li>
+ * <li><code>X<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)));
+ }
+ }
+ }
+}