xref: /aosp_15_r20/build/bazel_common_rules/exec/tests/exec_test.py (revision 7887bec861e78e44e4e86ae7a52515235a00b778)
1# Copyright (C) 2022 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 argparse
16import sys
17import subprocess
18import unittest
19
20from absl.testing import absltest
21
22def load_arguments():
23    parser = argparse.ArgumentParser()
24    parser.add_argument("script")
25    return parser.parse_known_args()
26
27
28arguments = None
29
30
31class ExecruleTest(unittest.TestCase):
32    def test_no_args(self):
33        output = subprocess.check_output([arguments.script], text=True).strip()
34        self.assertEqual(output, """combined_args=
35script_a --argsA=valueA --args_expanded=build/bazel_common_rules/exec/tests/data.txt
36SCRIPT_B_ENV=env_value SCRIPT_B_ENV_EXPANDED=build/bazel_common_rules/exec/tests/data.txt script_b --script_b_arg=value --args_expanded=build/bazel_common_rules/exec/tests/data.txt
37text""")
38
39    def test_args(self):
40        output = subprocess.check_output([arguments.script, "--some_arg"], text=True).strip()
41        self.assertEqual(output, """combined_args=--some_arg
42script_a --argsA=valueA --args_expanded=build/bazel_common_rules/exec/tests/data.txt
43SCRIPT_B_ENV=env_value SCRIPT_B_ENV_EXPANDED=build/bazel_common_rules/exec/tests/data.txt script_b --script_b_arg=value --args_expanded=build/bazel_common_rules/exec/tests/data.txt
44text""")
45
46
47if __name__ == '__main__':
48    arguments, unknown = load_arguments()
49    sys.argv[1:] = unknown
50    absltest.main()
51