github.com/mcuadros/ascode@v1.3.1/starlark/module/os/testdata/test.star (about) 1 load('os', 'os') 2 load('assert.star', 'assert') 3 4 assert.ne(os.temp_dir(), "") 5 6 key = "HELLO" 7 value = "world" 8 9 os.setenv(key, value) 10 assert.eq(os.getenv(key), value) 11 12 content = "hello world\n" 13 14 home = os.getenv("HOME") 15 os.chdir(home) 16 assert.eq(os.getwd(), home) 17 18 temp = os.temp_dir() 19 os.chdir(temp) 20 21 assert.eq(os.write_file("plain.txt", content), None) 22 assert.eq(os.read_file("plain.txt"), content) 23 24 assert.eq(os.write_file("perms.txt", content=content, perms=0o777), None) 25 assert.eq(os.read_file("perms.txt"), content) 26 27 os.mkdir("foo", 0o755) 28 os.remove("foo") 29 30 os.mkdir_all("foo/bar", 0o755) 31 32 os.rename("foo", "bar") 33 os.remove_all("bar") 34 35 def deleteNotExistant(): os.remove("foo") 36 assert.fails(deleteNotExistant, "remove foo: no such file or directory") 37 38 # test command 39 temp = os.temp_dir() + "/example-dir" 40 os.mkdir_all(temp + "/foo/bar", 0o755) 41 os.chdir(temp) 42 43 assert.eq(os.command("ls -1"), "foo") 44 45 # test command dir 46 assert.eq(os.command("ls -1", dir="foo"), "bar") 47 48 # test command shell and env 49 assert.eq(os.command("echo $FOO", shell=True, env=["FOO=foo"]), "foo") 50 51 # test command combined 52 assert.ne(os.command("not-exists || true", shell=True, combined=True), "") 53 54 # test command error 55 assert.fails(lambda: os.command("not-exists"), "executable file not found") 56 57 os.remove_all(temp)