def test_read_file_to_string_raises_UnicodeDecodeError_for_binary_file( self): _, path = tempfile.mkstemp() with open(path, "wb") as f: f.write(os.urandom(32)) with self.assertRaises(UnicodeDecodeError): util.read_file_to_string(path)
def test_read_file_to_string_returns_file_contents_as_string(self): _, path = tempfile.mkstemp() content = util.base36_str() with open(path, "w") as f: f.write(content) returned_content = util.read_file_to_string(path) self.assertEqual(content, returned_content)
def test_call_and_write_stdout_to_file_writes_stdout_to_file(self): args = ["python3", tst_helpers.fixture("print_hello_world.py")] _, tmp_output = tempfile.mkstemp() subprocess.call_and_write_stdout_to_file(args, tmp_output) file_content = util.read_file_to_string(tmp_output) self.assertEqual("Hello, world!\n", file_content)
def test_create_file_creates_file_with_content(self): source_roots = [tst_helpers.fixture("templates/")] destination_root = tempfile.mkdtemp() generator = ConfigurableSourceDestGenerator(source_roots, destination_root) content = util.base36_str() filename = util.base36_str() generator.create_file(content, filename) output_path = os.path.join(destination_root, filename) self.assertTrue(os.path.exists(output_path)) written_content = util.read_file_to_string(output_path) self.assertEqual(content, written_content)
def test_render_template_writes_rendered_template_to_destination_root(self): source_roots = [tst_helpers.fixture("templates/")] destination_root = tempfile.mkdtemp() generator = ConfigurableSourceDestGenerator(source_roots, destination_root) output_filename = util.base36_str() foo_val = util.base36_str() generator.render_template("example.txt.jinja2", output_filename, {"foo": foo_val}) expected_output_path = os.path.join(destination_root, output_filename) self.assertTrue(os.path.exists(expected_output_path)) file_content = util.read_file_to_string(expected_output_path) self.assertTrue("foo\n", file_content)
def test_create_file_creates_subdirs_when_required(self): source_roots = [tempfile.mkdtemp()] destination_root = tempfile.mkdtemp() generator = ConfigurableSourceDestGenerator(source_roots, destination_root) provided_content = util.base36_str() output_relpath = os.path.join(util.base36_str(), util.base36_str()) generator.create_file(provided_content, output_relpath) expected_output_path = os.path.join(destination_root, output_relpath) self.assertTrue(os.path.exists(expected_output_path)) written_content = util.read_file_to_string(expected_output_path) self.assertTrue(provided_content, written_content)
def test_copy_file_copies_file_to_destination(self): source_root = tempfile.mkdtemp() file_at_source = util.base36_str() content_in_source_file = util.base36_str() with open(os.path.join(source_root, file_at_source), "w") as f: f.write(content_in_source_file) destination_root = tempfile.mkdtemp() generator = ConfigurableSourceDestGenerator([source_root], destination_root) destination_path = util.base36_str() generator.copy_file(file_at_source, destination_path) expected_output_file = os.path.join(destination_root, destination_path) self.assertTrue(os.path.exists(expected_output_file)) content_in_destination_file = util.read_file_to_string(expected_output_file) self.assertEqual(content_in_source_file, content_in_destination_file)
def test_read_file_to_string_raises_FileNotFoundError_for_non_existent_path( self): with self.assertRaises(FileNotFoundError): util.read_file_to_string(util.base36_str(64))