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="labm8/data/test/bin")
def test_scp_bad_dst(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self._test(["Hello, world!"], fs.read("/tmp/labm8.tmp")) # Error is raised if destination file cannot be written. with self.assertRaises(system.ScpError): system.scp("localhost", "/tmp/labm8.tmp", "/not/a/valid/path", path="tests/bin")
def test_scp_bad_dst_permission(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self._test(["Hello, world!"], fs.read("/tmp/labm8.tmp")) # Error is raised if no write permission for destination. with self.assertRaises(system.ScpError): system.scp("localhost", "/tmp/labm8.tmp", "/dev", path="tests/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="labm8/data/test/bin")
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_cp(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self._test(["Hello, world!"], fs.read("/tmp/labm8.tmp")) # Cleanup any existing file. fs.rm("/tmp/labm8.tmp.copy") self._test(False, fs.exists("/tmp/labm8.tmp.copy")) fs.cp("/tmp/labm8.tmp", "/tmp/labm8.tmp.copy") self._test(fs.read("/tmp/labm8.tmp"), fs.read("/tmp/labm8.tmp.copy"))
def test_sed(self): system.echo("Hello, world!", "/tmp/labm8.tmp") system.sed("Hello", "Goodbye", "/tmp/labm8.tmp") self._test(["Goodbye, world!"], fs.read("/tmp/labm8.tmp")) system.sed("o", "_", "/tmp/labm8.tmp") self._test(["G_odbye, world!"], fs.read("/tmp/labm8.tmp")) system.sed("o", "_", "/tmp/labm8.tmp", "g") self._test(["G__dbye, w_rld!"], fs.read("/tmp/labm8.tmp"))
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_must_exist(self): system.echo("Hello, world!", "/tmp/labm8.must_exist.txt") self.assertEqual(fs.must_exist("/tmp/labm8.must_exist.txt"), "/tmp/labm8.must_exist.txt") self.assertEqual(fs.must_exist("/tmp", "labm8.must_exist.txt"), "/tmp/labm8.must_exist.txt") with self.assertRaises(fs.File404): fs.must_exist("/not/a/real/path") fs.rm("/tmp/labm8.must_exist.txt")
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_user(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self._test(["Hello, world!"], fs.read("/tmp/labm8.tmp")) # Cleanup any existing file. fs.rm("/tmp/labm8.tmp.copy") self._test(False, fs.exists("/tmp/labm8.tmp.copy")) # Perform scp. system.scp("localhost", "/tmp/labm8.tmp", "/tmp/labm8.tmp.copy", path="tests/bin", user="******") self._test(fs.read("/tmp/labm8.tmp"), fs.read("/tmp/labm8.tmp.copy"))
def test_scp_user(): 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="labm8/data/test/bin", user="******") 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_read_file(self): 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") self.assertEqual(a["a"], 1) self.assertEqual(a["b"], [1, 2, 3]) self.assertFalse("c" in a)
def test_rm(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self._test(True, fs.isfile("/tmp/labm8.tmp")) fs.rm("/tmp/labm8.tmp") self._test(False, 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") self._test(True, fs.isfile("/tmp/labm8.dir/foo/bar/baz")) fs.rm("/tmp/labm8.dir") self._test(False, fs.isfile("/tmp/labm8.dir/foo/bar/baz")) self._test(False, fs.isfile("/tmp/labm8.dir/"))
def test_rmtrash(self): system.echo("Hello, world!", "/tmp/labm8.tmp") self.assertTrue(fs.isfile("/tmp/labm8.tmp")) fs.rmtrash("/tmp/labm8.tmp") self.assertFalse(fs.isfile("/tmp/labm8.tmp")) fs.rmtrash("/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") self.assertTrue(fs.isfile("/tmp/labm8.dir/foo/bar/baz")) fs.rmtrash("/tmp/labm8.dir") self.assertFalse(fs.isfile("/tmp/labm8.dir/foo/bar/baz")) self.assertFalse(fs.isfile("/tmp/labm8.dir/"))
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_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_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_cp_over_dir(self): 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") self._test(True, fs.isdir("/tmp/labm8.tmp.src")) self._test(True, fs.isfile("/tmp/labm8.tmp.src/foo")) self._test(True, fs.isdir("/tmp/labm8.tmp.copy")) self._test(False, fs.isfile("/tmp/labm8.tmp.copy/foo")) fs.cp("/tmp/labm8.tmp.src", "/tmp/labm8.tmp.copy/") self._test(True, fs.isdir("/tmp/labm8.tmp.src")) self._test(True, fs.isfile("/tmp/labm8.tmp.src/foo")) self._test(True, fs.isdir("/tmp/labm8.tmp.copy")) self._test(True, fs.isfile("/tmp/labm8.tmp.copy/foo")) self._test(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_dict_key(self): c = cache.FSCache("/tmp/labm8-cache-dict") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check self.assertEqual(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 self.assertTrue(fs.read(c[key]), ["Hello, world!"]) 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 run_example_prog(prog, args): """ Run a SkelCL example program. Arguments: prog (str): The name of the program to run args (list of str): Any arguments """ fs.cd(fs.path(experiment.EXAMPLES_BUILD, prog)) cmd = ["./" + prog] + args cmd_str = " ".join(cmd) io.info("COMMAND:", io.colourise(io.Colours.RED, cmd_str)) ret, _, _ = system.run(cmd, stdout=system.STDOUT, stderr=system.STDERR) if ret: system.echo(cmd_str, "/tmp/naughty.txt", append=True) return ret
def test_iter_len(self): 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: self.assertEqual(path, c.keypath("foo")) system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["bar"] = "/tmp/labm8.testfile.txt" self.assertEqual(len(c), 2) self.assertEqual(len(c.ls()), 2) self.assertTrue("bar" in c.ls()) self.assertTrue("foo" in c.ls()) c.clear()
def test_set_and_get(self): 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 self.assertEqual(fs.read("/tmp/labm8.testfile.txt"), ["Hello, world!"]) # insert file into cache c['foobar'] = "/tmp/labm8.testfile.txt" # file must be in cache self.assertTrue(fs.isfile(c.keypath("foobar"))) # file must have been moved self.assertFalse(fs.isfile("/tmp/labm8.testfile.txt")) # check file contents self.assertTrue(fs.read(c['foobar']), ["Hello, world!"]) self.assertEqual(fs.read(c['foobar']), fs.read(c.get('foobar'))) c.clear()
def test_remove(self): c = cache.FSCache("/tmp/labm8-cache-remove") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check self.assertEqual(fs.read("/tmp/labm8.test.remove.txt"), ["Hello, world!"]) # insert file into cache c['foobar'] = "/tmp/labm8.test.remove.txt" # sanity check self.assertEqual(fs.read(c['foobar']), ["Hello, world!"]) # remove from cache del c['foobar'] with self.assertRaises(KeyError): a = c['foobar'] self.assertEqual(c.get("foobar"), None) 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_rm_glob(self): 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) self._test(True, fs.isfile("/tmp/labm8.glob/1")) self._test(True, fs.isfile("/tmp/labm8.glob/2")) self._test(True, fs.isfile("/tmp/labm8.glob/abc")) fs.rm("/tmp/labm8.glob/a*") self._test(True, fs.isfile("/tmp/labm8.glob/1")) self._test(True, fs.isfile("/tmp/labm8.glob/2")) self._test(False, fs.isfile("/tmp/labm8.glob/abc")) fs.rm("/tmp/labm8.glob/*") self._test(False, fs.isfile("/tmp/labm8.glob/1")) self._test(False, fs.isfile("/tmp/labm8.glob/2")) self._test(False, fs.isfile("/tmp/labm8.glob/abc"))
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(self): system.echo("foo", "/tmp/labm8.tmp", end="_") self._test(["foo_"], fs.read("/tmp/labm8.tmp"))
def test_echo_append(self): system.echo("foo", "/tmp/labm8.tmp") system.echo("bar", "/tmp/labm8.tmp", append=True) self._test(["foo", "bar"], fs.read("/tmp/labm8.tmp"))
def test_echo(self): system.echo("foo", "/tmp/labm8.tmp") self._test(["foo"], fs.read("/tmp/labm8.tmp")) system.echo("", "/tmp/labm8.tmp") self._test([""], fs.read("/tmp/labm8.tmp"))
def test_echo_kwargs(): system.echo("foo", "/tmp/labm8.tmp", end="_") assert fs.read("/tmp/labm8.tmp") == ["foo_"]
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_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_mv_no_dst(self): system.echo("Hello, world!", "/tmp/labm8.tmp") with self.assertRaises(IOError): fs.mv("/tmp/labm8.tmp", "/not/a/real/path") fs.rm("/tmp/labm8.tmp")