blob: deabeb4668c50b64fc000521b53e8ff492235869 [file] [log] [blame]
David Ostrovsky2536d062013-11-14 00:35:07 +01001// Copyright (C) 2013 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import static org.junit.Assert.assertTrue;
16
17import com.google.common.io.ByteStreams;
18import com.google.common.base.Splitter;
19
20import java.io.File;
21import java.io.IOException;
22import java.io.InputStream;
23import org.junit.Test;
24
25public class PythonTestCaller {
26
27 @Test
28 public void resolveUrl() throws Exception {
29 PythonTestCaller.pythonUnit("tools", "util_test");
30 }
31
32 private static void pythonUnit(String d, String sut) throws Exception {
33 ProcessBuilder b =
34 new ProcessBuilder(Splitter.on(' ').splitToList(
35 "python -m unittest " + sut))
36 .directory(new File(d))
37 .redirectErrorStream(true);
38 Process p = null;
39 InputStream i = null;
40 byte[] out;
41 try {
42 p = b.start();
43 i = p.getInputStream();
44 out = ByteStreams.toByteArray(i);
45 } catch (IOException e) {
46 throw new Exception(e);
47 } finally {
48 if (p != null) {
49 p.getOutputStream().close();
50 }
51 if (i != null) {
52 i.close();
53 }
54 }
55 int value;
56 try {
57 value = p.waitFor();
58 } catch (InterruptedException e) {
59 throw new Exception("interrupted waiting for process");
60 }
61 String err = new String(out, "UTF-8");
62 if (value != 0) {
63 System.err.print(err);
64 }
65 assertTrue(err, value == 0);
66 }
67}