Skip to content

Commit

Permalink
chore: refactor paser tests to make more organized
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Jul 15, 2023
1 parent c39af88 commit 2ab1ad1
Show file tree
Hide file tree
Showing 73 changed files with 284 additions and 183 deletions.
2 changes: 1 addition & 1 deletion tackle/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def compact_hook_call_macro(context: 'Context', element: str) -> dict:
def list_to_var_macro(context: 'Context', element: list) -> dict:
"""
Convert arrow keys with a list as the value to `var` hooks via a re-write to the
input.
input.
"""
# TODO: Convert this to a block. Issue is that keys are not rendered by default so
# when str items in a list are parsed, they are not rendered by default. Should
Expand Down
4 changes: 2 additions & 2 deletions tackle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def tackle(
kwargs['input_string'] = args[0]
if len(args) != 1:
kwargs['global_args'] = []
for i in range(1, len(args)):
kwargs['global_args'].append(args[i])
for i in args:
kwargs['global_args'].append(i)

# Handle empty calls which fallback to finding the closest tackle file
# in the parent directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ def test_collections_hook_distinct(change_dir):
output = tackle('distinct.yaml')
for i in output['distincts']:
assert i in ['stuff', 'foo', 'things']


def test_collections_hook_range(change_dir):
o = tackle('range.yaml')
assert o['forward_1'] == [0, 1, 2]
assert o['forward_2'] == [1, 2]
assert o['backward_2'] == [3, 2]
assert o['forward_3'] == [0, 2, 4]
assert o['backward_3'] == [6, 4]
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ def test_provider_tackle_flatten_method(change_dir):
output = tackle('method.yaml', no_input=True)

assert output


def test_provider_tackle_flatten_splat(change_dir):
output = tackle('splat.yaml', no_input=True)

assert output
9 changes: 8 additions & 1 deletion tackle/utils/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ def split_input_string(input_string: str) -> list:
def unpack_args_kwargs_string(input_string: str) -> (list, dict, list):
"""Split up based on whitespace input args and pass to unpack_args_kwargs_list."""
input_list = split_input_string(input_string)
return unpack_args_kwargs_list(input_list)

args, kwargs, flags = unpack_args_kwargs_list(input_list)

clean_kwargs = {k.replace('-', '_'): v for k, v in kwargs.items()}
clean_flags = [i.replace('-', '_') for i in flags]

return args, clean_kwargs, clean_flags
# return unpack_args_kwargs_list(input_list)


def assert_if_flag(arg: str):
Expand Down
3 changes: 3 additions & 0 deletions tackle/utils/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ def run_help(context: 'Context', hook: ModelMetaclass = None):
if context.input_string[-4:] == 'help':
context.input_string = context.input_string[:-4]

# Determine max width of each kwarg / arg / method for formatting


template = Template(HELP_TEMPLATE)
help_rendered = template.render(
args=args,
Expand Down
20 changes: 19 additions & 1 deletion tests/functions/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,25 @@ def test_function_import_func_from_hooks_dir_call_method(change_curdir_fixtures)
assert o['t']['p'] == 'a-default'


def test_function_import_func_from_hooks_dir_call_function_cli(change_curdir_fixtures):
def test_function_run_hook_in_parent_preserve_context(change_curdir_fixtures):
"""
Verify that we can call a hook in a parent dir and have some sensible environment
variables to work with.
"""
os.chdir(os.path.join('func-provider-hook', 'a-dir'))
o = tackle('funcs.yaml', find_in_parent=True)
assert o


def test_function_merge_field_into_hook(change_curdir_fixtures):
"""Verify that we can merge a dict into the parent's field."""
os.chdir('functions')
x = os.listdir()
o = tackle('merge-field.yaml', 'foo')
assert o


def test_function_import_func_from_hooks_dir_context_preserved2(change_curdir_fixtures):
"""
Check that we can call a function on an imported hook from a hooks directory when
calling with args like in CLI.
Expand Down
61 changes: 61 additions & 0 deletions tests/parser/.functions/fixtures/func-provider-hook/funcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

<-:
cwd_->: "{{cwd}}"
home_dir_->: "{{home_dir}}"
system_->: "{{system}}"
platform_->: "{{platform}}"
version_->: "{{version}}"
processor_->: "{{processor}}"
architecture_->: "{{architecture}}"
lsb_release_->: "{{lsb_release}}"
tackle_dir_->: "{{tackle_dir}}"
provider_dir_->: "{{provider_dir}}"
calling_directory_->: "{{calling_directory}}"
calling_file_->: "{{calling_file}}"
current_file_->: "{{current_file}}"
current_directory_->: "{{current_directory}}"
this_->: "{{this}}"
public_context_->: "{{public_context}}"
private_context_->: "{{private_context}}"
existing_context_->: "{{existing_context}}"
temporary_context_->: "{{temporary_context}}"
key_path_->: "{{key_path}}"
key_path_block_->: "{{key_path_block}}"
xdg_cache_home_->: "{{xdg_cache_home}}"
xdg_config_dirs_->: "{{xdg_config_dirs}}"
xdg_config_home_->: "{{xdg_config_home}}"
xdg_data_dirs_->: "{{xdg_data_dirs}}"
xdg_data_home_->: "{{xdg_data_home}}"
xdg_runtime_dir_->: "{{xdg_runtime_dir}}"
xdg_state_home_->: "{{xdg_state_home}}"


here<-:
cwd_->: "{{cwd}}"
home_dir_->: "{{home_dir}}"
system_->: "{{system}}"
platform_->: "{{platform}}"
version_->: "{{version}}"
processor_->: "{{processor}}"
architecture_->: "{{architecture}}"
lsb_release_->: "{{lsb_release}}"
tackle_dir_->: "{{tackle_dir}}"
provider_dir_->: "{{provider_dir}}"
calling_directory_->: "{{calling_directory}}"
calling_file_->: "{{calling_file}}"
current_file_->: "{{current_file}}"
current_directory_->: "{{current_directory}}"
this_->: "{{this}}"
public_context_->: "{{public_context}}"
private_context_->: "{{private_context}}"
existing_context_->: "{{existing_context}}"
temporary_context_->: "{{temporary_context}}"
key_path_->: "{{key_path}}"
key_path_block_->: "{{key_path_block}}"
xdg_cache_home_->: "{{xdg_cache_home}}"
xdg_config_dirs_->: "{{xdg_config_dirs}}"
xdg_config_home_->: "{{xdg_config_home}}"
xdg_data_dirs_->: "{{xdg_data_dirs}}"
xdg_data_home_->: "{{xdg_data_home}}"
xdg_runtime_dir_->: "{{xdg_runtime_dir}}"
xdg_state_home_->: "{{xdg_state_home}}"
8 changes: 8 additions & 0 deletions tests/parser/.functions/fixtures/functions/hook-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

allow<-:
foo: bar

__config__:

__args__:

8 changes: 8 additions & 0 deletions tests/parser/.functions/fixtures/functions/merge-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

base<-:
bar: baz

foo<-:
help: Foo
baz: bing
a_field->: base --merge
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

a_funky<-:
full:
type: str
description: A description
default: a-default
compact: a-default

exec:
foo: bar
do->: var "{{full}}"

args:
- full

return: do

#b_funky<-:
# full:
# type: str
# description: A description
# default: a-default
# compact: a-default
#
# exec:
# foo: bar
# do->: var "{{full}}"
#
# args:
# - full
#
# return: do

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

stuff: things

jinja_extension_default->: var {{a_funky()}}
jinja_extension->: var {{a_funky(stuff)}}
compact->: a_funky
compact_arg->: a_funky stuff
#jinja_filter->: var {{stuff|a_funky}}

calling_dir->: {{calling_directory}}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

a_funky<-:
full:
type: str
description: A description
default: a-default
compact: a-default

exec:
foo: bar
do->: var "{{full}}"

args:
- full

return: do

#b_funky<-:
# full:
# type: str
# description: A description
# default: a-default
# compact: a-default
#
# exec:
# foo: bar
# do->: var "{{full}}"
#
# args:
# - full
#
# return: do

5 changes: 5 additions & 0 deletions tests/parser/fixtures/copied-hook-args.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
upper->: random_string --case upper
# this should now be lower
lower_default->: random_string
# this should now be lower but forced...
lower->: random_string --case lower
20 changes: 0 additions & 20 deletions tests/parser/fixtures/dunder-key.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion tests/parser/fixtures/duplicate-values.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local->: tackle args.yaml --no_input
local->: tackle args.yaml --no_input
24 changes: 0 additions & 24 deletions tests/parser/fixtures/embedded_list.yaml

This file was deleted.

30 changes: 0 additions & 30 deletions tests/parser/fixtures/match-case-logic.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions tests/parser/fixtures/remote.yaml

This file was deleted.

File renamed without changes.
16 changes: 16 additions & 0 deletions tests/parser/macro-fixtures/inner-tackle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

stuff: things

more_things->: var "lots of {{stuff}}" -if "stuff == 'things'"

foo:
- bar
- baz

foo_items->: var item -for foo
foo_index->: var index -for foo

calling_file->: "{{calling_file}}"

#inner:
# type: print this
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions tests/parser/test_parser_args_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest

from tackle import tackle

# from tackle.parser import handle_leading_brackets
from tackle.macros import var_hook_macro
from tackle.utils.command import unpack_args_kwargs_string

Expand All @@ -21,7 +19,7 @@

@pytest.mark.parametrize("template,len_args,len_kwargs,len_flags", TEMPLATES)
def test_unpack_args_kwargs_handle_leading_brackets(
template, len_args, len_kwargs, len_flags
template, len_args, len_kwargs, len_flags
):
"""Validate the count of each input arg/kwarg/flag."""
args, kwargs, flags = unpack_args_kwargs_string(template)
Expand Down
Loading

0 comments on commit 2ab1ad1

Please sign in to comment.