Repository navigation
Use render-engine's init instead of cookiecutter #75
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,11 +55,10 @@ def test_import_lib_gets_site(): | |
| pass | ||
|
|
||
|
|
||
| def test_collection_call(tmp_path_factory): | ||
| def test_collection_call(tmp_path): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I'm not opposed to these changes they seem out of scope for this PR. |
||
| """Tests that you can get content from the parser using `new_entry`""" | ||
| test_collection = Collection() | ||
| content_path1 = tmp_path_factory.getbasetemp() | ||
| filepath = content_path1 / "test.md" | ||
| filepath = tmp_path / "test.md" | ||
| content = create_collection_entry( | ||
| collection=test_collection, content=None, foo="bar", filepath=filepath, editor=None | ||
| ) | ||
|
|
@@ -71,11 +70,10 @@ def test_collection_call(tmp_path_factory): | |
| assert post["foo"] == "bar" | ||
|
|
||
|
|
||
| def test_collection_call_with_content(tmp_path_factory): | ||
| def test_collection_call_with_content(tmp_path): | ||
| """Tests that you can get content from the parser using `new_entry`""" | ||
| test_collection = Collection() | ||
| content_path1 = tmp_path_factory.getbasetemp() | ||
| filepath = content_path1 / "test.md" | ||
| filepath = tmp_path / "test.md" | ||
| create_collection_entry( | ||
| content="This is a test", collection=test_collection, foo="bar", filepath=filepath, editor=None | ||
| ) | ||
|
|
@@ -160,11 +158,10 @@ def test_config_loading_invalid_file(tmp_path, monkeypatch, capsys): | |
| CliConfig().load_config(str(config_file)) | ||
|
|
||
|
|
||
| def test_collection_entry_with_custom_attributes(tmp_path_factory): | ||
| def test_collection_entry_with_custom_attributes(tmp_path): | ||
| """Tests that custom attributes are passed through to collection entry""" | ||
| test_collection = Collection() | ||
| content_path1 = tmp_path_factory.getbasetemp() | ||
| filepath = content_path1 / "test.md" | ||
| filepath = tmp_path / "test.md" | ||
|
|
||
| create_collection_entry( | ||
| content="Test content", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,109 +6,53 @@ | |
| from render_engine_cli.cli import app | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", autouse=True) | ||
| def get_test_template(tmp_path_factory): | ||
| path = tmp_path_factory.getbasetemp() | ||
| project_slug = path.joinpath("{{cookiecutter.project_slug}}") | ||
| project_slug.mkdir() | ||
| project_slug.joinpath("app.py").write_text("hello {{cookiecutter.world}}") | ||
| path.joinpath("cookiecutter.json").write_text(json.dumps({"world": "world", "project_slug": "testslug"})) | ||
|
|
||
| return path.resolve() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def runner(): | ||
| """CLI test runner fixture""" | ||
| yield CliRunner() | ||
|
|
||
|
|
||
| def tests_error_raised_if_cookiecutter_not_installed(request, mocker, runner, get_test_template): | ||
| """Tests that an error is raised if cookiecutter is not installed""" | ||
| def test_init_is_listed_in_the_help(runner): | ||
| """`render-engine --help` lists the init command.""" | ||
| result = runner.invoke(app, ["--help"]) | ||
|
|
||
| # Mock importing cookiecutter | ||
| mocker.patch("cookiecutter.main.cookiecutter", side_effect=ImportError) | ||
| assert result.exit_code == 0 | ||
| assert "Create a new Render Engine site." in result.output | ||
|
|
||
| result = runner.invoke( | ||
| app, | ||
| [ | ||
| "init", | ||
| "--extra-context", | ||
| json.dumps( | ||
| { | ||
| "project_slug": request.node.name, | ||
| } | ||
| ), | ||
| "--template", | ||
| get_test_template, | ||
| "--no-input", | ||
| ], | ||
| ) | ||
| assert isinstance(result.exception, ImportError) | ||
|
|
||
|
|
||
| def test_init_local_path_extra_context(request, tmp_path, get_test_template, runner): | ||
| """ | ||
| Tests that you can call init using a local path | ||
|
|
||
| It also tests that the extra_context is passed to the template. | ||
| """ | ||
| runner.invoke( | ||
| app, | ||
| [ | ||
| "init", | ||
| "--extra-context", | ||
| json.dumps( | ||
| { | ||
| "project_slug": request.node.name, | ||
| } | ||
| ), | ||
| "--template", | ||
| get_test_template, | ||
| "--no-input", | ||
| "--output-dir", | ||
| tmp_path, | ||
| ], | ||
| ) | ||
|
|
||
| template_path = tmp_path.joinpath(request.node.originalname) | ||
| app_py = template_path.joinpath("app.py") | ||
| assert app_py.exists() | ||
| assert app_py.read_bytes() == b"hello world" | ||
|
|
||
|
|
||
| def test_init_called_with_config(request, tmp_path, tmp_path_factory, runner): | ||
| """ | ||
| Tests that you can call init with cookiecutter configs passed in through a config file. | ||
| """ | ||
| temp_file = tmp_path.joinpath("test_cookiecutter_args.json") | ||
| temp_file.write_text( | ||
| f""" | ||
| default_context: | ||
| world: "Earth" | ||
| project_slug: {request.node.originalname} | ||
| """ | ||
| def test_init_passes_the_arguments_to_render_engine(runner, mocker): | ||
| """All arguments after `init` are passed to render-engine's init, with the CLI's command name.""" | ||
| main = mocker.patch("render_engine.init.__main__.main") | ||
|
|
||
| runner.invoke(app, ["init", "--no-input", "-o", "sites", "-e", '{"site_title": "My Blog"}']) | ||
|
|
||
| main.assert_called_once_with( | ||
| ["--no-input", "-o", "sites", "-e", '{"site_title": "My Blog"}'], prog="render-engine init" | ||
|
Comment on lines
+27
to
+30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be less error-prone, should a change need to be made, to have the list of arguments as a variable that we both pass and check. |
||
| ) | ||
| runner.invoke( | ||
| app, | ||
| [ | ||
| "init", | ||
| "--extra-context", | ||
| json.dumps( | ||
| { | ||
| "project_slug": request.node.name, | ||
| } | ||
| ), | ||
| "--template", | ||
| str(tmp_path_factory.getbasetemp().resolve()), | ||
| "--no-input", | ||
| "--output-dir", | ||
| tmp_path, | ||
| "--config-file", | ||
| temp_file, | ||
| ], | ||
|
|
||
|
|
||
| def test_init_help_shows_the_init_options(runner): | ||
| """`render-engine init --help` shows the options of render-engine's init, under the CLI's command name.""" | ||
| result = runner.invoke(app, ["init", "--help"]) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert "usage: render-engine init" in result.output | ||
| assert "--no-input" in result.output | ||
|
|
||
|
|
||
| def test_init_creates_a_site(runner, tmp_path): | ||
| """`render-engine init --no-input` creates a site from the answers in --extra-context.""" | ||
| result = runner.invoke( | ||
| app, ["init", "--no-input", "-o", str(tmp_path), "-e", json.dumps({"site_title": "My Blog"})] | ||
| ) | ||
| output_path = tmp_path.joinpath(request.node.originalname) | ||
| app_py = output_path.joinpath("app.py") | ||
| assert app_py.exists() | ||
| assert app_py.read_bytes() == b"hello Earth" | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
| assert (tmp_path / "my-blog" / "app.py").is_file() | ||
|
|
||
|
|
||
| def test_init_template_option_is_rejected(runner): | ||
| """Custom templates are no longer supported, so --template is an error.""" | ||
| result = runner.invoke(app, ["init", "--template", "https://github.com/me/my-template"]) | ||
|
|
||
| assert result.exit_code == 2 | ||
| assert "unrecognized arguments: --template" in result.output | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not leave
argsas a tuple? Is there benefit to changing it to alist?