From 23f7b23ab9057662c873a8b41d448675e54b1642 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 10:42:51 -0500 Subject: [PATCH 1/7] Handle all failures when pwd does not exist. Signed-off-by: James Tanner --- dynaconf/cli.py | 6 +++++- dynaconf/loaders/env_loader.py | 10 +++++++++- dynaconf/utils/files.py | 5 ++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dynaconf/cli.py b/dynaconf/cli.py index 1490f3787..fe24c98d4 100644 --- a/dynaconf/cli.py +++ b/dynaconf/cli.py @@ -29,7 +29,11 @@ os.environ["PYTHONIOENCODING"] = "utf-8" -CWD = Path.cwd() +CWD=None +try: + CWD = Path.cwd() +except FileNotFoundError: + pass EXTS = ["ini", "toml", "yaml", "json", "py", "env"] WRITERS = ["ini", "toml", "yaml", "json", "py", "redis", "vault", "env"] diff --git a/dynaconf/loaders/env_loader.py b/dynaconf/loaders/env_loader.py index 04050e4a5..28041dd74 100644 --- a/dynaconf/loaders/env_loader.py +++ b/dynaconf/loaders/env_loader.py @@ -5,7 +5,15 @@ from dynaconf.utils import missing from dynaconf.utils import upperfy from dynaconf.utils.parse_conf import parse_conf_data -from dynaconf.vendor.dotenv import cli as dotenv_cli + +DOTENV_IMPORTED=False +try: + from dynaconf.vendor.dotenv import cli as dotenv_cli + DOTENV_IMPORTED=True +except ImportError: + pass +except FileNotFoundError: + pass IDENTIFIER = "env" diff --git a/dynaconf/utils/files.py b/dynaconf/utils/files.py index 135880ac9..ec6fbd851 100644 --- a/dynaconf/utils/files.py +++ b/dynaconf/utils/files.py @@ -47,7 +47,10 @@ def find_file(filename=".env", project_root=None, skip_files=None, **kwargs): additional `./config` folder. """ search_tree = [] - work_dir = os.getcwd() + try: + work_dir = os.getcwd() + except FileNotFoundError: + return "" skip_files = skip_files or [] # If filename is an absolute path and exists, just return it From 6712be9664322a095ff227295d2994ba767fd646 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 10:53:01 -0500 Subject: [PATCH 2/7] Do not write to .env if dotenv wasn't imported. Signed-off-by: James Tanner --- dynaconf/loaders/env_loader.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynaconf/loaders/env_loader.py b/dynaconf/loaders/env_loader.py index 28041dd74..42bf08a08 100644 --- a/dynaconf/loaders/env_loader.py +++ b/dynaconf/loaders/env_loader.py @@ -92,6 +92,8 @@ def load_from_env( def write(settings_path, settings_data, **kwargs): """Write data to .env file""" + if not DOTENV_IMPORTED: + return for key, value in settings_data.items(): quote_mode = ( isinstance(value, str) From 5d9a9f2c2e2e62e2a2714aa5c4ac6bde3121b559 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 11:14:27 -0500 Subject: [PATCH 3/7] Add functional test & allow test listing or filtering. Signed-off-by: James Tanner --- tests_functional/missing_pwd/test.sh | 12 ++++++ tests_functional/runtests.py | 58 +++++++++++++++++++++------- 2 files changed, 57 insertions(+), 13 deletions(-) create mode 100755 tests_functional/missing_pwd/test.sh diff --git a/tests_functional/missing_pwd/test.sh b/tests_functional/missing_pwd/test.sh new file mode 100755 index 000000000..800774e80 --- /dev/null +++ b/tests_functional/missing_pwd/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +rm -rf /tmp/foobar +mkdir -p /tmp/foobar +cd /tmp/foobar +rm -rf /tmp/foobar + +dynaconf -i config.settings list +RC=$? +exit $RC diff --git a/tests_functional/runtests.py b/tests_functional/runtests.py index 29449cfc0..b97f226d5 100755 --- a/tests_functional/runtests.py +++ b/tests_functional/runtests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from __future__ import annotations +import argparse import os import subprocess import sys @@ -65,22 +66,35 @@ def execute_tests(path): return False -def run_tests(): +def run_tests(show_list=False, test_filter=None): + + def filter_allowed(testpath): + if not test_filter: + return True + for keyword in test_filter: + if keyword in testpath.name: + return True + return False + passed = 0 root_directory = Path(__file__).parent print("Workdir:", root_directory.absolute()) functional_tests = sorted(list(root_directory.iterdir())) print("Collected functional tests:", len(functional_tests)) sleep(1) + for path in functional_tests: if path.is_dir(): if path.name in [".", "__pycache__"]: continue - if execute_tests(path): - passed += 1 - print(f"Passed {path}") - continue + if show_list: + print(path) + else: + if filter_allowed(path) and execute_tests(path): + passed += 1 + print(f"Passed {path}") + continue # Now Subdirectories one level subdirs = sorted(list(path.iterdir())) @@ -89,10 +103,13 @@ def run_tests(): if subdir.name in [".", "__pycache__"]: continue - if execute_tests(subdir): - passed += 1 - print(f"Passed {subdir}") - continue + if show_list: + print(subdir) + else: + if filter_allowed(subdir) and execute_tests(subdir): + passed += 1 + print(f"Passed {subdir}") + continue if not subdirs: exit( @@ -100,10 +117,25 @@ def run_tests(): "Makefile, test.sh, app.py, test.py, program.py" ) - print("-" * 40) - print(f"{passed} functional tests passed") - print("-" * 40) + if not show_list: + print("-" * 40) + print(f"{passed} functional tests passed") + print("-" * 40) if __name__ == "__main__": - run_tests() + parser = argparse.ArgumentParser() + parser.add_argument( + '--list', + dest='show_list', + action='store_true', + help='List all available tests (do not execute)' + ) + parser.add_argument( + '--filter', + dest='test_filter', + action='append', + help='limt tests to those that contain a substring' + ) + args = parser.parse_args() + run_tests(show_list=args.show_list, test_filter=args.test_filter) From f5d84f5dcfd02bf310d4a1fa122d5641cf7a487b Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 11:16:07 -0500 Subject: [PATCH 4/7] Make functional test pass. Signed-off-by: James Tanner --- dynaconf/cli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynaconf/cli.py b/dynaconf/cli.py index fe24c98d4..8702ab6b4 100644 --- a/dynaconf/cli.py +++ b/dynaconf/cli.py @@ -120,6 +120,8 @@ def import_settings(dotted_path): module = importlib.import_module(module) except ImportError as e: raise click.UsageError(e) + except FileNotFoundError: + return try: return getattr(module, name) except AttributeError as e: From 6b3d31e4383a46c640499fab06de529519b668e4 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 11:26:35 -0500 Subject: [PATCH 5/7] Fix flake8 url to github. Signed-off-by: James Tanner --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bdb051c09..a19aaf170 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: hooks: - id: black args: [--line-length=79] - - repo: https://gitlab.com/pycqa/flake8 + - repo: https://github.com/pycqa/flake8 rev: 3.9.2 hooks: - id: flake8 From c34eb9cb2b735e36b949ae95f3301c4aa8a45d45 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 11:41:11 -0500 Subject: [PATCH 6/7] Fix lint problems. Signed-off-by: James Tanner --- dynaconf/cli.py | 2 +- dynaconf/loaders/env_loader.py | 5 +++-- tests_functional/missing_pwd/test.sh | 2 +- tests_functional/runtests.py | 17 ++++++++--------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dynaconf/cli.py b/dynaconf/cli.py index 8702ab6b4..8b8ab5d53 100644 --- a/dynaconf/cli.py +++ b/dynaconf/cli.py @@ -29,7 +29,7 @@ os.environ["PYTHONIOENCODING"] = "utf-8" -CWD=None +CWD = None try: CWD = Path.cwd() except FileNotFoundError: diff --git a/dynaconf/loaders/env_loader.py b/dynaconf/loaders/env_loader.py index 42bf08a08..779e9a4f6 100644 --- a/dynaconf/loaders/env_loader.py +++ b/dynaconf/loaders/env_loader.py @@ -6,10 +6,11 @@ from dynaconf.utils import upperfy from dynaconf.utils.parse_conf import parse_conf_data -DOTENV_IMPORTED=False +DOTENV_IMPORTED = False try: from dynaconf.vendor.dotenv import cli as dotenv_cli - DOTENV_IMPORTED=True + + DOTENV_IMPORTED = True except ImportError: pass except FileNotFoundError: diff --git a/tests_functional/missing_pwd/test.sh b/tests_functional/missing_pwd/test.sh index 800774e80..457d1da71 100755 --- a/tests_functional/missing_pwd/test.sh +++ b/tests_functional/missing_pwd/test.sh @@ -7,6 +7,6 @@ mkdir -p /tmp/foobar cd /tmp/foobar rm -rf /tmp/foobar -dynaconf -i config.settings list +dynaconf -i config.settings list RC=$? exit $RC diff --git a/tests_functional/runtests.py b/tests_functional/runtests.py index b97f226d5..9b22da802 100755 --- a/tests_functional/runtests.py +++ b/tests_functional/runtests.py @@ -67,7 +67,6 @@ def execute_tests(path): def run_tests(show_list=False, test_filter=None): - def filter_allowed(testpath): if not test_filter: return True @@ -126,16 +125,16 @@ def filter_allowed(testpath): if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( - '--list', - dest='show_list', - action='store_true', - help='List all available tests (do not execute)' + "--list", + dest="show_list", + action="store_true", + help="List all available tests (do not execute)", ) parser.add_argument( - '--filter', - dest='test_filter', - action='append', - help='limt tests to those that contain a substring' + "--filter", + dest="test_filter", + action="append", + help="limt tests to those that contain a substring", ) args = parser.parse_args() run_tests(show_list=args.show_list, test_filter=args.test_filter) From 00e1b6046a089277d70752cfb1b287661a147335 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 31 Jan 2023 12:07:42 -0500 Subject: [PATCH 7/7] Skip missing_pwd test on windows. Signed-off-by: James Tanner --- tests_functional/skipfile.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests_functional/skipfile.toml b/tests_functional/skipfile.toml index 113a76a2b..ca55430aa 100644 --- a/tests_functional/skipfile.toml +++ b/tests_functional/skipfile.toml @@ -18,4 +18,5 @@ nt = [ "449_django_lazy_path", "685_disable_dotted_lookup", "741_envvars_ignored", + "missing_pwd", ]