diff --git a/src/rocker/core.py b/src/rocker/core.py index cb667da..b1aa3c8 100644 --- a/src/rocker/core.py +++ b/src/rocker/core.py @@ -422,7 +422,8 @@ def write_files(extensions, args_dict, target_directory): 'and cannot be written out, skipping' % (file_path, active_extension.get_name())) continue Path(os.path.dirname(full_path)).mkdir(exist_ok=True, parents=True) - with open(full_path, 'w') as fh: + mode = 'wb' if isinstance(contents, bytes) else 'w' # check to see if contents should be written as binary + with open(full_path, mode) as fh: print('Writing to file %s' % full_path) fh.write(contents) return all_files diff --git a/test/test_file_writing.py b/test/test_file_writing.py index ceca690..899b766 100644 --- a/test/test_file_writing.py +++ b/test/test_file_writing.py @@ -50,6 +50,7 @@ def get_files(self, cliargs): all_files = {} all_files['test_file.txt'] = """The quick brown fox jumped over the lazy dog. %s""" % cliargs all_files['path/to/test_file.txt'] = """The quick brown fox jumped over the lazy dog. %s""" % cliargs + all_files['test_file.bin'] = bytes("""The quick brown fox jumped over the lazy dog. %s""" % cliargs, 'utf-8') all_files['../outside/path/to/test_file.txt'] = """Path outside directory should be skipped""" all_files['/absolute.txt'] = """Absolute file path should be skipped""" return all_files @@ -96,5 +97,11 @@ def test_file_injection(self): self.assertIn('test_key', content) self.assertIn('test_value', content) + with open(os.path.join(td, 'test_file.bin'), 'r') as fh: # this particular binary file can be read in text mode + content = fh.read() + self.assertIn('quick brown', content) + self.assertIn('test_key', content) + self.assertIn('test_value', content) + self.assertFalse(os.path.exists('../outside/path/to/test_file.txt')) self.assertFalse(os.path.exists('/absolute.txt'))