def test_cp(): system.echo("Hello, world!", "/tmp/labm8.tmp") assert ["Hello, world!"] == fs.read("/tmp/labm8.tmp") # Cleanup any existing file. fs.rm("/tmp/labm8.tmp.copy") assert not fs.exists("/tmp/labm8.tmp.copy") fs.cp("/tmp/labm8.tmp", "/tmp/labm8.tmp.copy") assert fs.read("/tmp/labm8.tmp") == fs.read("/tmp/labm8.tmp.copy")
def test_sed(): system.echo("Hello, world!", "/tmp/labm8.tmp") system.sed("Hello", "Goodbye", "/tmp/labm8.tmp") assert ["Goodbye, world!"] == fs.read("/tmp/labm8.tmp") system.sed("o", "_", "/tmp/labm8.tmp") assert ["G_odbye, world!"] == fs.read("/tmp/labm8.tmp") system.sed("o", "_", "/tmp/labm8.tmp", "g") assert ["G__dbye, w_rld!"] == fs.read("/tmp/labm8.tmp")
def test_scp_bad_dst(): system.echo("Hello, world!", "/tmp/labm8.tmp") assert ["Hello, world!"] == fs.read("/tmp/labm8.tmp") # Error is raised if destination file cannot be written. with pytest.raises(system.ScpError): system.scp("localhost", "/tmp/labm8.tmp", "/not/a/valid/path", path="lib/labm8/data/test/bin")
def test_scp_bad_dst_permission(): system.echo("Hello, world!", "/tmp/labm8.tmp") assert ["Hello, world!"] == fs.read("/tmp/labm8.tmp") # Error is raised if no write permission for destination. with pytest.raises(system.ScpError): system.scp("localhost", "/tmp/labm8.tmp", "/dev", path="lib/labm8/data/test/bin")
def test_read_file(): a_str = """{ "a": 1, // this has comments "b": [1, 2, 3] } # end comment // begin with comment """ system.echo(a_str, "/tmp/labm8.loaf.json") a = jsonutil.read_file("/tmp/labm8.loaf.json") assert a == {'a': 1, 'b': [1, 2, 3]}
def test_scp(): system.echo("Hello, world!", "/tmp/labm8.tmp") assert ["Hello, world!"] == fs.read("/tmp/labm8.tmp") # Cleanup any existing file. fs.rm("/tmp/labm8.tmp.copy") assert not fs.exists("/tmp/labm8.tmp.copy") # Perform scp. system.scp("localhost", "/tmp/labm8.tmp", "/tmp/labm8.tmp.copy", path="lib/labm8/data/test/bin") assert fs.read("/tmp/labm8.tmp") == fs.read("/tmp/labm8.tmp.copy")
def test_FSCache_dict_key(): c = cache.FSCache("/tmp/labm8-cache-dict") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache key = {'a': 5, "c": [1, 2, 3]} c[key] = "/tmp/labm8.test.remove.txt" # check file contents assert fs.read(c[key]) == ["Hello, world!"] c.clear()
def test_FSCache_iter_len(): c = cache.FSCache("/tmp/labm8-fscache-iter", escape_key=cache.escape_path) c.clear() system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["foo"] = "/tmp/labm8.testfile.txt" for path in c: assert path == c.keypath("foo") system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["bar"] = "/tmp/labm8.testfile.txt" assert len(c) == 2 assert len(c.ls()) == 2 assert "bar" in c.ls() assert "foo" in c.ls() c.clear()
def test_rm(): system.echo("Hello, world!", "/tmp/labm8.tmp") assert fs.isfile("/tmp/labm8.tmp") fs.rm("/tmp/labm8.tmp") assert not fs.isfile("/tmp/labm8.tmp") fs.rm("/tmp/labm8.tmp") fs.rm("/tmp/labm8.tmp") fs.rm("/tmp/labm8.dir") fs.mkdir("/tmp/labm8.dir/foo/bar") system.echo("Hello, world!", "/tmp/labm8.dir/foo/bar/baz") assert fs.isfile("/tmp/labm8.dir/foo/bar/baz") fs.rm("/tmp/labm8.dir") assert not fs.isfile("/tmp/labm8.dir/foo/bar/baz") assert not fs.isfile("/tmp/labm8.dir/")
def test_rmtrash(): with tempfile.NamedTemporaryFile(prefix='labm8_') as f: assert fs.isfile(f.name) fs.rmtrash(f.name) assert not fs.isfile(f.name) fs.rmtrash(f.name) fs.rm(f.name) with tempfile.TemporaryDirectory() as d: fs.rm(d) fs.mkdir(d, "foo/bar") system.echo("Hello, world!", fs.path(d, "foo/bar/baz")) assert fs.isfile(f, "foo/bar/baz") fs.rmtrash(d) assert not fs.isfile(d, "foo/bar/baz") assert not fs.isdir(d)
def test_cp_over_dir(): fs.mkdir("/tmp/labm8.tmp.src") system.echo("Hello, world!", "/tmp/labm8.tmp.src/foo") fs.rm("/tmp/labm8.tmp.copy") fs.mkdir("/tmp/labm8.tmp.copy") assert fs.isdir("/tmp/labm8.tmp.src") assert fs.isfile("/tmp/labm8.tmp.src/foo") assert fs.isdir("/tmp/labm8.tmp.copy") assert not fs.isfile("/tmp/labm8.tmp.copy/foo") fs.cp("/tmp/labm8.tmp.src", "/tmp/labm8.tmp.copy/") assert fs.isdir("/tmp/labm8.tmp.src") assert fs.isfile("/tmp/labm8.tmp.src/foo") assert fs.isdir("/tmp/labm8.tmp.copy") assert fs.isfile("/tmp/labm8.tmp.copy/foo") assert (fs.read("/tmp/labm8.tmp.src/foo") == fs.read( "/tmp/labm8.tmp.copy/foo"))
def test_FSCache_remove(): c = cache.FSCache("/tmp/labm8-cache-remove") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.test.remove.txt" # sanity check assert fs.read(c['foobar']) == ["Hello, world!"] # remove from cache del c['foobar'] with pytest.raises(KeyError): c['foobar'] assert not c.get("foobar") c.clear()
def test_set_and_get(): fs.rm("/tmp/labm8-cache-set-and-get") c = cache.FSCache("/tmp/labm8-cache-set-and-get") # create file system.echo("Hello, world!", "/tmp/labm8.testfile.txt") # sanity check assert fs.read("/tmp/labm8.testfile.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.testfile.txt" # file must be in cache assert fs.isfile(c.keypath("foobar")) # file must have been moved assert not fs.isfile("/tmp/labm8.testfile.txt") # check file contents assert fs.read(c['foobar']) == ["Hello, world!"] assert fs.read(c['foobar']) == fs.read(c.get('foobar')) c.clear()
def test_rm_glob(): fs.mkdir("/tmp/labm8.glob") system.echo("Hello, world!", "/tmp/labm8.glob/1") system.echo("Hello, world!", "/tmp/labm8.glob/2") system.echo("Hello, world!", "/tmp/labm8.glob/abc") fs.rm("/tmp/labm8.glob/a*", glob=False) assert fs.isfile("/tmp/labm8.glob/1") assert fs.isfile("/tmp/labm8.glob/2") assert fs.isfile("/tmp/labm8.glob/abc") fs.rm("/tmp/labm8.glob/a*") assert fs.isfile("/tmp/labm8.glob/1") assert fs.isfile("/tmp/labm8.glob/2") assert not fs.isfile("/tmp/labm8.glob/abc") fs.rm("/tmp/labm8.glob/*") assert not fs.isfile("/tmp/labm8.glob/1") assert not fs.isfile("/tmp/labm8.glob/2") assert not fs.isfile("/tmp/labm8.glob/abc")
def test_echo_append(): system.echo("foo", "/tmp/labm8.tmp") system.echo("bar", "/tmp/labm8.tmp", append=True) assert fs.read("/tmp/labm8.tmp") == ["foo", "bar"]
def test_echo(): system.echo("foo", "/tmp/labm8.tmp") assert fs.read("/tmp/labm8.tmp") == ["foo"] system.echo("", "/tmp/labm8.tmp") assert fs.read("/tmp/labm8.tmp") == [""]
def test_mv_no_dst(): system.echo("Hello, world!", "/tmp/labm8.tmp") with pytest.raises(IOError): fs.mv("/tmp/labm8.tmp", "/not/a/real/path") fs.rm("/tmp/labm8.tmp")
def test_echo_kwargs(): system.echo("foo", "/tmp/labm8.tmp", end="_") assert fs.read("/tmp/labm8.tmp") == ["foo_"]