import os import tarfile import pytest from bunker.files.tarfile import RewriteableTarFile from bunker.files.bunkeredfile import BunkeredFile def test_create(tmpdir): tmpdname = str(tmpdir) tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = BunkeredFile.empty("__bunker_main__") tf.add_file(f) f = tf.get_file("__bunker_main__") f.write(b"foobar") tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = tf.get_file("__bunker_main__") assert f.read() == b"foobar" def test_rewrite(tmpdir): tmpdname = str(tmpdir) tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = BunkeredFile.empty("__bunker_main__") tf.add_file(f) tf.add_file(BunkeredFile.empty("test.tx")) f = tf.get_file("__bunker_main__") f.write(b"foobar") tf.close() assert f.closed == True tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = tf.get_file("__bunker_main__") f.write(b"foobaz") tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = tf.get_file("__bunker_main__") assert f.read() == b"foobaz" def test_writeback(tmpdir): tmpdname = str(tmpdir) tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = BunkeredFile.empty("__bunker_main__") tf.add_file(f) tf.add_file(BunkeredFile.empty("test.tx")) f = tf.get_file("__bunker_main__") f.write(b"foobar") tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = tf.get_file("__bunker_main__") f.write(b"foobaz") f.close() tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = tf.get_file("__bunker_main__") assert f.read() == b"foobaz" def test_delete(tmpdir): tmpdname = str(tmpdir) tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) f = BunkeredFile.empty("__bunker_main__") tf.add_file(f) tf.add_file(BunkeredFile.empty("test.tx")) f = tf.get_file("__bunker_main__") f.write(b"foobar") tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) tf.delete_file("__bunker_main__") tf.close() tf = RewriteableTarFile.open(os.path.join(tmpdname, "test.bunker")) with pytest.raises(KeyError): f = tf.get_file("__bunker_main__")