|
| 1 | +# Copyright 2020, 2021 Google LLC |
| 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 | +# https://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 | +"""Binary wrap is a test helper program for //elisp:binary_test, which see.""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import json |
| 19 | +import os |
| 20 | +import pathlib |
| 21 | +import unittest |
| 22 | +import sys |
| 23 | +from typing import Any, Dict, List |
| 24 | + |
| 25 | +from phst_rules_elisp.elisp import runfiles |
| 26 | + |
| 27 | +def _main() -> None: |
| 28 | + print('Args:', sys.argv) |
| 29 | + print('Environment:', os.environ) |
| 30 | + parser = argparse.ArgumentParser(allow_abbrev=False) |
| 31 | + parser.add_argument('--manifest', type=pathlib.Path, required=True) |
| 32 | + parser.add_argument('rest', nargs='+') |
| 33 | + args = parser.parse_args() |
| 34 | + run_files = runfiles.Runfiles() |
| 35 | + output_file = pathlib.PurePath( |
| 36 | + r'C:\Temp\output.dat' if os.name == 'nt' else '/tmp/output.dat') |
| 37 | + |
| 38 | + class Test(unittest.TestCase): |
| 39 | + """Unit tests for the command line and manifest.""" |
| 40 | + |
| 41 | + maxDiff = 5000 |
| 42 | + |
| 43 | + def test_args(self) -> None: |
| 44 | + """Test that the Emacs command line is as expected.""" |
| 45 | + got = args.rest |
| 46 | + want = ['--quick', '--batch'] |
| 47 | + # The load path setup depends on whether we use manifest-based or |
| 48 | + # directory-based runfiles. |
| 49 | + try: |
| 50 | + directory = run_files.resolve( |
| 51 | + pathlib.PurePosixPath('phst_rules_elisp')) |
| 52 | + except FileNotFoundError: |
| 53 | + # Manifest-based runfiles. |
| 54 | + want += [ |
| 55 | + '--load=' + str(run_files.resolve(pathlib.PurePosixPath( |
| 56 | + 'phst_rules_elisp/elisp/runfiles/runfiles.elc'))), |
| 57 | + '--funcall=elisp/runfiles/install-handler', |
| 58 | + '--directory=/bazel-runfile:phst_rules_elisp', |
| 59 | + ] |
| 60 | + else: |
| 61 | + # Directory-based runfiles. |
| 62 | + want.append('--directory=' + str(directory)) |
| 63 | + want += [ |
| 64 | + '--option', |
| 65 | + str(run_files.resolve(pathlib.PurePosixPath( |
| 66 | + 'phst_rules_elisp/elisp/binary.cc'))), |
| 67 | + ' \t\n\r\f äα𝐴🐈\'\\"', |
| 68 | + '/:' + str(output_file), |
| 69 | + ] |
| 70 | + self.assertListEqual(got, want) |
| 71 | + |
| 72 | + def test_manifest(self) -> None: |
| 73 | + """Test the manifest.""" |
| 74 | + got = json.loads(args.manifest.read_text(encoding='utf-8')) |
| 75 | + want = { |
| 76 | + 'root': 'RUNFILES_ROOT', |
| 77 | + 'tags': ['local', 'mytag'], |
| 78 | + 'loadPath': ['phst_rules_elisp'], |
| 79 | + 'inputFiles': ['phst_rules_elisp/elisp/binary.cc', |
| 80 | + 'phst_rules_elisp/elisp/binary.h'], |
| 81 | + 'outputFiles': [str(output_file)], |
| 82 | + } # type: Dict[str, Any] |
| 83 | + for var in (got, want): |
| 84 | + files = var.get('inputFiles', []) # type: List[str] |
| 85 | + for i, file in enumerate(files): |
| 86 | + file = pathlib.PurePosixPath(file) |
| 87 | + if not file.is_absolute(): |
| 88 | + files[i] = str(run_files.resolve(file)) |
| 89 | + self.assertDictEqual(got, want) |
| 90 | + |
| 91 | + tests = unittest.defaultTestLoader.loadTestsFromTestCase(Test) |
| 92 | + if not unittest.TextTestRunner().run(tests).wasSuccessful(): |
| 93 | + raise ValueError('incorrect arguments') |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + _main() |
0 commit comments