|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright © Spyder Project Contributors |
| 4 | +# Licensed under the terms of the MIT License |
| 5 | +# |
| 6 | + |
| 7 | +""" |
| 8 | +Tests for qcookiecutter widget. |
| 9 | +""" |
| 10 | + |
| 11 | +# Standard library imports |
| 12 | +import json |
| 13 | +import os |
| 14 | +import shutil |
| 15 | +import tempfile |
| 16 | + |
| 17 | +# Third party imports |
| 18 | +import pytest |
| 19 | + |
| 20 | +# Local imports |
| 21 | +from spyder.plugins.projects.utils.cookie import ( |
| 22 | + generate_cookiecutter_project, load_cookiecutter_project) |
| 23 | + |
| 24 | + |
| 25 | +def test_load_cookiecutter_project_config(): |
| 26 | + settings = { |
| 27 | + "opt_1": "value", |
| 28 | + "opt_2": "{{ cookiecutter.opt_1 }}", |
| 29 | + } |
| 30 | + temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') |
| 31 | + temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') |
| 32 | + |
| 33 | + with open(temp_cookie_path, 'w') as fh: |
| 34 | + fh.write(json.dumps(settings, sort_keys=True)) |
| 35 | + |
| 36 | + sets, pre_gen_code = load_cookiecutter_project(temp_path) |
| 37 | + assert settings == sets |
| 38 | + assert pre_gen_code is None |
| 39 | + |
| 40 | + shutil.rmtree(temp_path) |
| 41 | + |
| 42 | + |
| 43 | +def test_load_cookiecutter_project_hooks(): |
| 44 | + settings = { |
| 45 | + "opt_1": "value", |
| 46 | + "opt_2": "{{ cookiecutter.opt_1 }}", |
| 47 | + } |
| 48 | + pre_gen_code = "import sys\n\nprint('test!')\nsys.exit(1)\n" |
| 49 | + temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') |
| 50 | + temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') |
| 51 | + temp_hooks_path = os.path.join(temp_path, 'hooks') |
| 52 | + temp_hooks_pre_path = os.path.join(temp_hooks_path, 'pre_gen_project.py') |
| 53 | + os.makedirs(temp_hooks_path) |
| 54 | + |
| 55 | + with open(temp_cookie_path, 'w') as fh: |
| 56 | + fh.write(json.dumps(settings, sort_keys=True)) |
| 57 | + |
| 58 | + with open(temp_hooks_pre_path, 'w') as fh: |
| 59 | + fh.write(pre_gen_code) |
| 60 | + |
| 61 | + sets, pre_gen_code = load_cookiecutter_project(temp_path) |
| 62 | + assert settings == sets |
| 63 | + assert pre_gen_code == pre_gen_code |
| 64 | + |
| 65 | + shutil.rmtree(temp_path) |
| 66 | + |
| 67 | + |
| 68 | +def test_generate_cookiecutter_project_defaults(): |
| 69 | + settings = { |
| 70 | + "repo_name": "value", |
| 71 | + } |
| 72 | + temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') |
| 73 | + temp_path_created = tempfile.mkdtemp(suffix='-created-project') |
| 74 | + temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') |
| 75 | + temp_project_path = os.path.join(temp_path, '{{cookiecutter.repo_name}}') |
| 76 | + os.makedirs(temp_project_path) |
| 77 | + |
| 78 | + with open(temp_cookie_path, 'w') as fh: |
| 79 | + fh.write(json.dumps(settings, sort_keys=True)) |
| 80 | + |
| 81 | + status, result = generate_cookiecutter_project( |
| 82 | + temp_path, |
| 83 | + temp_path_created, |
| 84 | + ) |
| 85 | + assert "value" in result |
| 86 | + assert status is True |
| 87 | + shutil.rmtree(temp_path) |
| 88 | + |
| 89 | + |
| 90 | +def test_generate_cookiecutter_project_extra_content(): |
| 91 | + settings = { |
| 92 | + "repo_name": "value", |
| 93 | + } |
| 94 | + temp_path = tempfile.mkdtemp(suffix='-some-cookiecutter') |
| 95 | + temp_path_created = tempfile.mkdtemp(suffix='-created-project') |
| 96 | + temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') |
| 97 | + temp_project_path = os.path.join(temp_path, '{{cookiecutter.repo_name}}') |
| 98 | + os.makedirs(temp_project_path) |
| 99 | + |
| 100 | + with open(temp_cookie_path, 'w') as fh: |
| 101 | + fh.write(json.dumps(settings, sort_keys=True)) |
| 102 | + |
| 103 | + status, result = generate_cookiecutter_project( |
| 104 | + temp_path, |
| 105 | + temp_path_created, |
| 106 | + {"repo_name": "boom"}, |
| 107 | + ) |
| 108 | + assert "boom" in result |
| 109 | + assert status is True |
| 110 | + shutil.rmtree(temp_path) |
| 111 | + |
| 112 | + |
| 113 | +def test_generate_cookiecutter_project_exception(): |
| 114 | + settings = { |
| 115 | + "repo_name": "value", |
| 116 | + } |
| 117 | + temp_path = tempfile.mkdtemp(suffix='-some-invalid-cookiecutter') |
| 118 | + temp_path_created = tempfile.mkdtemp(suffix='-created-project') |
| 119 | + temp_cookie_path = os.path.join(temp_path, 'cookiecutter.json') |
| 120 | + temp_project_path = os.path.join( |
| 121 | + temp_path, |
| 122 | + '{{cookiecutter.not_foun_variable}}', |
| 123 | + ) |
| 124 | + os.makedirs(temp_project_path) |
| 125 | + |
| 126 | + with open(temp_cookie_path, 'w') as fh: |
| 127 | + fh.write(json.dumps(settings, sort_keys=True)) |
| 128 | + |
| 129 | + status, __ = generate_cookiecutter_project( |
| 130 | + temp_path, |
| 131 | + temp_path_created, |
| 132 | + ) |
| 133 | + assert status is False |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + pytest.main() |
0 commit comments