Skip to content

Commit

Permalink
fix: install requirements.txt install when there is a ModuleNotFound …
Browse files Browse the repository at this point in the history
…error on importing a provider's hook
  • Loading branch information
robcxyz committed Nov 8, 2022
1 parent 061739c commit 047f542
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion tackle/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def import_from_path(
context: 'Context',
provider_path: str,
hooks_dir_name: str = None,
skip_on_error: bool = True,
):
"""Append a provider with a given path."""
# Look for `.hooks` or `hooks` dir
Expand Down Expand Up @@ -102,7 +103,9 @@ def import_from_path(
context.private_hooks[h] = hook

# This pass will import all the modules and extract hooks
import_with_fallback_install(context, mod_name, hooks_path, skip_on_error=True)
import_with_fallback_install(
context=context, mod_name=mod_name, path=hooks_path, skip_on_error=skip_on_error
)


def get_native_provider_paths():
Expand Down
4 changes: 2 additions & 2 deletions tackle/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,8 @@ def extract_base_file(context: 'Context'):
context.public_context = {}
context.private_context = {}

# Import the hooks
import_from_path(context, context.input_dir)
# Import the hooks and install requirements.txt if there is a ModuleNotFound error
import_from_path(context, context.input_dir, skip_on_error=False)


def import_local_provider_source(context: 'Context', provider_dir: str):
Expand Down
9 changes: 9 additions & 0 deletions tests/models/fixtures/test-provider-reqs/hooks/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from tackle import BaseHook
from art import art


class ArtHook(BaseHook):
hook_type: str = 'art'

def exec(self):
return art("random")
1 change: 1 addition & 0 deletions tests/models/fixtures/test-provider-reqs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
art
1 change: 1 addition & 0 deletions tests/models/fixtures/test-provider-reqs/tackle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
art->: art
20 changes: 20 additions & 0 deletions tests/models/test_models_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def test_parser_provider_import_installs_requirements(
temporary_uninstall('requests')
try:
import requests

assert False
except ImportError:
assert True

Expand All @@ -46,6 +48,24 @@ def test_parser_provider_import_installs_requirements(
assert requests


def test_parser_provider_import_requirements_installs_requirements(
chdir, temporary_uninstall
):
"""Validate that if a package is missing, that it will be installed and usable."""
chdir(os.path.join('fixtures', 'test-provider-reqs'))
temporary_uninstall('art')
try:
import art

assert False
except ImportError:
assert True

o = tackle()
temporary_uninstall('art')
assert o['art']


def test_parser_hooks_raises_error_on_unknown_hook_type(change_curdir_fixtures):
"""Verify raising error.
Expand Down

0 comments on commit 047f542

Please sign in to comment.