2019-03-08 09:54:42 +00:00
|
|
|
import os
|
|
|
|
import tarfile
|
2019-03-08 11:06:57 +00:00
|
|
|
import pytest
|
2019-03-08 09:54:42 +00:00
|
|
|
|
|
|
|
from bunker.files.tarfile import RewriteableTarFile
|
2019-03-08 11:09:01 +00:00
|
|
|
from bunker.files.bunkeredfile import BunkeredFile
|
2019-03-08 09:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
2019-03-08 11:06:57 +00:00
|
|
|
|
|
|
|
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__")
|
|
|
|
|