blob: 06b7cbc3ab6f3122dd1c90c04aa6bf12c7e64b51 [file] [log] [blame]
Maxim Cournoyercb8d3b12022-12-20 00:38:41 -05001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright (c) 2022 Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com>
4#
5
6import argparse
7import contextlib
8import os
9import sys
10import tempfile
11
12from patman import settings
Simon Glass131444f2023-02-23 18:18:04 -070013from u_boot_pylib import tools
Maxim Cournoyercb8d3b12022-12-20 00:38:41 -050014
15
16@contextlib.contextmanager
17def 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
25def 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
34def 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