def test_file_avoid_write(self): with MockedOpen({'file': 'content'}): # Overwriting an existing file replaces its content with FileAvoidWrite('file') as file: file.write('bazqux') self.assertEqual(open('file', 'r').read(), 'bazqux') # Creating a new file (obviously) stores its content with FileAvoidWrite('file2') as file: file.write('content') self.assertEqual(open('file2').read(), 'content') class MyMockedOpen(MockedOpen): '''MockedOpen extension to raise an exception if something attempts to write in an opened file. ''' def __call__(self, name, mode): if 'w' in mode: raise Exception, 'Unexpected open with write mode' return MockedOpen.__call__(self, name, mode) with MyMockedOpen({'file': 'content'}): # Validate that MyMockedOpen works as intended file = FileAvoidWrite('file') file.write('foobar') self.assertRaises(Exception, file.close) # Check that no write actually happens when writing the # same content as what already is in the file with FileAvoidWrite('file') as file: file.write('content')
def test_diff_not_default(self): """Diffs are not produced by default.""" faw = FileAvoidWrite('doesnotexist') faw.write('dummy') faw.close() self.assertIsNone(faw.diff)
def test_diff_not_default(self): """Diffs are not produced by default.""" with MockedOpen({"file": "old"}): faw = FileAvoidWrite("file") faw.write("dummy") faw.close() self.assertIsNone(faw.diff)
def test_write_unicode(tmp_path): # Unicode grinning face :D binary_emoji = b"\xf0\x9f\x98\x80" file = tmp_path / "file.dat" faw = FileAvoidWrite(str(file)) faw.write(binary_emoji) faw.close()
def test_diff_not_default(self): """Diffs are not produced by default.""" with MockedOpen({'file': 'old'}): faw = FileAvoidWrite('file') faw.write('dummy') faw.close() self.assertIsNone(faw.diff)
def test_store_new_contents(tmp_path): file = tmp_path / "file.txt" faw = FileAvoidWrite(str(file)) faw.write("content") assert faw.close() == (False, True) assert file.read_text() == "content"
def test_change_binary_file_contents(tmp_path): file = tmp_path / "file.dat" file.write_bytes(b"\0") faw = FileAvoidWrite(str(file), readmode="rb") faw.write(b"\0\0\0") assert faw.close() == (True, True) assert file.read_bytes() == b"\0\0\0"
def test_overwrite_contents(tmp_path): file = tmp_path / "file.txt" file.write_text("abc") faw = FileAvoidWrite(str(file)) faw.write("bazqux") assert faw.close() == (True, True) assert file.read_text() == "bazqux"
def test_no_write_happens_if_file_contents_same(tmp_path): file = tmp_path / "file.txt" file.write_text("content") original_write_time = file.stat().st_mtime faw = FileAvoidWrite(str(file)) faw.write("content") assert faw.close() == (True, False) assert file.stat().st_mtime == original_write_time
def test_diff_update(tmp_path): file = tmp_path / "diffable.txt" file.write_text("old") faw = FileAvoidWrite(str(file), capture_diff=True) faw.write("new") faw.close() diff = "\n".join(faw.diff) assert "-old" in diff assert "+new" in diff
def _maybe_write_file(self, path, content, result): fh = FileAvoidWrite(path) fh.write(content) existed, updated = fh.close() if not existed: result[0].add(path) elif updated: result[1].add(path) else: result[2].add(path)
def test_diff_update(self): """Diffs are produced on file update.""" with MockedOpen({"file": "old"}): faw = FileAvoidWrite("file", capture_diff=True) faw.write("new") faw.close() diff = "\n".join(faw.diff) self.assertIn("-old", diff) self.assertIn("+new", diff)
def test_diff_update(self): """Diffs are produced on file update.""" with MockedOpen({'file': 'old'}): faw = FileAvoidWrite('file', capture_diff=True) faw.write('new') faw.close() diff = '\n'.join(faw.diff) self.assertIn('-old', diff) self.assertIn('+new', diff)
def test_diff_create(self): """Diffs are produced when files are created.""" tmpdir = tempfile.mkdtemp() try: path = os.path.join(tmpdir, 'file') faw = FileAvoidWrite(path, capture_diff=True) faw.write('new') faw.close() diff = '\n'.join(faw.diff) self.assertIn('+new', diff) finally: shutil.rmtree(tmpdir)
def install_from_file(self, filename, distdir): self.log(logging.INFO, 'artifact', {'filename': filename}, 'Installing from {filename}') # Copy all .so files to dist/bin, avoiding modification where possible. ensureParentDir(os.path.join(distdir, 'bin', '.dummy')) with zipfile.ZipFile(filename) as zf: for info in zf.infolist(): if not info.filename.endswith('.so'): continue n = os.path.join(distdir, 'bin', os.path.basename(info.filename)) fh = FileAvoidWrite(n, mode='r') shutil.copyfileobj(zf.open(info), fh) fh.write(zf.open(info).read()) file_existed, file_updated = fh.close() self.log(logging.INFO, 'artifact', {'updating': 'Updating' if file_updated else 'Not updating', 'filename': n}, '{updating} {filename}') return 0
def install_from_file(self, filename, distdir): self.log(logging.INFO, 'artifact', {'filename': filename}, 'Installing from {filename}') # Copy all .so files to dist/bin, avoiding modification where possible. ensureParentDir(os.path.join(distdir, 'bin', '.dummy')) with zipfile.ZipFile(filename) as zf: for info in zf.infolist(): if not info.filename.endswith('.so'): continue n = os.path.join(distdir, 'bin', os.path.basename(info.filename)) fh = FileAvoidWrite(n, mode='r') shutil.copyfileobj(zf.open(info), fh) fh.write(zf.open(info).read()) file_existed, file_updated = fh.close() self.log( logging.INFO, 'artifact', { 'updating': 'Updating' if file_updated else 'Not updating', 'filename': n }, '{updating} {filename}') return 0
def test_file_avoid_write(self): with MockedOpen({"file": "content"}): # Overwriting an existing file replaces its content faw = FileAvoidWrite("file") faw.write("bazqux") self.assertEqual(faw.close(), (True, True)) self.assertEqual(open("file", "r").read(), "bazqux") # Creating a new file (obviously) stores its content faw = FileAvoidWrite("file2") faw.write("content") self.assertEqual(faw.close(), (False, True)) self.assertEqual(open("file2").read(), "content") with MockedOpen({"file": "content"}): with FileAvoidWrite("file") as file: file.write("foobar") self.assertEqual(open("file", "r").read(), "foobar") class MyMockedOpen(MockedOpen): """MockedOpen extension to raise an exception if something attempts to write in an opened file. """ def __call__(self, name, mode): if "w" in mode: raise Exception, "Unexpected open with write mode" return MockedOpen.__call__(self, name, mode) with MyMockedOpen({"file": "content"}): # Validate that MyMockedOpen works as intended file = FileAvoidWrite("file") file.write("foobar") self.assertRaises(Exception, file.close) # Check that no write actually happens when writing the # same content as what already is in the file faw = FileAvoidWrite("file") faw.write("content") self.assertEqual(faw.close(), (True, False))
def test_diff_not_created_by_default(tmp_path): file = tmp_path / "file.txt" faw = FileAvoidWrite(str(file)) faw.write("dummy") faw.close() assert faw.diff is None