From d20a9def8586467da361a52a2113a7eb6d2b05cb Mon Sep 17 00:00:00 2001 From: Alex Youngs Date: Mon, 13 Nov 2023 14:42:26 -0600 Subject: [PATCH 1/2] Allow binary files to be copied into the build context --- src/rocker/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From e7ab415245d340efadad73549e6c497053522a35 Mon Sep 17 00:00:00 2001 From: Alex Youngs Date: Mon, 13 Nov 2023 18:24:32 -0600 Subject: [PATCH 2/2] Update test --- test/test_file_writing.py | 7 +++++++ 1 file changed, 7 insertions(+) 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'))