def test_full_toml_intersect_list_inheritance(): child = _("""\ __extends = ["table1.toml", "table2.toml"] [child_only_section] should_be_child = "child" [common] should_be_child = "child" """) table1 = _("""\ [common] should_be_child = "table1" should_be_table1 = "table1" should_be_table2 = "table1" """) table2 = _("""\ [common] should_be_child = "table2" should_be_table2 = "table2" """) expected = _("""\ [child_only_section] should_be_child = "child" [common] should_be_child = "child" should_be_table1 = "table1" should_be_table2 = "table2" """) check( ("child.toml", child), ("table1.toml", table1), ("table2.toml", table2), ("expected.toml", expected), )
def test_mixed_module_bad(): with pytest.raises(modcfg.errors.MixedModuleContents): modcfg.loads( _(""" mod a: a = a b = b - c """)) with pytest.raises(modcfg.errors.MixedModuleContents): modcfg.loads( _(""" mod a: a = a - b - c """)) with pytest.raises(modcfg.errors.MixedModuleContents): modcfg.loads( _(""" mod a: a = a - b c = c """))
def test_full_toml_table_inheritance(): child = _("""\ [__extends] first_only = "table1.toml" second_only = "table2.toml" common = ["table1.toml", "table2.toml"] [child_only_section] should_be_child = "child" [common] should_be_child = "child" """) table1 = _("""\ [common] should_be_child = "table1" should_be_table1 = "table1" should_be_table2 = "table1" [first_only] should_be_table1 = "table1" [second_only] should_be_table2 = "table1" """) table2 = _("""\ [common] should_be_child = "table2" should_be_table2 = "table2" [first_only] should_be_table1 = "table2" [second_only] should_be_table2 = "table2" """) expected = _("""\ [first_only] should_be_table1 = "table1" [second_only] should_be_table2 = "table2" [child_only_section] should_be_child = "child" [common] should_be_child = "child" should_be_table1 = "table1" should_be_table2 = "table2" """) check( ("child.toml", child), ("table1.toml", table1), ("table2.toml", table2), ("expected.toml", expected), )
def test_missing_categories_label(): # Bad schema in yaml form. yaml_schema = _("""\ $id: test.schema title: Test Event version: 1 type: object properties: test_property: description: testing a property type: string """) yaml = YAML(typ='safe') schema = yaml.load(yaml_schema) # Register schema with an EventLog e = EventLog( allowed_schemas={ SCHEMA_ID: { "allowed_categories": ["random-category"] } } ) # This schema does not have categories as a list. with pytest.raises(KeyError) as err: e.register_schema(schema) # Verify that the error message is the expected error message. assert 'All properties must have a "categories"' in str(err.value)
def test_raised_exception_for_nonlist_categories(): # Bad schema in yaml form. yaml_schema = _("""\ $id: test.schema title: Test Event version: 1 type: object properties: test_property: description: testing a property categories: user-identifier type: string """) yaml = YAML(typ='safe') schema = yaml.load(yaml_schema) # Register schema with an EventLog e = EventLog( allowed_schemas={ SCHEMA_ID: { "allowed_categories": ["user-identifier"] } }, ) # This schema does not have categories as a list. with pytest.raises(ValueError) as err: e.register_schema(schema) # Verify that the error message is the expected error message. assert 'must be a list.' in str(err.value)
def test_impossible_enum_resolve_will_fail(self): DOC = _(""" module Story: is_made_by_a: :duck_typing # NANI!? mod Python: has = :duck_typing """) class WeirdEnum(enum.Enum): useless = True with pytest.raises(modcfg.errors.EnumResolveError): modcfg.loads(DOC) with pytest.raises(modcfg.errors.EnumResolveError): modcfg.loads(DOC, enums=[]) assert (modcfg.loads(DOC, enum_resolve_fail_silently=True) == [ Module(name="Story", contents={"is_made_by_a": ":duck_typing"}), Module(name="Python", contents={"has": ":duck_typing"}), ] == modcfg.loads( DOC, enums=[WeirdEnum], enum_resolve_fail_silently=True)) with pytest.raises(modcfg.errors.EnumResolveError): modcfg.loads(DOC, enums=[WeirdEnum]) with pytest.raises(modcfg.errors.EnumResolveError): modcfg.loads("""{main: :e.e}""", enums=[WeirdEnum]) assert modcfg.loads("""{main: :e.e}""", enums=[WeirdEnum], enum_resolve_fail_silently=True) == [{ "main": ":e.e" }]
def test_enum_explicit(self): DOC = _(""" module Story: is_made_by_a: :Enum1.duck_typing # NANI!? mod Python: has = :Enum2.duck_typing """) class Enum1(enum.Enum): duck_typing = "DUCKS CAN TYPE!!?" human_typing = "Much better" class Enum2(enum.Enum): polymorphism = "sucks" duck_typing = "is cool" assert modcfg.loads( DOC, enums=[Enum1, Enum2], ) == [ Module( name="Story", contents={"is_made_by_a": Enum1.duck_typing}, ), Module( name="Python", contents={"has": Enum2.duck_typing}, ), ]
def test_yaml_like(self): DOC = _(""" bruh: yaml: { kinda: - works - { this: [is, insane] } } """) assert modcfg.loads(DOC) == [{ "bruh": { "yaml": { "kinda": ["works", { "this": ["is", "insane"] }] } } }] assert modcfg.loads(DOC, inline=True) == { "bruh": { "yaml": { "kinda": ["works", { "this": ["is", "insane"] }] } } }
def test_single_mod(): assert (modcfg.loads( _(""" mod a: b = c """)) == modcfg.loads("module a:\n\tb -> c") == modcfg.loads("module a:\n\tb: c") == modcfg.loads("module a:\n\tb : c") == modcfg.loads("module a:\n\tb => c") == modcfg.loads("module a:\n\tb =>c") == modcfg.loads("module a:\n\tb=>c") == [Module("a", {"b": "c"})])
def test_fail_on_ambiguity(self): DOC = _(""" module Story: is_made_by_a: :duck_typing # NANI!? mod Python: has = :duck_typing """) with pytest.raises(modcfg.errors.EnumResolveError): modcfg.loads( DOC, enums=[Enum1, Enum2], )
def test_multiple_mod(): assert modcfg.loads( _(""" mod a: b = c mod b: c = d module c: d = e """)) == [ Module("a", {"b": "c"}), Module("b", {"c": "d"}), Module("c", {"d": "e"}), ]
def test_full_toml_disjoint_list_inheritance(): child = _("""\ __extends = ["table1.toml", "table2.toml"] """) table1 = _("""\ tabe1data = {foo = "bar"} """) table2 = _("""\ tabe2data = {fizz = "buzz"} """) expected = _("""\ [tabe1data] foo = "bar" [tabe2data] fizz = "buzz" """) check( ("child.toml", child), ("table1.toml", table1), ("table2.toml", table2), ("expected.toml", expected), )
def test_chained_table_inheritance(): child = _("""\ __extends = "table1.toml" """) table1 = _("""\ [tool.black] __extends = "table2.toml" [tool.isort] __extends = "table3.toml" """) table2 = _("""\ [tool.black] key1 = "value1" key2 = "value2" """) table3 = _("""\ [tool.isort] key11 = "value11" key21 = "value21" """) expected = _("""\ [tool.black] key1 = "value1" key2 = "value2" [tool.isort] key11 = "value11" key21 = "value21" """) check( ("child.toml", child), ("table1.toml", table1), ("table2.toml", table2), ("table3.toml", table3), ("expected.toml", expected), )
def install(): loc = "/usr/local/bin/netsurf" script = _("""\ #!/usr/bin/env bash docker run --rm -it --privileged pwoolvett/netsurf-fb:1.0.0 $@ """) cmd = '''sh -c "echo -n '{}' | sudo tee {}"'''.format( r"{}".format(script), loc) r1 = subprocess.check_output(split(cmd)) r2 = subprocess.check_output(split(f"sudo chmod +x {loc}")) print(f"Succesfully installed netsurf at {loc}") return r1 + r2
def test_table_inheritance(): child = _("""\ [from_table_1] __extends = "table1.toml" another = "value" """) table1 = _("""\ [from_table_1] should_be_child = "table1" should_be_table1 = "table1" should_be_table2 = "table1" """) expected = _("""\ [from_table_1] another = "value" should_be_child = "table1" should_be_table1 = "table1" should_be_table2 = "table1" """) check( ("child.toml", child), ("table1.toml", table1), ("expected.toml", expected), )
def test_no_module(): assert (modcfg.loads( _("""bruh: json: { kinda: - works - {this: [is, insane]} } """)) == [{ "bruh": { "json": { "kinda": ["works", { "this": ["is", "insane"] }] } } }])
def test_datetimes_and_dates(self): DOC = _(""" mod 'Date example': today = datetime(2021-04-18 14:50:55.016922) tomorrow = date(2021-04-19) """) assert (modcfg.loads(DOC) == modcfg.loads(DOC.replace( "mod", "module")) == [ Module( name="Date example", contents={ "today": datetime.datetime(2021, 4, 18, 14, 50, 55, 16922), "tomorrow": datetime.date(2021, 4, 19), }, ) ])
def test_ignore_ambiguity_is_deterministic(self): DOC = _(""" module Story: is_made_by_a: :duck_typing # NANI!? mod Python: has = :duck_typing """) assert modcfg.loads(DOC, enums=[Enum1, Enum2], enum_ambiguity_check=False) == [ Module("Story", {"is_made_by_a": Enum1.duck_typing}), Module("Python", {"has": Enum1.duck_typing}), ] assert modcfg.loads(DOC, enums=[Enum2, Enum1], enum_ambiguity_check=False) == [ Module("Story", {"is_made_by_a": Enum2.duck_typing}), Module("Python", {"has": Enum2.duck_typing}), ]
def test_enums(self): class MyEnum(enum.Enum): is_cool = "we are swag" isnt_cool = "we are not swag" DOC = _(""" module "Bob": personality: :is_cool 'hair color': brown 'loves yaml': no """) assert (modcfg.loads(DOC, enums=[MyEnum]) == modcfg.loads( DOC.replace("module", "mod"), enums=[MyEnum]) == [ Module( name="Bob", contents={ "personality": MyEnum.is_cool, "hair color": "brown", "loves yaml": "no", }, ) ])
def test_more_enums(self): DOC = _(""" module ThatXliner: personality = :is_cool hair_color => brown coder = true module SomePythoniast: hates = :polymorphism loves = :duck_typing """) class MyFirstEnum(enum.Enum): is_cool = "we are swag" isnt_cool = "we are not swag" class MySecondEnum(enum.Enum): polymorphism = "sucks" duck_typing = "is cool" assert modcfg.loads( DOC, enums=[MyFirstEnum, MySecondEnum], ) == [ Module( name="ThatXliner", contents={ "personality": MyFirstEnum.is_cool, "hair_color": "brown", "coder": True, }, ), Module( name="SomePythoniast", contents={ "hates": MySecondEnum.polymorphism, "loves": MySecondEnum.duck_typing, }, ), ]
def test_initial(self): DOC = _(""" module hello_world: hello => world this: "also works" 'single quotes' = "equals double quotes" how -> { about: { some: - very - crazy - data: structures = o_0 } } """) DOC2 = _(""" module hello_world: hello => world this: "also works" 'single quotes' = "equals double quotes" how -> { about: { some: - very - crazy - data: structures = o_0 } } """) assert (modcfg.loads(DOC) == modcfg.loads(DOC2) == [ Module( name="hello_world", contents={ "hello": "world", "this": "also works", "single quotes": "equals double quotes", "how": { "about": { "some": [ "very", "crazy", { "data": { "structures": "o_0" } }, ] } }, }, ) ])
def test_full_toml_single_inheritance(example_raw): child = _("""\ __extends = "parent.toml" """) parent = example_raw check(("child.toml", child), ("parent.toml", parent))
def test_pyproject(): child = _("""\ __extends = "ref_pyproject.toml" [tool.poetry] section_key = "value" """) ref_pyproject = _("""\ [tool.black] __extends = "black.toml" [tool.isort] __extends = "isort.toml" [tool.docformatter] __extends = "docformatter.toml" [tool.flakehell] __extends = "flakehell.toml" """) black = _("""\ [tool.black] line-length = 79 exclude = ''' a multiline string ''' """) isort = _("""\ [tool.isort] profile = "black" force_single_line = true src_paths = ["src"] known_local_folder = ["test"] atomic = true """) docformatter = _("""\ [tool.docformatter] blank=true recursive=true """) flakehell = _("""\ [tool.flakehell] format = "colored" max_line_length = 79 exclude = [ # self-managed files should not be checked "poetry.lock", "./.venv", ] ignore = [ "F401", "E501" # pylint takes care of these ] docstring-convention = "google" # flake8-docstrings docstring_style = "google" # darglint [tool.flakehell.plugins] flake8-bandit = [ "+*", "-S322", # input for python2, no problem ] flake8-bugbear = ["+*"] flake8-builtins = ["+*"] flake8-comprehensions = ["+*"] flake8-darglint = ["+*"] flake8-docstrings = [ "+*", "-D202", # black conflict "-D412", # we do want lines between header and contents. See https://github.com/PyCQA/pydocstyle/issues/412 ] flake8-eradicate = ["+*"] flake8-isort = ["+*"] flake8-debugger = ["+*"] flake8-mutable = ["+*"] flake8-pytest-style = ["+*"] mccabe = ["+*"] pep8-naming = [ "+*", "-N805", # pylint duplicate ] pycodestyle = [ "+*", "-E501", # pylint duplicate "-E203", # false positives on list slice "-E722", # bugbear duplicate ] pyflakes = ["+*"] pylint = ["+*"] pandas-dev = ["+*"] [tool.flakehell.exceptions."docs/src/conf.py"] flake8-eradicate = [ "-E800", ] flake8-docstrings = [ "-D100", ] flake8-builtins = [ "-A001", # variable "copyright" is shadowing a python builtin -> sphinx wants it ] [tool.flakehell.exceptions."tests/"] flake8-docstrings = [ "-D100", "-D101", "-D102", "-D103", "-D104", ] flake8-bandit = [ "-S101", # asserts are ok ] flake8-darglint = [ "-DAR101", ] pylint = [ "-C0115", "-C0115", "-C0116", "-C0116", ] """) expected = _("""\ [tool.poetry] section_key = "value" [tool.black] line-length = 79 exclude = ''' a multiline string ''' [tool.isort] atomic = true profile = "black" known_local_folder = ["test"] force_single_line = true src_paths = ["src"] [tool.docformatter] blank = true recursive = true [tool.flakehell] format = "colored" max_line_length = 79 exclude = [ # self-managed files should not be checked "poetry.lock", "./.venv", ] ignore = [ "F401", "E501" # pylint takes care of these ] docstring-convention = "google" # flake8-docstrings docstring_style = "google" # darglint [tool.flakehell.plugins] flake8-bandit = [ "+*", "-S322", # input for python2, no problem ] flake8-bugbear = ["+*"] flake8-builtins = ["+*"] flake8-comprehensions = ["+*"] flake8-darglint = ["+*"] flake8-docstrings = [ "+*", "-D202", # black conflict "-D412", # we do want lines between header and contents. See https://github.com/PyCQA/pydocstyle/issues/412 ] flake8-eradicate = ["+*"] flake8-isort = ["+*"] flake8-debugger = ["+*"] flake8-mutable = ["+*"] flake8-pytest-style = ["+*"] mccabe = ["+*"] pep8-naming = [ "+*", "-N805", # pylint duplicate ] pycodestyle = [ "+*", "-E501", # pylint duplicate "-E203", # false positives on list slice "-E722", # bugbear duplicate ] pyflakes = ["+*"] pylint = ["+*"] pandas-dev = ["+*"] [tool.flakehell.exceptions."docs/src/conf.py"] flake8-eradicate = [ "-E800", ] flake8-docstrings = [ "-D100", ] flake8-builtins = [ "-A001", # variable "copyright" is shadowing a python builtin -> sphinx wants it ] [tool.flakehell.exceptions."tests/"] flake8-docstrings = [ "-D100", "-D101", "-D102", "-D103", "-D104", ] flake8-bandit = [ "-S101", # asserts are ok ] flake8-darglint = [ "-DAR101", ] pylint = [ "-C0115", "-C0115", "-C0116", "-C0116", ] """) check( ("child.toml", child), ("ref_pyproject.toml", ref_pyproject), ("black.toml", black), ("isort.toml", isort), ("docformatter.toml", docformatter), ("flakehell.toml", flakehell), ("expected.toml", expected), )
".", "_") NOW = now() IPSEC_CONF = _("""\ config setup virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12 nat_traversal=yes protostack=netkey plutoopts="--interface={interface}" conn L2TP-PSK authby=secret pfs=no auto=add keyingtries=3 dpddelay=30 dpdtimeout=120 dpdaction=clear rekey=yes ikelifetime=8h keylife=1h type=transport left={local_ip} leftprotoport=17/1701 right={server_ip} rightprotoport=17/1701 """) IPSEC_SECRETS = '{local_ip} {server_ip} : PSK "{psk}"' XL2TPD_CONF = _("""\