2021-08-23 08:27:48 +02:00
|
|
|
# standard imports
|
|
|
|
import unittest
|
|
|
|
import os
|
2021-10-28 12:16:23 +02:00
|
|
|
import logging
|
2021-08-23 08:27:48 +02:00
|
|
|
|
2022-05-11 17:24:59 +02:00
|
|
|
# external imports
|
|
|
|
from aiee.arg import process_args
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
# 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,
|
|
|
|
)
|
2021-08-23 08:27:48 +02:00
|
|
|
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')
|
|
|
|
|
2021-10-28 12:16:23 +02:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
|
|
|
|
class TestCli(unittest.TestCase):
|
2022-05-11 17:24:59 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.flags = ArgFlag()
|
|
|
|
self.arg = Arg(self.flags)
|
|
|
|
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
|
|
|
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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)
|
2021-08-23 08:27:48 +02:00
|
|
|
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')
|
2021-08-23 08:27:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|