|
| 1 | +import sys |
| 2 | +from tempfile import NamedTemporaryFile |
| 3 | +import unittest |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +from tap import Tap |
| 7 | + |
| 8 | + |
| 9 | +class LoadConfigFilesTests(TestCase): |
| 10 | + |
| 11 | + def setUp(self) -> None: |
| 12 | + class DevNull: |
| 13 | + def write(self, msg): |
| 14 | + pass |
| 15 | + self.dev_null = DevNull() |
| 16 | + |
| 17 | + def test_file_does_not_exist(self) -> None: |
| 18 | + class EmptyTap(Tap): |
| 19 | + pass |
| 20 | + |
| 21 | + with self.assertRaises(FileNotFoundError): |
| 22 | + EmptyTap(config_files=['nope']).parse_args([]) |
| 23 | + |
| 24 | + def test_single_config(self) -> None: |
| 25 | + class SimpleTap(Tap): |
| 26 | + a: int |
| 27 | + b: str = 'b' |
| 28 | + |
| 29 | + with NamedTemporaryFile() as f: |
| 30 | + f.write(b'--a 1') |
| 31 | + f.flush() |
| 32 | + args = SimpleTap(config_files=[f.name]).parse_args([]) |
| 33 | + |
| 34 | + self.assertEqual(args.a, 1) |
| 35 | + self.assertEqual(args.b, 'b') |
| 36 | + |
| 37 | + def test_single_config_overwriting(self) -> None: |
| 38 | + class SimpleOverwritingTap(Tap): |
| 39 | + a: int |
| 40 | + b: str = 'b' |
| 41 | + |
| 42 | + with NamedTemporaryFile() as f: |
| 43 | + f.write(b'--a 1 --b two') |
| 44 | + f.flush() |
| 45 | + args = SimpleOverwritingTap(config_files=[f.name]).parse_args('--a 2'.split()) |
| 46 | + |
| 47 | + self.assertEqual(args.a, 2) |
| 48 | + self.assertEqual(args.b, 'two') |
| 49 | + |
| 50 | + def test_single_config_known_only(self) -> None: |
| 51 | + class KnownOnlyTap(Tap): |
| 52 | + a: int |
| 53 | + b: str = 'b' |
| 54 | + |
| 55 | + with NamedTemporaryFile() as f: |
| 56 | + f.write(b'--a 1 --c seeNothing') |
| 57 | + f.flush() |
| 58 | + args = KnownOnlyTap(config_files=[f.name]).parse_args([], known_only=True) |
| 59 | + |
| 60 | + self.assertEqual(args.a, 1) |
| 61 | + self.assertEqual(args.b, 'b') |
| 62 | + self.assertEqual(args.extra_args, ['--c', 'seeNothing']) |
| 63 | + |
| 64 | + def test_single_config_required_still_required(self) -> None: |
| 65 | + class KnownOnlyTap(Tap): |
| 66 | + a: int |
| 67 | + b: str = 'b' |
| 68 | + |
| 69 | + with NamedTemporaryFile() as f, self.assertRaises(SystemExit): |
| 70 | + sys.stderr = self.dev_null |
| 71 | + f.write(b'--b fore') |
| 72 | + f.flush() |
| 73 | + KnownOnlyTap(config_files=[f.name]).parse_args([]) |
| 74 | + |
| 75 | + def test_multiple_configs(self) -> None: |
| 76 | + class MultipleTap(Tap): |
| 77 | + a: int |
| 78 | + b: str = 'b' |
| 79 | + |
| 80 | + with NamedTemporaryFile() as f1, NamedTemporaryFile() as f2: |
| 81 | + f1.write(b'--b two') |
| 82 | + f1.flush() |
| 83 | + f2.write(b'--a 1') |
| 84 | + f2.flush() |
| 85 | + args = MultipleTap(config_files=[f1.name, f2.name]).parse_args([]) |
| 86 | + |
| 87 | + self.assertEqual(args.a, 1) |
| 88 | + self.assertEqual(args.b, 'two') |
| 89 | + |
| 90 | + def test_multiple_configs_overwriting(self) -> None: |
| 91 | + class MultipleOverwritingTap(Tap): |
| 92 | + a: int |
| 93 | + b: str = 'b' |
| 94 | + c: str = 'c' |
| 95 | + |
| 96 | + with NamedTemporaryFile() as f1, NamedTemporaryFile() as f2: |
| 97 | + f1.write(b'--a 1 --b two') |
| 98 | + f1.flush() |
| 99 | + f2.write(b'--a 2 --c see') |
| 100 | + f2.flush() |
| 101 | + args = MultipleOverwritingTap(config_files=[f1.name, f2.name]).parse_args('--b four'.split()) |
| 102 | + |
| 103 | + self.assertEqual(args.a, 2) |
| 104 | + self.assertEqual(args.b, 'four') |
| 105 | + self.assertEqual(args.c, 'see') |
| 106 | + |
| 107 | + def test_junk_config(self) -> None: |
| 108 | + class JunkConfigTap(Tap): |
| 109 | + a: int |
| 110 | + b: str = 'b' |
| 111 | + |
| 112 | + with NamedTemporaryFile() as f1, self.assertRaises(SystemExit): |
| 113 | + sys.stderr = self.dev_null |
| 114 | + f1.write(b'is not a file that can reasonably be parsed') |
| 115 | + f1.flush() |
| 116 | + JunkConfigTap(config_files=[f1.name]).parse_args() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + unittest.main() |
0 commit comments