annotate test/org/tmatesoft/hg/test/ExecHelper.java @ 203:66fd2c73c56f

Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 26 Apr 2011 15:52:33 +0200
parents a736f42ed75b
children b015f3918120
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;
203
66fd2c73c56f Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 201
diff changeset
36 private File dir;
201
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
37 private int exitValue;
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
38
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 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
40 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
41 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
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
fac8e7fcc8b0 Simple test framework - capable of parsing Hg cmdline output to compare with Java result
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 }
111
32e794c599d7 Reminder comment why exe check goes first
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 110
diff changeset
53 // PATHEXT controls precedence of .exe, .bat and .cmd files, ususlly .exe wins
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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
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 }
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 }
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 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
65 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
66 }
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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 }
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 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
78 } 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
79 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
80 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
81 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
82 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
83 }
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 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
85 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
86 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
87 }
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 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
89 p.waitFor();
201
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
90 exitValue = p.exitValue();
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
91 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
92 }
201
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
93
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
94 public int getExitValue() {
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
95 return exitValue;
a736f42ed75b Primitive test for clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 111
diff changeset
96 }
203
66fd2c73c56f Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 201
diff changeset
97
66fd2c73c56f Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 201
diff changeset
98 public void cwd(File wd) {
66fd2c73c56f Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 201
diff changeset
99 dir = wd;
66fd2c73c56f Basic test for HgOutgoingCommand. Handle cases with no outgoing changes in RepositoryComparator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 201
diff changeset
100 }
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
101 }