Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Bazel Authors. All rights reserved. |
| 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 | |
| 15 | """Unit tests for shell.bzl.""" |
| 16 | |
Thomas Van Lenten | b5f4086 | 2018-08-24 15:00:13 -0400 | [diff] [blame] | 17 | load("//lib:shell.bzl", "shell") |
| 18 | load("//lib:unittest.bzl", "asserts", "unittest") |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 19 | |
| 20 | def _shell_array_literal_test(ctx): |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 21 | """Unit tests for shell.array_literal.""" |
| 22 | env = unittest.begin(ctx) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 23 | |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 24 | asserts.equals(env, "()", shell.array_literal([])) |
| 25 | asserts.equals(env, "('1')", shell.array_literal([1])) |
| 26 | asserts.equals(env, "('1' '2' '3')", shell.array_literal([1, 2, 3])) |
| 27 | asserts.equals(env, "('$foo')", shell.array_literal(["$foo"])) |
| 28 | asserts.equals(env, "('qu\"o\"te')", shell.array_literal(['qu"o"te'])) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 29 | |
László Csomor | daf5137 | 2018-12-04 16:14:08 +0100 | [diff] [blame] | 30 | return unittest.end(env) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 31 | |
| 32 | shell_array_literal_test = unittest.make(_shell_array_literal_test) |
| 33 | |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 34 | def _shell_quote_test(ctx): |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 35 | """Unit tests for shell.quote.""" |
| 36 | env = unittest.begin(ctx) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 37 | |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 38 | asserts.equals(env, "'foo'", shell.quote("foo")) |
| 39 | asserts.equals(env, "'foo bar'", shell.quote("foo bar")) |
| 40 | asserts.equals(env, "'three spaces'", shell.quote("three spaces")) |
| 41 | asserts.equals(env, "' leading'", shell.quote(" leading")) |
| 42 | asserts.equals(env, "'trailing '", shell.quote("trailing ")) |
| 43 | asserts.equals(env, "'new\nline'", shell.quote("new\nline")) |
| 44 | asserts.equals(env, "'tab\tcharacter'", shell.quote("tab\tcharacter")) |
| 45 | asserts.equals(env, "'$foo'", shell.quote("$foo")) |
| 46 | asserts.equals(env, "'qu\"o\"te'", shell.quote('qu"o"te')) |
| 47 | asserts.equals(env, "'it'\\''s'", shell.quote("it's")) |
| 48 | asserts.equals(env, "'foo\\bar'", shell.quote("foo\\bar")) |
| 49 | asserts.equals(env, "'back`echo q`uote'", shell.quote("back`echo q`uote")) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 50 | |
László Csomor | daf5137 | 2018-12-04 16:14:08 +0100 | [diff] [blame] | 51 | return unittest.end(env) |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 52 | |
| 53 | shell_quote_test = unittest.make(_shell_quote_test) |
| 54 | |
László Csomor | 4c26bf4 | 2019-03-26 10:52:27 +0100 | [diff] [blame] | 55 | def _shell_args_test_gen_impl(ctx): |
| 56 | """Test argument escaping: this rule writes a script for a sh_test.""" |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 57 | args = [ |
| 58 | "foo", |
| 59 | "foo bar", |
| 60 | "three spaces", |
| 61 | " leading", |
| 62 | "trailing ", |
| 63 | "new\nline", |
| 64 | "tab\tcharacter", |
| 65 | "$foo", |
| 66 | 'qu"o"te', |
| 67 | "it's", |
| 68 | "foo\\bar", |
| 69 | "back`echo q`uote", |
| 70 | ] |
| 71 | script_content = "\n".join([ |
Yesudeep Mangalapilly | 8e923ca | 2021-10-25 06:12:41 -0700 | [diff] [blame] | 72 | "#!/usr/bin/env bash", |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 73 | "myarray=" + shell.array_literal(args), |
| 74 | 'output=$(echo "${myarray[@]}")', |
| 75 | # For logging: |
| 76 | 'echo "DEBUG: output=[${output}]" >&2', |
| 77 | # The following is a shell representation of what the echo of the quoted |
| 78 | # array will look like. It looks a bit confusing considering it's shell |
| 79 | # quoted into Python. Shell using single quotes to minimize shell |
| 80 | # escaping, so only the single quote needs to be escaped as '\'', all |
| 81 | # others are essentially kept literally. |
| 82 | "expected='foo foo bar three spaces leading trailing new", |
| 83 | "line tab\tcharacter $foo qu\"o\"te it'\\''s foo\\bar back`echo q`uote'", |
| 84 | '[[ "${output}" == "${expected}" ]]', |
| 85 | ]) |
László Csomor | 4c26bf4 | 2019-03-26 10:52:27 +0100 | [diff] [blame] | 86 | out = ctx.actions.declare_file(ctx.label.name + ".sh") |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 87 | ctx.actions.write( |
László Csomor | 4c26bf4 | 2019-03-26 10:52:27 +0100 | [diff] [blame] | 88 | output = out, |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 89 | content = script_content, |
| 90 | is_executable = True, |
| 91 | ) |
László Csomor | 4c26bf4 | 2019-03-26 10:52:27 +0100 | [diff] [blame] | 92 | return [DefaultInfo(files = depset([out]))] |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 93 | |
László Csomor | 4c26bf4 | 2019-03-26 10:52:27 +0100 | [diff] [blame] | 94 | shell_args_test_gen = rule( |
| 95 | implementation = _shell_args_test_gen_impl, |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 96 | ) |
| 97 | |
Tony Allevato | 82b3ad6 | 2017-10-10 07:59:31 -0700 | [diff] [blame] | 98 | def shell_test_suite(): |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 99 | """Creates the test targets and test suite for shell.bzl tests.""" |
| 100 | unittest.suite( |
| 101 | "shell_tests", |
| 102 | shell_array_literal_test, |
| 103 | shell_quote_test, |
Thomas Van Lenten | e5203c0 | 2018-06-12 13:09:57 -0400 | [diff] [blame] | 104 | ) |