def test_generate_empty_file(self): with infra_libs.temporary_directory(prefix="new-tool-test-") as tempdir: filename = new_tool.generate_python_file(tempdir, "test.py", None) self.assertTrue(os.path.isfile(filename)) # Read the content and do some basic check with open(filename, "r") as f: content = f.read() self.assertTrue(content.startswith(new_tool.COPYRIGHT_NOTICE)) # Now make sure the file is not overwritten new_content = "other content" with open(filename, "w") as f: f.write(new_content) filename2 = new_tool.generate_python_file(tempdir, "test.py", None) self.assertEqual(filename, filename2) with open(filename, "r") as f: content = f.read() self.assertEqual(content, new_content)
def test_generate_file_from_template(self): with infra_libs.temporary_directory(prefix="new-tool-test-") as tempdir: filename = new_tool.generate_python_file( tempdir, "test.py", "test_template", template_dir=DATA_DIR, value="Passed string." ) self.assertTrue(os.path.isfile(filename)) # Read the content with open(filename, "r") as f: content = f.read() expected_content = ( new_tool.COPYRIGHT_NOTICE + "This is a template used by the test suite.\n" "Passed string.\n" ) self.assertEqual(content, expected_content)