Skip to content

Commit

Permalink
Added tests to ensure optional argument options are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
HJK-X committed Aug 30, 2024
1 parent 2a6399a commit 7b788b6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
3 changes: 2 additions & 1 deletion landscape/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def make_parser(self):
parser.add_argument(
"--manage-sources-list-d",
nargs="?",
default=True,
default="true",
const="true",
help="Repository profiles manage the files in "
"’etc/apt/sources.list.d'. (default: true)",
)
Expand Down
2 changes: 1 addition & 1 deletion landscape/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __getattr__(self, name):
option = action
break
if option is not None and option.type is not None:
if option.nargs is None or option.nargs == 1:
if option.nargs in [None, 1, "?"]:
return option.type(value)
else:
return tuple([option.type(v) for v in value])
Expand Down
80 changes: 76 additions & 4 deletions landscape/lib/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ def make_parser(self):
return parser


class OptionalArgConfiguration(BaseConfiguration):
config_section = "my-config"

def make_parser(self):
parser = super().make_parser()
parser.add_argument("--test")
parser.add_argument(
"--foo-bar", nargs="?", default="foofoo", const="foofoo"
)

return parser


def cfg_class(section=None, **defaults):
class MyConfiguration(BaseConfiguration):
config_section = section or "my-config"
Expand Down Expand Up @@ -161,7 +174,7 @@ def test_get_config_filename_precedence(self):

# argparse CLI parsing

def test_default_positional_argument(self):
def test_parse_default_positional_argument(self):
self.reset_config(cfg_class(foo_bar=None))
self.config_class.config = None
self.write_config_file()
Expand All @@ -170,7 +183,7 @@ def test_default_positional_argument(self):
self.assertEqual(self.config.foo_bar, "ooga")
self.assertEqual(self.config.positional, SUPPRESS)

def test_single_positional_argument(self):
def test_parse_single_positional_argument(self):
self.reset_config(cfg_class(foo_bar=None))
self.config_class.config = None
self.write_config_file()
Expand All @@ -179,7 +192,7 @@ def test_single_positional_argument(self):
self.assertEqual(self.config.foo_bar, "ooga")
self.assertEqual(self.config.positional, ["bar"])

def test_intermixed_positional_arguments(self):
def test_parse_intermixed_positional_arguments(self):
self.reset_config(cfg_class(foo_bar=None, extra_bar=None))
self.config_class.config = None
self.write_config_file()
Expand All @@ -200,7 +213,7 @@ def test_intermixed_positional_arguments(self):
self.assertEqual(self.config.extra_bar, "foo")
self.assertEqual(self.config.positional, ["bar", "b1", "b2", "b3"])

def test_positional_arguments_unsaved(self):
def test_parse_positional_arguments_unsaved(self):
self.reset_config(cfg_class())
self.config_class.config = None
self.write_config_file()
Expand All @@ -212,6 +225,65 @@ def test_positional_arguments_unsaved(self):
self.config.load([])
self.assertEqual(self.config.positional, SUPPRESS)

def test_parse_without_option_and_optional_argument(self):
self.reset_config(OptionalArgConfiguration)
self.config_filename = self.makeFile("")
os.unlink(self.config_filename)
self.config.default_config_filenames[:] = [self.config_filename]
self.config_class.config = None
self.config.write()

self.config.load(["--test", "ooga"])
self.assertEqual(self.config.test, "ooga")
self.assertEqual(self.config.foo_bar, "foofoo")
self.config.write()

self.config.load([])
self.assertEqual(self.config.foo_bar, "foofoo")

data = read_text_file(self.config_filename)
self.assertConfigEqual(data, "[my-config]\ntest = ooga")

def test_parse_with_option_including_optional_argument(self):
self.reset_config(OptionalArgConfiguration)
self.config_filename = self.makeFile("")
os.unlink(self.config_filename)
self.config.default_config_filenames[:] = [self.config_filename]
self.config_class.config = None
self.config.write()

self.config.load(["--foo-bar", "barbar", "--test", "ooga"])
self.assertEqual(self.config.foo_bar, "barbar")
self.config.write()

self.config.load([])
self.assertEqual(self.config.foo_bar, "barbar")

data = read_text_file(self.config_filename)
self.assertConfigEqual(
data, "[my-config]\nfoo_bar = barbar\ntest = ooga"
)

def test_parse_with_option_excluding_optional_argument(self):
self.reset_config(OptionalArgConfiguration)
self.config_filename = self.makeFile("")
os.unlink(self.config_filename)
self.config.default_config_filenames[:] = [self.config_filename]
self.config_class.config = None
self.config.write()

self.config.load(["--foo-bar", "--test", "ooga"])
self.assertEqual(self.config.foo_bar, "foofoo")
self.config.write()

self.config.load([])
self.assertEqual(self.config.foo_bar, "foofoo")

data = read_text_file(self.config_filename)
self.assertConfigEqual(
data, "[my-config]\nfoo_bar = foofoo\ntest = ooga"
)

# ConfigObj

def test_get_config_object(self):
Expand Down

0 comments on commit 7b788b6

Please sign in to comment.