Skip to content

Commit

Permalink
Switch to utility open functions
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed May 31, 2017
1 parent d6cdb88 commit 7b92738
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion molecule/config.py
Expand Up @@ -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))

Expand Down
3 changes: 1 addition & 2 deletions molecule/provisioner/ansible/plugins/libraries/goss.py
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion test/unit/command/test_base.py
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion test/unit/command/test_init.py
Expand Up @@ -23,6 +23,7 @@

import pytest

from molecule import util
from molecule.command import init


Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/unit/lint/test_trailing.py
Expand Up @@ -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())
12 changes: 9 additions & 3 deletions test/unit/test_util.py
Expand Up @@ -21,6 +21,7 @@
from __future__ import print_function

import binascii
import io
import os

import colorama
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 7b92738

Please sign in to comment.