diff --git a/molecule/config.py b/molecule/config.py index 1deaa77ba..3a70f21c5 100644 --- a/molecule/config.py +++ b/molecule/config.py @@ -224,7 +224,7 @@ def _combine(self): os.environ) base = self._get_defaults() - with open(self.molecule_file, 'r') as stream: + with util.open_file(self.molecule_file) as stream: interpolated_config = i.interpolate(stream.read()) base = self.merge_dicts(base, util.safe_load(interpolated_config)) diff --git a/molecule/provisioner/ansible/plugins/libraries/goss.py b/molecule/provisioner/ansible/plugins/libraries/goss.py index 5ef1ae4eb..c4cc5360d 100644 --- a/molecule/provisioner/ansible/plugins/libraries/goss.py +++ b/molecule/provisioner/ansible/plugins/libraries/goss.py @@ -69,8 +69,7 @@ def check(module, test_file_path, output_format, goss_path): # write goss result to output_file_path def output_file(output_file_path, out): if output_file_path is not None: - with open(output_file_path, 'w') as output_file: - output_file.write(out) + util.write_file(output_file_path, output_file.write(out)) def main(): diff --git a/test/unit/command/test_base.py b/test/unit/command/test_base.py index 2597f8a6b..fee63264b 100644 --- a/test/unit/command/test_base.py +++ b/test/unit/command/test_base.py @@ -63,7 +63,7 @@ def test_prune(base_instance): os.mkdir(baz_directory) for f in [foo_file, bar_file, state_file]: - open(f, 'a').close() + util.write_file(f, '') base_instance.prune() diff --git a/test/unit/command/test_init.py b/test/unit/command/test_init.py index 817a9e5ef..eda0b4731 100644 --- a/test/unit/command/test_init.py +++ b/test/unit/command/test_init.py @@ -23,6 +23,7 @@ import pytest +from molecule import util from molecule.command import init @@ -37,7 +38,7 @@ def test_process_templates(temp_dir): expected_file = os.path.join(temp_dir.strpath, repo_name, 'template.yml') expected_contents = '- value: foo' - with open(expected_file, 'r') as stream: + with util.open_file(expected_file) as stream: for line in stream.readlines(): assert line.strip() in expected_contents diff --git a/test/unit/lint/test_trailing.py b/test/unit/lint/test_trailing.py index 8f3dc2687..2de2f5f16 100644 --- a/test/unit/lint/test_trailing.py +++ b/test/unit/lint/test_trailing.py @@ -175,7 +175,7 @@ def test_get_tests(trailing_instance): '.foo/foo.yml', '.bar/foo.yml', ]: - open(os.path.join(project_directory, f), 'a').close() + util.write_file(os.path.join(project_directory, f), '') # NOTE(retr0h): Unit tests add a molecule.yml automatically. assert 4 == len(trailing_instance._get_tests()) diff --git a/test/unit/test_util.py b/test/unit/test_util.py index 6bcb57d7f..c9ecfccc7 100644 --- a/test/unit/test_util.py +++ b/test/unit/test_util.py @@ -21,6 +21,7 @@ from __future__ import print_function import binascii +import io import os import colorama @@ -168,7 +169,7 @@ def test_os_walk(temp_dir): scenario_directory = os.path.join(molecule_directory, scenario) molecule_file = pytest.helpers.get_molecule_file(scenario_directory) os.makedirs(scenario_directory) - open(molecule_file, 'a').close() + util.write_file(molecule_file, '') result = [f for f in util.os_walk(molecule_directory, 'molecule.yml')] assert 3 == len(result) @@ -184,7 +185,7 @@ def test_write_file(temp_dir): dest_file = os.path.join(temp_dir.strpath, 'test_util_write_file.tmp') contents = binascii.b2a_hex(os.urandom(15)).decode() util.write_file(dest_file, contents) - with open(dest_file, 'r') as stream: + with util.open_file(dest_file) as stream: data = stream.read() x = '# Molecule managed\n\n{}'.format(contents) @@ -217,7 +218,12 @@ def test_open_file(temp_dir): util.write_file(path, 'foo: bar') with util.open_file(path) as stream: - assert type(stream) is file + try: + file_types = (file, io.IOBase) + except NameError: + file_types = io.IOBase + + assert isinstance(stream, file_types) def test_instance_with_scenario_name():