File: test_cli.py

package info (click to toggle)
python-confuse 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: python: 3,065; makefile: 112; sh: 8
file content (152 lines) | stat: -rw-r--r-- 5,852 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import argparse
import optparse
import unittest
from argparse import Namespace

import pytest

import confuse


class ArgparseTest(unittest.TestCase):
    def setUp(self):
        self.config = confuse.Configuration("test", read=False)
        self.parser = argparse.ArgumentParser()

    def _parse(self, args, **kwargs):
        args = self.parser.parse_args(args.split())
        self.config.set_args(args, **kwargs)

    def test_text_argument_parsed(self):
        self.parser.add_argument("--foo", metavar="BAR")
        self._parse("--foo bar")
        assert self.config["foo"].get() == "bar"

    def test_boolean_argument_parsed(self):
        self.parser.add_argument("--foo", action="store_true")
        self._parse("--foo")
        assert self.config["foo"].get()

    def test_missing_optional_argument_not_included(self):
        self.parser.add_argument("--foo", metavar="BAR")
        self._parse("")
        with pytest.raises(confuse.NotFoundError):
            self.config["foo"].get()

    def test_argument_overrides_default(self):
        self.config.add({"foo": "baz"})

        self.parser.add_argument("--foo", metavar="BAR")
        self._parse("--foo bar")
        assert self.config["foo"].get() == "bar"

    def test_nested_destination_single(self):
        self.parser.add_argument("--one", dest="one.foo")
        self.parser.add_argument("--two", dest="one.two.foo")
        self._parse("--two TWO", dots=True)
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_nested(self):
        self.parser.add_argument("--one", dest="one.foo")
        self.parser.add_argument("--two", dest="one.two.foo")
        self._parse("--two TWO --one ONE", dots=True)
        assert self.config["one"]["foo"].get() == "ONE"
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_nested_rev(self):
        self.parser.add_argument("--one", dest="one.foo")
        self.parser.add_argument("--two", dest="one.two.foo")
        # Reverse to ensure order doesn't matter
        self._parse("--one ONE --two TWO", dots=True)
        assert self.config["one"]["foo"].get() == "ONE"
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_clobber(self):
        self.parser.add_argument("--one", dest="one.two")
        self.parser.add_argument("--two", dest="one.two.foo")
        self._parse("--two TWO --one ONE", dots=True)
        # Clobbered
        assert self.config["one"]["two"].get() == {"foo": "TWO"}
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_clobber_rev(self):
        # Reversed order
        self.parser.add_argument("--two", dest="one.two.foo")
        self.parser.add_argument("--one", dest="one.two")
        self._parse("--one ONE --two TWO", dots=True)
        # Clobbered just the same
        assert self.config["one"]["two"].get() == {"foo": "TWO"}
        assert self.config["one"]["two"]["foo"].get() == "TWO"


class OptparseTest(unittest.TestCase):
    def setUp(self):
        self.config = confuse.Configuration("test", read=False)
        self.parser = optparse.OptionParser()

    def _parse(self, args, **kwargs):
        options, _ = self.parser.parse_args(args.split())
        self.config.set_args(options, **kwargs)

    def test_text_argument_parsed(self):
        self.parser.add_option("--foo", metavar="BAR")
        self._parse("--foo bar")
        assert self.config["foo"].get() == "bar"

    def test_boolean_argument_parsed(self):
        self.parser.add_option("--foo", action="store_true")
        self._parse("--foo")
        assert self.config["foo"].get()

    def test_missing_optional_argument_not_included(self):
        self.parser.add_option("--foo", metavar="BAR")
        self._parse("")
        with pytest.raises(confuse.NotFoundError):
            self.config["foo"].get()

    def test_argument_overrides_default(self):
        self.config.add({"foo": "baz"})

        self.parser.add_option("--foo", metavar="BAR")
        self._parse("--foo bar")
        assert self.config["foo"].get() == "bar"

    def test_nested_destination_single(self):
        self.parser.add_option("--one", dest="one.foo")
        self.parser.add_option("--two", dest="one.two.foo")
        self._parse("--two TWO", dots=True)
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_nested(self):
        self.parser.add_option("--one", dest="one.foo")
        self.parser.add_option("--two", dest="one.two.foo")
        self._parse("--two TWO --one ONE", dots=True)
        assert self.config["one"]["foo"].get() == "ONE"
        assert self.config["one"]["two"]["foo"].get() == "TWO"

    def test_nested_destination_nested_rev(self):
        self.parser.add_option("--one", dest="one.foo")
        self.parser.add_option("--two", dest="one.two.foo")
        # Reverse to ensure order doesn't matter
        self._parse("--one ONE --two TWO", dots=True)
        assert self.config["one"]["foo"].get() == "ONE"
        assert self.config["one"]["two"]["foo"].get() == "TWO"


class GenericNamespaceTest(unittest.TestCase):
    def setUp(self):
        self.config = confuse.Configuration("test", read=False)

    def test_value_added_to_root(self):
        self.config.set_args(Namespace(foo="bar"))
        assert self.config["foo"].get() == "bar"

    def test_value_added_to_subview(self):
        self.config["baz"].set_args(Namespace(foo="bar"))
        assert self.config["baz"]["foo"].get() == "bar"

    def test_nested_namespace(self):
        args = Namespace(first="Hello", nested=Namespace(second="World"))
        self.config.set_args(args, dots=True)
        assert self.config["first"].get() == "Hello"
        assert self.config["nested"]["second"].get() == "World"