Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.lib.util; |
| 15 | |
lberki | 78cfa8d | 2017-05-30 17:00:48 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.devtools.build.lib.shell.Command; |
| 21 | import com.google.devtools.build.lib.shell.CommandException; |
lberki | 78cfa8d | 2017-05-30 17:00:48 +0200 | [diff] [blame] | 22 | import java.io.File; |
| 23 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | @RunWith(JUnit4.class) |
| 29 | public 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()); |
lberki | 78cfa8d | 2017-05-30 17:00:48 +0200 | [diff] [blame] | 48 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | } |
| 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); |
lberki | 78cfa8d | 2017-05-30 17:00:48 +0200 | [diff] [blame] | 87 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |