chainlib

Generic blockchain access library and tooling
Info | Log | Files | Refs | README | LICENSE

test_cli.py (3753B)


      1 # standard imports
      2 import unittest
      3 import os
      4 import logging
      5 
      6 # external imports
      7 from aiee.arg import process_args
      8 
      9 # local imports
     10 #from chainlib.cli.base import argflag_std_base
     11 from chainlib.cli.arg import (
     12         ArgFlag,
     13         Arg,
     14         ArgumentParser,
     15         )
     16 from chainlib.cli.config import (
     17         Config,
     18         process_config,
     19         )
     20 script_dir = os.path.dirname(os.path.realpath(__file__))
     21 data_dir = os.path.join(script_dir, 'testdata')
     22 config_dir = os.path.join(data_dir, 'config')
     23 
     24 logging.basicConfig(level=logging.DEBUG)
     25 
     26 
     27 class TestCli(unittest.TestCase):
     28 
     29     def setUp(self):
     30         self.flags = ArgFlag()
     31         self.arg = Arg(self.flags)
     32 
     33 
     34     def test_args_process_single(self):
     35         ap = ArgumentParser()
     36         flags = self.flags.VERBOSE | self.flags.CONFIG
     37         process_args(ap, self.arg, flags)
     38 
     39         argv = [
     40             '-vv',
     41             '-n',
     42             'foo',
     43                 ]
     44         args = ap.parse_args(argv)
     45         config = Config(config_dir)
     46         config = process_config(config, self.arg, args, flags)
     47         self.assertEqual(config.get('CONFIG_USER_NAMESPACE'), 'foo')
     48 
     49 
     50     def test_args_process_schema_override(self):
     51         ap = ArgumentParser()
     52         flags = self.flags.VERBOSE | self.flags.CONFIG
     53         process_args(ap, self.arg, flags)
     54 
     55         args = ap.parse_args([])
     56 
     57         config = Config(config_dir)
     58         config = process_config(config, self.arg, args, flags)
     59         self.assertEqual(config.get('FOO_BAR'), 'baz')
     60 
     61 
     62     def test_args_process_arg_override(self):
     63         ap = ArgumentParser()
     64         flags = self.flags.VERBOSE | self.flags.CONFIG
     65         process_args(ap, self.arg, flags)
     66 
     67         argv = [
     68             '-c',
     69             config_dir,
     70             '-n',
     71             'foo',
     72             ]
     73         args = ap.parse_args(argv)
     74         config = Config(config_dir, namespace=args.namespace)
     75         config = process_config(config, self.arg, args, flags)
     76         self.assertEqual(config.get('FOO_BAR'), 'bazbazbaz')
     77 
     78 
     79     def test_args_process_internal_override(self):
     80         ap = ArgumentParser()
     81         flags = self.flags.VERBOSE | self.flags.CONFIG | self.flags.CHAIN_SPEC
     82         process_args(ap, self.arg, flags)
     83 
     84         args = ap.parse_args()
     85         default_config_dir = os.path.join(config_dir, 'default')
     86 
     87         config = Config(default_config_dir)
     88         config = process_config(config, self.arg, args, flags)
     89         self.assertEqual(config.get('CHAIN_SPEC'), 'baz:bar:13:foo')
     90 
     91         user_config_dir = os.path.join(default_config_dir, 'user')
     92         config = Config(default_config_dir)
     93         config.add_override_dir(user_config_dir)
     94         config = process_config(config, self.arg, args, flags)
     95         self.assertEqual(config.get('CHAIN_SPEC'), 'foo:foo:666:foo')
     96 
     97         config = Config(default_config_dir)
     98         config = process_config(config, self.arg, args, flags)
     99         self.assertEqual(config.get('CHAIN_SPEC'), 'baz:bar:13:foo')
    100 
    101         ap = ArgumentParser()
    102         process_args(ap, self.arg, flags)
    103         argv = [
    104                 '-n',
    105                 'user',
    106                 ]
    107         args = ap.parse_args(argv)
    108         config = Config(default_config_dir, namespace=args.namespace)
    109         config = process_config(config, self.arg, args, flags)
    110         self.assertEqual(config.get('CHAIN_SPEC'), 'foo:foo:666:foo')
    111 
    112 
    113     def test_all_args(self):
    114         ap = ArgumentParser()
    115         flags = self.flags.all
    116         process_args(ap, self.arg, flags)
    117 
    118         args = ap.parse_args([
    119             '-y', 'foo',
    120             '-i', 'foo:bar:42:baz',
    121             '-z',
    122             ])
    123 
    124         config = Config()
    125         config = process_config(config, self.arg, args, flags)
    126 
    127 
    128 if __name__ == '__main__':
    129     unittest.main()