Maxim Cournoyer | 8f8d3f7 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright (c) 2022 Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com> |
| 4 | # |
| 5 | |
| 6 | import argparse |
| 7 | import contextlib |
| 8 | import os |
| 9 | import sys |
| 10 | import tempfile |
| 11 | |
| 12 | from patman import settings |
Simon Glass | 4583c00 | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 13 | from u_boot_pylib import tools |
Maxim Cournoyer | 8f8d3f7 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 14 | |
| 15 | |
| 16 | @contextlib.contextmanager |
| 17 | def empty_git_repository(): |
| 18 | with tempfile.TemporaryDirectory() as tmpdir: |
| 19 | os.chdir(tmpdir) |
| 20 | tools.run('git', 'init', raise_on_error=True) |
| 21 | yield tmpdir |
| 22 | |
| 23 | |
| 24 | @contextlib.contextmanager |
| 25 | def cleared_command_line_args(): |
| 26 | old_value = sys.argv[:] |
| 27 | sys.argv = [sys.argv[0]] |
| 28 | try: |
| 29 | yield |
| 30 | finally: |
| 31 | sys.argv = old_value |
| 32 | |
| 33 | |
| 34 | def test_git_local_config(): |
| 35 | # Clearing the command line arguments is required, otherwise |
| 36 | # arguments passed to the test running such as in 'pytest -k |
| 37 | # filter' would be processed by _UpdateDefaults and fail. |
| 38 | with cleared_command_line_args(): |
| 39 | with empty_git_repository(): |
| 40 | with tempfile.NamedTemporaryFile() as global_config: |
| 41 | global_config.write(b'[settings]\n' |
| 42 | b'project=u-boot\n') |
| 43 | global_config.flush() |
| 44 | parser = argparse.ArgumentParser() |
| 45 | parser.add_argument('-p', '--project', default='unknown') |
| 46 | subparsers = parser.add_subparsers(dest='cmd') |
| 47 | send = subparsers.add_parser('send') |
| 48 | send.add_argument('--no-check', action='store_false', |
| 49 | dest='check_patch', default=True) |
| 50 | |
| 51 | # Test "global" config is used. |
| 52 | settings.Setup(parser, 'unknown', global_config.name) |
| 53 | args, _ = parser.parse_known_args([]) |
| 54 | assert args.project == 'u-boot' |
| 55 | send_args, _ = send.parse_known_args([]) |
| 56 | assert send_args.check_patch |
| 57 | |
| 58 | # Test local config can shadow it. |
| 59 | with open('.patman', 'w', buffering=1) as f: |
| 60 | f.write('[settings]\n' |
| 61 | 'project: guix-patches\n' |
| 62 | 'check_patch: False\n') |
| 63 | settings.Setup(parser, 'unknown', global_config.name) |
| 64 | args, _ = parser.parse_known_args([]) |
| 65 | assert args.project == 'guix-patches' |
| 66 | send_args, _ = send.parse_known_args([]) |
| 67 | assert not send_args.check_patch |