Skip to content

Commit

Permalink
Refactor template handler (#1151)
Browse files Browse the repository at this point in the history
In 0abe669, some unnecessary
refactoring occurred while fixing Issue #1148. This reverts that
refactoring and adds unit tests for the File handler.
  • Loading branch information
alexharv074 committed Nov 8, 2021
1 parent 0abe669 commit ca1407f
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/test_template_handlers/test_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-

import pytest

from sceptre.template_handlers.file import File
from unittest.mock import patch, mock_open


class TestFile(object):

@pytest.mark.parametrize("project_path,path,output_path", [
("my_project_dir", "my.template.yaml", "my_project_dir/templates/my.template.yaml"), # NOQA
("/src/my_project_dir", "my.template.yaml", "/src/my_project_dir/templates/my.template.yaml"), # NOQA
("my_project_dir", "/src/my_project_dir/templates/my.template.yaml", "/src/my_project_dir/templates/my.template.yaml"), # NOQA
("/src/my_project_dir", "/src/my_project_dir/templates/my.template.yaml", "/src/my_project_dir/templates/my.template.yaml"), # NOQA
])
@patch("builtins.open", new_callable=mock_open, read_data="some_data")
def test_handler_open(self, mocked_open, project_path, path, output_path):
template_handler = File(
name="file_handler",
arguments={"path": path},
stack_group_config={"project_path": project_path}
)
template_handler.handle()
mocked_open.assert_called_with(output_path)

@pytest.mark.parametrize("project_path,path,output_path", [
("my_project_dir", "my.template.yaml.j2", "my_project_dir/templates/my.template.yaml.j2"), # NOQA
("/src/my_project_dir", "my.template.yaml.j2", "/src/my_project_dir/templates/my.template.yaml.j2"), # NOQA
("my_project_dir", "/src/my_project_dir/templates/my.template.yaml.j2", "/src/my_project_dir/templates/my.template.yaml.j2"), # NOQA
("/src/my_project_dir", "/src/my_project_dir/templates/my.template.yaml.j2", "/src/my_project_dir/templates/my.template.yaml.j2"), # NOQA
])
@patch("sceptre.template_handlers.helper.render_jinja_template")
def test_handler_render(self, mocked_render, project_path, path, output_path):
template_handler = File(
name="file_handler",
arguments={"path": path},
stack_group_config={"project_path": project_path}
)
template_handler.handle()
mocked_render.assert_called_with(output_path, {"sceptre_user_data": None}, {})

@pytest.mark.parametrize("project_path,path,output_path", [
("my_project_dir", "my.template.yaml.py", "my_project_dir/templates/my.template.yaml.py"), # NOQA
("/src/my_project_dir", "my.template.yaml.py", "/src/my_project_dir/templates/my.template.yaml.py"), # NOQA
("my_project_dir", "/src/my_project_dir/templates/my.template.yaml.py", "/src/my_project_dir/templates/my.template.yaml.py"), # NOQA
("/src/my_project_dir", "/src/my_project_dir/templates/my.template.yaml.py", "/src/my_project_dir/templates/my.template.yaml.py"), # NOQA
])
@patch("sceptre.template_handlers.helper.call_sceptre_handler")
def test_handler_handler(self, mocked_handler, project_path, path, output_path):
template_handler = File(
name="file_handler",
arguments={"path": path},
stack_group_config={"project_path": project_path}
)
template_handler.handle()
mocked_handler.assert_called_with(output_path, None)

0 comments on commit ca1407f

Please sign in to comment.