annotate test/org/tmatesoft/hg/test/ExecHelper.java @ 110:0170f95ca915

On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 31 Jan 2011 19:42:19 +0100
parents a3a2e5deb320
children 32e794c599d7
rev   line source
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
3 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
4 * This program is free software; you can redistribute it and/or modify
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
5 * it under the terms of the GNU General Public License as published by
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
6 * the Free Software Foundation; version 2 of the License.
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
7 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
8 * This program is distributed in the hope that it will be useful,
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
11 * GNU General Public License for more details.
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
12 *
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
13 * For information on how to redistribute this software under
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 66
diff changeset
15 * contact TMate Software at support@hg4j.com
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
17 package org.tmatesoft.hg.test;
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.File;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.IOException;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.InputStreamReader;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.nio.CharBuffer;
110
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
23 import java.util.ArrayList;
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
24 import java.util.Arrays;
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.util.LinkedList;
110
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
26 import java.util.StringTokenizer;
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 /**
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 *
66
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
30 * @author Artem Tikhomirov
52dc3f4cfc76 Primitive test suite in org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 61
diff changeset
31 * @author TMate Software Ltd.
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 */
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 public class ExecHelper {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 private final OutputParser parser;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 private final File dir;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public ExecHelper(OutputParser outParser, File workingDir) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 parser = outParser;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 dir = workingDir;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 public void run(String... cmd) throws IOException, InterruptedException {
110
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
44 ProcessBuilder pb = null;
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
45 if (System.getProperty("os.name").startsWith("Windows")) {
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
46 StringTokenizer st = new StringTokenizer(System.getenv("PATH"), ";");
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
47 while (st.hasMoreTokens()) {
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
48 File pe = new File(st.nextToken());
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
49 if (new File(pe, cmd[0] + ".exe").exists()) {
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
50 break;
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
51 }
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
52 if (new File(pe, cmd[0] + ".bat").exists() || new File(pe, cmd[0] + ".cmd").exists()) {
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
53 ArrayList<String> command = new ArrayList<String>();
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
54 command.add("cmd.exe");
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
55 command.add("/C");
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
56 command.addAll(Arrays.asList(cmd));
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
57 pb = new ProcessBuilder(command);
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
58 break;
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
59 }
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
60 }
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
61 }
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
62 if (pb == null) {
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
63 pb = new ProcessBuilder(cmd);
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
64 }
0170f95ca915 On Windows, if hg.exe is wrapped into batch file, need to use cmd.exe to let it run
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
65 Process p = pb.directory(dir).redirectErrorStream(true).start();
61
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 InputStreamReader stdOut = new InputStreamReader(p.getInputStream());
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 int r = -1;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 CharBuffer b = null;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 do {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 if (b == null || b.remaining() < b.capacity() / 3) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 b = CharBuffer.allocate(512);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 l.add(b);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 r = stdOut.read(b);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 } while (r != -1);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 int total = 0;
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 for (CharBuffer cb : l) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 total += cb.position();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 cb.flip();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 CharBuffer res = CharBuffer.allocate(total);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 for (CharBuffer cb : l) {
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 res.put(cb);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 res.flip();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 p.waitFor();
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 parser.parse(res);
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 }
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 }