Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove config import from init to fix scaffold usage #372

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# cookiecutter-data-science Changelog

## UNRELEASED

- Fixes issue with scaffold code that import of config did not work. Adds testing of imports to test suite. (Issue [#370](https://github.com/drivendataorg/cookiecutter-data-science/issues/370))

## v2.0.0 (2024-05-22)

- Released version 2.0.0! :tada: See [docs](https://cookiecutter-data-science.drivendata.org/) and [announcement blog post](https://drivendata.co/blog/ccds-v2) for more information.
3 changes: 3 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@
shutil.rmtree(generated_path)
elif generated_path.name != "__init__.py":
generated_path.unlink()
elif generated_path.name == "__init__.py":
# remove any content in __init__.py since it won't be available
generated_path.write_text("")
# {% endif %}
3 changes: 2 additions & 1 deletion tests/conda_harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ eval "$(conda shell.bash hook)"

PROJECT_NAME=$(basename $1)
CCDS_ROOT=$(dirname $0)
MODULE_NAME=$2

# configure exit / teardown behavior
function finish {
Expand Down Expand Up @@ -34,4 +35,4 @@ make create_environment
conda activate $PROJECT_NAME
make requirements

run_tests $PROJECT_NAME
run_tests $PROJECT_NAME $MODULE_NAME
9 changes: 9 additions & 0 deletions tests/pipenv_harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -ex

PROJECT_NAME=$(basename $1)
CCDS_ROOT=$(dirname $0)
MODULE_NAME=$2

# configure exit / teardown behavior
function finish {
Expand All @@ -27,3 +28,11 @@ make requirements

# test with pipenv run
pipenv run python -c "import sys; assert \"$PROJECT_NAME\" in sys.executable"

# test importable
pipenv run python -c "import $MODULE_NAME"

# test config importable if scaffolded
if [ -f "$MODULE_NAME/config.py" ]; then
pipenv run python -c "from $MODULE_NAME import config"
fi
62 changes: 30 additions & 32 deletions tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
BASH_EXECUTABLE = os.getenv("BASH_EXECUTABLE", "bash")


def _decode_print_stdout_stderr(result):
"""Print command stdout and stderr to console to use when debugging failing tests
Normally hidden by pytest except in failure we want this displayed
"""
encoding = sys.stdout.encoding

if encoding is None:
encoding = "utf-8"

print("\n======================= STDOUT ======================")
stdout = result.stdout.decode(encoding)
print(stdout)

print("\n======================= STDERR ======================")
stderr = result.stderr.decode(encoding)
print(stderr)

return stdout, stderr


def no_curlies(filepath):
"""Utility to make sure no curly braces appear in a file.
That is, was Jinja able to render everything?
Expand Down Expand Up @@ -154,31 +174,23 @@ def verify_makefile_commands(root, config):
)

result = run(
[BASH_EXECUTABLE, str(harness_path), str(root.resolve())],
[
BASH_EXECUTABLE,
str(harness_path),
str(root.resolve()),
str(config["module_name"]),
],
stderr=PIPE,
stdout=PIPE,
)
result_returncode = result.returncode

encoding = sys.stdout.encoding

if encoding is None:
encoding = "utf-8"

# normally hidden by pytest except in failure we want this displayed
print("PATH=", os.getenv("PATH"))
print("\n======================= STDOUT ======================")
stdout_output = result.stdout.decode(encoding)
print(stdout_output)

print("\n======================= STDERR ======================")
print(result.stderr.decode(encoding))
stdout_output, _ = _decode_print_stdout_stderr(result)

# Check that makefile help ran successfully
assert "Available rules:" in stdout_output
assert "clean Delete all compiled Python files" in stdout_output

assert result_returncode == 0
assert result.returncode == 0


def lint(root):
Expand All @@ -189,20 +201,6 @@ def lint(root):
stderr=PIPE,
stdout=PIPE,
)
result_returncode = result.returncode

encoding = sys.stdout.encoding

if encoding is None:
encoding = "utf-8"

# normally hidden by pytest except in failure we want this displayed
print("PATH=", os.getenv("PATH"))
print("\n======================= STDOUT ======================")
stdout_output = result.stdout.decode(encoding)
print(stdout_output)

print("\n======================= STDERR ======================")
print(result.stderr.decode(encoding))
_, _ = _decode_print_stdout_stderr(result)

assert result_returncode == 0
assert result.returncode == 0
7 changes: 7 additions & 0 deletions tests/test_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ function run_tests () {
exit 99
fi

# test importable
python -c "import $2"

# test config importable if scaffolded
if [ -f "$2/config.py" ]; then
python -c "from $2 import config"
fi
}
4 changes: 3 additions & 1 deletion tests/virtualenv_harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -e

PROJECT_NAME=$(basename $1)
CCDS_ROOT=$(dirname $0)
MODULE_NAME=$2

# configure exit / teardown behavior
function finish {
Expand Down Expand Up @@ -55,4 +56,5 @@ fi

make requirements

run_tests $PROJECT_NAME
run_tests $PROJECT_NAME $MODULE_NAME

Original file line number Diff line number Diff line change
@@ -1 +1 @@
import config # noqa: F401
from {{ cookiecutter.module_name }} import config # noqa: F401
Loading