From e1b92ab37c8abb60a6fed4f42779ed4f74be6ded Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Thu, 12 May 2022 21:26:56 +0000 Subject: [PATCH] Remove unused render_template_to_file method In a filesystem access code audit I found the method render_template_to_file is not used in the code. This patch removes it and it's tests. Change-Id: I0264349176763f9aa4a87750aca270a6acd2c529 --- designate/tests/unit/test_utils.py | 38 ------------------------------ designate/utils.py | 15 ------------ 2 files changed, 53 deletions(-) diff --git a/designate/tests/unit/test_utils.py b/designate/tests/unit/test_utils.py index aabaa8859..4734d0045 100644 --- a/designate/tests/unit/test_utils.py +++ b/designate/tests/unit/test_utils.py @@ -213,44 +213,6 @@ class TestUtils(oslotest.base.BaseTestCase): self.assertEqual('Hello World', result) - @mock.patch('builtins.open', new_callable=mock.mock_open) - @mock.patch('os.path.exists') - def test_render_template_to_file(self, mock_exists, mock_open): - mock_exists.return_value = True - - output_path = '/tmp/designate/resources/templates/hello.jinja2' - - template = jinja2.Template('Hello {{name}}') - - utils.render_template_to_file( - template, makedirs=False, output_path=output_path, name='World' - ) - - mock_open.assert_called_once_with(output_path, 'w') - mock_open().write.assert_called_once_with('Hello World') - - @mock.patch('builtins.open', new_callable=mock.mock_open) - @mock.patch('os.path.exists') - @mock.patch('os.makedirs') - def test_render_template_to_file_with_makedirs(self, mock_makedirs, - mock_exists, - mock_open): - mock_exists.return_value = False - - output_path = '/tmp/designate/resources/templates/hello.jinja2' - - template = jinja2.Template('Hello {{name}}') - - utils.render_template_to_file( - template, makedirs=True, output_path=output_path, name='World' - ) - - mock_makedirs.assert_called_once_with( - '/tmp/designate/resources/templates' - ) - mock_open.assert_called_once_with(output_path, 'w') - mock_open().write.assert_called_once_with('Hello World') - @mock.patch.object(timeutils, 'utcnow_ts') def test_increment_serial_lower_than_ts(self, mock_utcnow_ts): mock_utcnow_ts.return_value = 1561698354 diff --git a/designate/utils.py b/designate/utils.py index 629558600..a14a67413 100644 --- a/designate/utils.py +++ b/designate/utils.py @@ -110,21 +110,6 @@ def render_template(template, **template_context): return template.render(**template_context) -def render_template_to_file(template_name, output_path, makedirs=True, - **template_context): - output_folder = os.path.dirname(output_path) - - # Create the output folder tree if necessary - if makedirs and not os.path.exists(output_folder): - os.makedirs(output_folder) - - # Render the template - content = render_template(template_name, **template_context) - - with open(output_path, 'w') as output_fh: - output_fh.write(content) - - def execute(*cmd, **kw): """Execute a command in a subprocess, blocking. """