1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-26 06:01:08 +01:00

starlark/module: os add temp_dir function

This commit is contained in:
Máximo Cuadros 2020-03-27 20:29:03 +01:00
parent af30b3e5d1
commit 452e5032c7
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
2 changed files with 22 additions and 10 deletions

@ -25,6 +25,7 @@ const (
RemoveFuncName = "remove" RemoveFuncName = "remove"
RemoveAllFuncName = "remove_all" RemoveAllFuncName = "remove_all"
RenameFuncName = "rename" RenameFuncName = "rename"
TempDirFuncName = "temp_dir"
) )
var ( var (
@ -55,6 +56,7 @@ func LoadModule() (starlark.StringDict, error) {
RemoveFuncName: starlark.NewBuiltin(MkdirFuncName, Remove), RemoveFuncName: starlark.NewBuiltin(MkdirFuncName, Remove),
RemoveAllFuncName: starlark.NewBuiltin(MkdirFuncName, RemoveAll), RemoveAllFuncName: starlark.NewBuiltin(MkdirFuncName, RemoveAll),
RenameFuncName: starlark.NewBuiltin(RenameFuncName, Rename), RenameFuncName: starlark.NewBuiltin(RenameFuncName, Rename),
TempDirFuncName: starlark.NewBuiltin(TempDirFuncName, TempDir),
}, },
}, },
} }
@ -323,3 +325,13 @@ func Rename(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, k
return starlark.None, os.Rename(oldpath, newpath) return starlark.None, os.Rename(oldpath, newpath)
} }
// TempDir returns the default directory to use for temporary files.
//
// outline: os
// functions:
// temp_dir()
// returns the default directory to use for temporary files.
func TempDir(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return starlark.String(os.TempDir()), nil
}

@ -1,6 +1,8 @@
load('os', 'os') load('os', 'os')
load('assert.star', 'assert') load('assert.star', 'assert')
assert.ne(os.temp_dir(), "")
key = "HELLO" key = "HELLO"
value = "world" value = "world"
@ -9,21 +11,19 @@ assert.eq(os.getenv(key), value)
content = "hello world\n" content = "hello world\n"
assert.eq(os.write_file("/tmp/plain.txt", content), None) temp = os.temp_dir()
assert.eq(os.read_file("/tmp/plain.txt"), content) os.chdir(temp)
assert.eq(os.getwd(), temp)
assert.eq(os.write_file("/tmp/perms.txt", content=content, perms=0o777), None) assert.eq(os.write_file("plain.txt", content), None)
assert.eq(os.read_file("/tmp/perms.txt"), content) assert.eq(os.read_file("plain.txt"), content)
assert.eq(os.write_file("perms.txt", content=content, perms=0o777), None)
assert.eq(os.read_file("perms.txt"), content)
os.mkdir("foo", 0o755) os.mkdir("foo", 0o755)
os.remove("foo") os.remove("foo")
assert.ne(os.getwd(), "")
home = os.getenv("HOME")
os.chdir(home)
assert.eq(os.getwd(), home)
os.mkdir_all("foo/bar", 0o755) os.mkdir_all("foo/bar", 0o755)
os.rename("foo", "bar") os.rename("foo", "bar")