blob: fde8fb71ee147cf7c22fc16f8b9a532f74dda9ff [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.util;
15
lberki78cfa8d2017-05-30 17:00:48 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import static org.junit.Assert.fail;
18
19import com.google.common.collect.Maps;
20import com.google.devtools.build.lib.shell.Command;
21import com.google.devtools.build.lib.shell.CommandException;
lberki78cfa8d2017-05-30 17:00:48 +020022import java.io.File;
23import java.util.Map;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028@RunWith(JUnit4.class)
29public class CommandUtilsTest {
30
31 @Test
32 public void longCommand() throws Exception {
33 String[] args = new String[40];
34 args[0] = "this_command_will_not_be_found";
35 for (int i = 1; i < args.length; i++) {
36 args[i] = "arg" + i;
37 }
38 Map<String, String> env = Maps.newTreeMap();
39 env.put("PATH", "/usr/bin:/bin:/sbin");
40 env.put("FOO", "foo");
41 File directory = new File("/tmp");
42 try {
43 new Command(args, env, directory).execute();
44 fail();
45 } catch (CommandException exception) {
46 String message = CommandUtils.describeCommandError(false, exception.getCommand());
47 String verboseMessage = CommandUtils.describeCommandError(true, exception.getCommand());
lberki78cfa8d2017-05-30 17:00:48 +020048 assertThat(message)
49 .isEqualTo(
50 "error executing command this_command_will_not_be_found arg1 "
51 + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 "
52 + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 "
53 + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 "
54 + "arg27 arg28 arg29 arg30 "
55 + "... (remaining 9 argument(s) skipped)");
56 assertThat(verboseMessage)
57 .isEqualTo(
58 "error executing command \n"
59 + " (cd /tmp && \\\n"
60 + " exec env - \\\n"
61 + " FOO=foo \\\n"
62 + " PATH=/usr/bin:/bin:/sbin \\\n"
63 + " this_command_will_not_be_found arg1 "
64 + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 "
65 + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 "
66 + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 "
67 + "arg27 arg28 arg29 arg30 arg31 arg32 arg33 arg34 "
68 + "arg35 arg36 arg37 arg38 arg39)");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069 }
70 }
71
72 @Test
73 public void failingCommand() throws Exception {
74 String[] args = new String[3];
75 args[0] = "/bin/sh";
76 args[1] = "-c";
77 args[2] = "echo Some errors 1>&2; echo Some output; exit 42";
78 Map<String, String> env = Maps.newTreeMap();
79 env.put("FOO", "foo");
80 env.put("PATH", "/usr/bin:/bin:/sbin");
81 try {
82 new Command(args, env, null).execute();
83 fail();
84 } catch (CommandException exception) {
85 String message = CommandUtils.describeCommandFailure(false, exception);
86 String verboseMessage = CommandUtils.describeCommandFailure(true, exception);
lberki78cfa8d2017-05-30 17:00:48 +020087 assertThat(message)
88 .isEqualTo(
89 "sh failed: error executing command "
90 + "/bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42': "
91 + "Process exited with status 42\n"
92 + "Some output\n"
93 + "Some errors\n");
94 assertThat(verboseMessage)
95 .isEqualTo(
96 "sh failed: error executing command \n"
97 + " (exec env - \\\n"
98 + " FOO=foo \\\n"
99 + " PATH=/usr/bin:/bin:/sbin \\\n"
100 + " /bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42'): "
101 + "Process exited with status 42\n"
102 + "Some output\n"
103 + "Some errors\n");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100104 }
105 }
106}