chainlib/tests/test_cli.py

142 lines
4.1 KiB
Python
Raw Normal View History

# standard imports
import unittest
import os
import logging
2022-05-11 17:24:59 +02:00
# external imports
from aiee.arg import process_args
# local imports
2022-05-11 17:24:59 +02:00
#from chainlib.cli.base import argflag_std_base
from chainlib.cli.arg import (
ArgFlag,
Arg,
ArgumentParser,
)
2022-05-11 20:20:45 +02:00
from chainlib.cli.config import (
Config,
process_config,
)
script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(script_dir, 'testdata')
config_dir = os.path.join(data_dir, 'config')
logging.basicConfig(level=logging.DEBUG)
class TestCli(unittest.TestCase):
2022-05-11 17:24:59 +02:00
def setUp(self):
self.flags = ArgFlag()
self.arg = Arg(self.flags)
def test_args_process_single(self):
2022-05-11 17:24:59 +02:00
ap = ArgumentParser()
flags = self.flags.VERBOSE | self.flags.CONFIG
process_args(ap, self.arg, flags)
argv = [
'-vv',
'-n',
'foo',
]
args = ap.parse_args(argv)
2022-05-11 20:20:45 +02:00
config = Config(config_dir)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('CONFIG_USER_NAMESPACE'), 'foo')
def test_args_process_schema_override(self):
2022-05-11 20:20:45 +02:00
ap = ArgumentParser()
2022-05-11 17:24:59 +02:00
flags = self.flags.VERBOSE | self.flags.CONFIG
process_args(ap, self.arg, flags)
args = ap.parse_args([])
2022-05-11 17:24:59 +02:00
2022-05-11 20:20:45 +02:00
config = Config(config_dir)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('FOO_BAR'), 'baz')
def test_args_process_arg_override(self):
2022-05-11 20:20:45 +02:00
ap = ArgumentParser()
2022-05-11 17:24:59 +02:00
flags = self.flags.VERBOSE | self.flags.CONFIG
process_args(ap, self.arg, flags)
argv = [
'-c',
config_dir,
'-n',
'foo',
]
args = ap.parse_args(argv)
2022-05-11 20:20:45 +02:00
config = Config(config_dir, namespace=args.namespace)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('FOO_BAR'), 'bazbazbaz')
def test_args_process_internal_override(self):
2022-05-11 20:20:45 +02:00
ap = ArgumentParser()
flags = self.flags.VERBOSE | self.flags.CONFIG | self.flags.CHAIN_SPEC
2022-05-11 17:24:59 +02:00
process_args(ap, self.arg, flags)
args = ap.parse_args()
default_config_dir = os.path.join(config_dir, 'default')
2022-05-11 20:20:45 +02:00
config = Config(default_config_dir)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('CHAIN_SPEC'), 'baz:bar:13:foo')
user_config_dir = os.path.join(default_config_dir, 'user')
2022-05-11 20:20:45 +02:00
config = Config(default_config_dir)
config.add_override_dir(user_config_dir)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('CHAIN_SPEC'), 'foo:foo:666:foo')
2022-05-11 20:20:45 +02:00
config = Config(default_config_dir)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('CHAIN_SPEC'), 'baz:bar:13:foo')
2022-05-11 20:20:45 +02:00
ap = ArgumentParser()
2022-05-11 17:24:59 +02:00
process_args(ap, self.arg, flags)
argv = [
'-n',
'user',
]
args = ap.parse_args(argv)
2022-05-11 20:20:45 +02:00
config = Config(default_config_dir, namespace=args.namespace)
config = process_config(config, self.arg, args, flags)
self.assertEqual(config.get('CHAIN_SPEC'), 'foo:foo:666:foo')
2022-05-11 20:20:45 +02:00
# def test_args_process_extra(self):
# ap = ArgumentParser()
# flags = self.flags.VERBOSE | self.flags.CONFIG
# process_args(ap, self.arg, flags)
# ap.add_argument('--foo', type=str)
# argv = [
# '--foo',
# 'bar',
# ]
# args = ap.parse_args(argv)
# extra_args = {
# 'foo': None,
# }
#
# config = Config()
# config = process_config(config, self.arg, args, flags)
# self.assertEqual(config.get('_FOO'), 'bar')
#
# extra_args = {
# 'foo': 'FOOFOO',
# }
#
# config = Config()
# config = process_config(config, self.arg, args, flags)
# self.assertEqual(config.get('FOOFOO'), 'bar')
if __name__ == '__main__':
unittest.main()