Skip to content

Commit 0e84148

Browse files
committed
Also add TempDir, which can be useful during testing.
1 parent 9e44781 commit 0e84148

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

tests/testdata/tests.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local goos = require 'goos'
2+
3+
function TestTempDir(t)
4+
-- Ensures that a tempdir created in subtests doesn't exist any longer after the test is run
5+
local tempDir = ''
6+
7+
t:Run('createTmpDir', function(t)
8+
tempDir = t:TempDir()
9+
stat = goos.stat(tempDir)
10+
assert(stat)
11+
assert(stat.is_dir)
12+
end)
13+
assert(tempDir ~= '', tempDir)
14+
assert(not goos.stat(tempDir))
15+
end

tests/testutil.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,21 @@ func tSkipf(L *lua.LState) int {
9494
return 0
9595
}
9696

97+
func tTempDir(L *lua.LState) int {
98+
t := checkT(L, 1)
99+
L.Push(lua.LString(t.TempDir()))
100+
return 1
101+
}
102+
97103
func registerTType(L *lua.LState) {
98104
mt := L.NewTypeMetatable(TType)
99105
index := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
100-
"Run": tRun,
101-
"Log": tLog,
102-
"Logf": tLogf,
103-
"Skip": tSkip,
104-
"Skipf": tSkipf,
106+
"Run": tRun,
107+
"Log": tLog,
108+
"Logf": tLogf,
109+
"Skip": tSkip,
110+
"Skipf": tSkipf,
111+
"TempDir": tTempDir,
105112
})
106113
L.SetField(mt, "__index", index)
107114
L.SetGlobal(TType, mt)

tests/testutil_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package tests
22

33
import (
44
"github.com/stretchr/testify/assert"
5+
"github.com/vadv/gopher-lua-libs/goos"
56
"github.com/vadv/gopher-lua-libs/strings"
67
"testing"
78
)
89

910
func TestSuite(t *testing.T) {
10-
preload := SeveralPreloadFuncs(
11-
strings.Preload,
12-
)
11+
preload := strings.Preload
1312
assert.NotZero(t, RunLuaTestFile(t, preload, "testdata/suite_test.lua"))
1413
}
14+
15+
func TestApi(t *testing.T) {
16+
preload := goos.Preload
17+
assert.NotZero(t, RunLuaTestFile(t, preload, "testdata/tests.lua"))
18+
}

0 commit comments

Comments
 (0)