aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/util/CommandUtilsTest.java
blob: c7639a2be445707a1ee0b22d7947a10a09bf505b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2014 Google Inc. 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.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.common.collect.Maps;
import com.google.devtools.build.lib.shell.Command;
import com.google.devtools.build.lib.shell.CommandException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.util.Map;

@RunWith(JUnit4.class)
public class CommandUtilsTest {

  @Test
  public void longCommand() throws Exception {
    String[] args = new String[40];
    args[0] = "this_command_will_not_be_found";
    for (int i = 1; i < args.length; i++) {
      args[i] = "arg" + i;
    }
    Map<String, String> env = Maps.newTreeMap();
    env.put("PATH", "/usr/bin:/bin:/sbin");
    env.put("FOO", "foo");
    File directory = new File("/tmp");
    try {
      new Command(args, env, directory).execute();
      fail();
    } catch (CommandException exception) {
      String message = CommandUtils.describeCommandError(false, exception.getCommand());
      String verboseMessage = CommandUtils.describeCommandError(true, exception.getCommand());
      assertEquals(
          "error executing command this_command_will_not_be_found arg1 "
          + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 "
          + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 "
          + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 "
          + "arg27 arg28 arg29 arg30 "
          + "... (remaining 9 argument(s) skipped)",
          message);
      assertEquals(
          "error executing command \n"
          + "  (cd /tmp && \\\n"
          + "  exec env - \\\n"
          + "    FOO=foo \\\n"
          + "    PATH=/usr/bin:/bin:/sbin \\\n"
          + "  this_command_will_not_be_found arg1 "
          + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 "
          + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 "
          + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 "
          + "arg27 arg28 arg29 arg30 arg31 arg32 arg33 arg34 "
          + "arg35 arg36 arg37 arg38 arg39)",
          verboseMessage);
    }
  }

  @Test
  public void failingCommand() throws Exception {
    String[] args = new String[3];
    args[0] = "/bin/sh";
    args[1] = "-c";
    args[2] = "echo Some errors 1>&2; echo Some output; exit 42";
    Map<String, String> env = Maps.newTreeMap();
    env.put("FOO", "foo");
    env.put("PATH", "/usr/bin:/bin:/sbin");
    try {
      new Command(args, env, null).execute();
      fail();
    } catch (CommandException exception) {
      String message = CommandUtils.describeCommandFailure(false, exception);
      String verboseMessage = CommandUtils.describeCommandFailure(true, exception);
      assertEquals(
          "sh failed: error executing command " +
          "/bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42': " +
          "Process exited with status 42\n" +
          "Some output\n" +
          "Some errors\n",
          message);
      assertEquals(
          "sh failed: error executing command \n" +
          "  (exec env - \\\n" +
          "    FOO=foo \\\n" +
          "    PATH=/usr/bin:/bin:/sbin \\\n" +
          "  /bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42'): " +
          "Process exited with status 42\n" +
          "Some output\n" +
          "Some errors\n",
          verboseMessage);
    }
  }
}