-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
769 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright © Spyder Project Contributors | ||
# Licensed under the terms of the MIT License | ||
# (see spyder/__init__.py for details) | ||
""" | ||
Cookiecutter utilities. | ||
""" | ||
import json | ||
import os | ||
|
||
from cookiecutter.main import cookiecutter | ||
|
||
|
||
def generate_cookiecutter_project(cookiecutter_path, output_path, | ||
extra_content=None): | ||
""" | ||
Generate a cookicutter project programmatically. | ||
""" | ||
status = True | ||
try: | ||
result = cookiecutter( | ||
cookiecutter_path, | ||
output_dir=output_path, | ||
overwrite_if_exists=True, | ||
extra_context=extra_content, | ||
no_input=True, | ||
) | ||
except Exception as err: | ||
result = err | ||
status = False | ||
|
||
return status, result | ||
|
||
|
||
def load_cookiecutter_project(project_path): | ||
""" | ||
Load a cookicutter options and pre-hook script. | ||
""" | ||
options = None | ||
pre_gen_code = None | ||
cookiepath = os.path.join(project_path, "cookiecutter.json") | ||
pre_gen_path = os.path.join(project_path, "hooks", "pre_gen_project.py") | ||
|
||
if os.path.isdir(project_path): | ||
if os.path.isfile(cookiepath): | ||
with open(cookiepath, 'r') as fh: | ||
options = json.loads(fh.read()) | ||
|
||
if os.path.isfile(pre_gen_path): | ||
with open(pre_gen_path, 'r') as fh: | ||
pre_gen_code = fh.read() | ||
|
||
return options, pre_gen_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) Spyder Project Contributors | ||
# | ||
# Licensed under the terms of the MIT License | ||
# (see LICENSE.txt for details) | ||
# ----------------------------------------------------------------------------- | ||
|
||
"""Tests.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright © Spyder Project Contributors | ||
# Licensed under the terms of the MIT License | ||
# | ||
""" | ||
Tests for qcookiecutter widget. | ||
""" | ||
# Standard library imports | ||
import json | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
# Third party imports | ||
import pytest | ||
|
||
# Local imports | ||
from spyder.plugins.projects.utils.cookie import (generate_cookiecutter_project, | ||
load_cookiecutter_project) | ||
|
||
|
||
def test_load_cookiecutter_project_config(): | ||
settings = { | ||
"opt_1": "value", | ||
"opt_2": "{{ cookiecutter.opt_1 }}", | ||
} | ||
temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') | ||
temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') | ||
|
||
with open(temp_cookie_path, 'w') as fh: | ||
fh.write(json.dumps(settings, sort_keys=True)) | ||
|
||
sets, pre_gen_code = load_cookiecutter_project(temp_path) | ||
assert settings == sets | ||
assert pre_gen_code is None | ||
|
||
shutil.rmtree(temp_path) | ||
|
||
|
||
def test_load_cookiecutter_project_hooks(): | ||
settings = { | ||
"opt_1": "value", | ||
"opt_2": "{{ cookiecutter.opt_1 }}", | ||
} | ||
pre_gen_code = "import sys\n\nprint('test!')\nsys.exit(1)\n" | ||
temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') | ||
temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') | ||
temp_hooks_path = os.path.join(temp_path, 'hooks') | ||
temp_hooks_pre_path = os.path.join(temp_hooks_path, 'pre_gen_project.py') | ||
os.makedirs(temp_hooks_path) | ||
|
||
with open(temp_cookie_path, 'w') as fh: | ||
fh.write(json.dumps(settings, sort_keys=True)) | ||
|
||
with open(temp_hooks_pre_path, 'w') as fh: | ||
fh.write(pre_gen_code) | ||
|
||
sets, pre_gen_code = load_cookiecutter_project(temp_path) | ||
assert settings == sets | ||
assert pre_gen_code == pre_gen_code | ||
|
||
shutil.rmtree(temp_path) | ||
|
||
|
||
def test_generate_cookiecutter_project_defaults(): | ||
settings = { | ||
"repo_name": "value", | ||
} | ||
temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') | ||
temp_path_created = tempfile.mkdtemp(suffix='-created-project') | ||
temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') | ||
temp_project_path = os.path.join(temp_path, '{{cookiecutter.repo_name}}') | ||
os.makedirs(temp_project_path) | ||
|
||
with open(temp_cookie_path, 'w') as fh: | ||
fh.write(json.dumps(settings, sort_keys=True)) | ||
|
||
status, result = generate_cookiecutter_project( | ||
temp_path, | ||
temp_path_created, | ||
) | ||
assert "value" in result | ||
assert status is True | ||
shutil.rmtree(temp_path) | ||
|
||
|
||
def test_generate_cookiecutter_project_extra_content(): | ||
settings = { | ||
"repo_name": "value", | ||
} | ||
temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') | ||
temp_path_created = tempfile.mkdtemp(suffix='-created-project') | ||
temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') | ||
temp_project_path = os.path.join(temp_path, '{{cookiecutter.repo_name}}') | ||
os.makedirs(temp_project_path) | ||
|
||
with open(temp_cookie_path, 'w') as fh: | ||
fh.write(json.dumps(settings, sort_keys=True)) | ||
|
||
status, result = generate_cookiecutter_project( | ||
temp_path, | ||
temp_path_created, | ||
{"repo_name": "boom"}, | ||
) | ||
assert "boom" in result | ||
assert status is True | ||
shutil.rmtree(temp_path) | ||
|
||
def test_generate_cookiecutter_project_exception(): | ||
settings = { | ||
"repo_name": "value", | ||
} | ||
temp_path = tempfile.mkdtemp(suffix='-some-invalid-cookiecutter') | ||
temp_path_created = tempfile.mkdtemp(suffix='-created-project') | ||
temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') | ||
temp_project_path = os.path.join(temp_path, '{{cookiecutter.not_foun_variable}}') | ||
os.makedirs(temp_project_path) | ||
|
||
with open(temp_cookie_path, 'w') as fh: | ||
fh.write(json.dumps(settings, sort_keys=True)) | ||
|
||
status, __ = generate_cookiecutter_project( | ||
temp_path, | ||
temp_path_created, | ||
) | ||
assert status is False | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |
Oops, something went wrong.