Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 408b8f0

Browse files
committed
Integrate Lua test results into tasty output
1 parent 2dd2f8b commit 408b8f0

5 files changed

Lines changed: 210 additions & 135 deletions

File tree

hslua-module-system.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ copyright: Albert Krewinkel <albert+hslua@zeitkraut.de>
1818
category: Foreign
1919
build-type: Simple
2020
extra-source-files: CHANGELOG.md
21-
test/system-module-tests.lua
21+
, test/test-system.lua
2222
cabal-version: >=1.10
2323
tested-with: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.5
2424

@@ -51,4 +51,5 @@ test-suite test-hslua-module-system
5151
, hslua-module-system
5252
, tasty
5353
, tasty-hunit
54+
, tasty-lua >= 0.2 && < 0.3
5455
, text

stack.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ packages:
33
- .
44
extra-deps:
55
- hslua-1.0.3
6+
- tasty-lua-0.2.0

test/system-module-tests.lua

Lines changed: 0 additions & 124 deletions
This file was deleted.

test/test-hslua-module-system.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ Portability : Requires language extensions ForeignFunctionInterface,
1111
Tests for the `system` Lua module.
1212
-}
1313

14-
import Control.Monad (void, when)
14+
import Control.Monad (void)
1515
import Foreign.Lua (Lua)
1616
import Foreign.Lua.Module.System (preloadModule, pushModule)
1717
import Test.Tasty (TestTree, defaultMain, testGroup)
1818
import Test.Tasty.HUnit (assertEqual, testCase)
19+
import Test.Tasty.Lua (translateResultsFromFile)
1920

2021
import qualified Foreign.Lua as Lua
2122

2223
main :: IO ()
23-
main = defaultMain $ testGroup "hslua-module-system" [tests]
24+
main = do
25+
luaTestResults <- Lua.run $ do
26+
Lua.openlibs
27+
Lua.requirehs "system" (void pushModule)
28+
translateResultsFromFile "test/test-system.lua"
29+
defaultMain $ testGroup "hslua-module-system" [tests, luaTestResults]
2430

2531
-- | HSpec tests for the Lua 'system' module
2632
tests :: TestTree
@@ -40,14 +46,6 @@ tests = testGroup "HsLua System module"
4046
preloadModule "hssystem"
4147
assertEqual' "loading the module fails " Lua.OK =<<
4248
Lua.dostring "require 'hssystem'"
43-
44-
, testCase "Lua tests pass" . Lua.run $ do
45-
Lua.openlibs
46-
preloadModule "system"
47-
assertEqual' "error while running lua tests" Lua.OK =<< do
48-
st <- Lua.loadfile "test/system-module-tests.lua"
49-
when (st == Lua.OK) $ Lua.call 0 0
50-
return st
5149
]
5250

5351
assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua ()

test/test-system.lua

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
--
2+
-- Tests for the system module
3+
--
4+
local system = require 'system'
5+
local tasty = require 'tasty'
6+
7+
local group = tasty.test_group
8+
local test = tasty.test_case
9+
local assert = tasty.assert
10+
11+
--- helper function, combining with_wd and with_tmpdir
12+
function in_tmpdir (callback)
13+
return function ()
14+
system.with_tmpdir('test-sytem-tmpdir', function (tmpdir)
15+
system.with_wd(tmpdir, callback)
16+
end)
17+
end
18+
end
19+
20+
--- dummy string for testing
21+
local token = 'Banana'
22+
23+
--- check if token can be written into a file in given directory; returns the
24+
--- content of this file.
25+
function write_read_token (dir, filename)
26+
local filename = string.format('%s/%s', dir, 'foo.txt')
27+
local fh = io.open(filename, 'w')
28+
fh:write(token .. '\n')
29+
fh:close()
30+
return io.open(filename):read '*l'
31+
end
32+
33+
34+
-- Check existence static fields
35+
return {
36+
group 'static fields' {
37+
test('arch', function ()
38+
assert.are_equal(type(system.arch), 'string')
39+
end),
40+
test('compiler_name', function ()
41+
assert.are_equal(type(system.compiler_name), 'string')
42+
end),
43+
test('compiler_version', function ()
44+
assert.are_equal(type(system.compiler_version), 'table')
45+
end),
46+
test('os', function ()
47+
assert.are_equal(type(system.os), 'string')
48+
end),
49+
},
50+
51+
group 'environment' {
52+
test('getenv returns same result as os.getenv', function ()
53+
assert.are_equal(system.getenv 'PATH', os.getenv 'PATH')
54+
end),
55+
56+
test('setenv sets environment values', function ()
57+
system.setenv('HSLUA_SYSTEM_MODULE', 'test')
58+
assert.are_equal(os.getenv 'HSLUA_SYSTEM_MODULE', 'test')
59+
end),
60+
61+
},
62+
group 'getwd' {
63+
test('returns a string', function ()
64+
assert.are_equal(type(system.getwd()), 'string')
65+
end)
66+
},
67+
68+
group 'env' {
69+
test('returns a table', function ()
70+
assert.are_equal(type(system.env()), 'table')
71+
end)
72+
},
73+
74+
group 'ls' {
75+
test('returns a table', function ()
76+
assert.are_equal(type(system.ls('.')), 'table')
77+
end),
78+
test('lists files in directory', in_tmpdir(function ()
79+
io.open('README.org', 'w'):close()
80+
assert.are_same(system.ls '.', {'README.org'})
81+
end)),
82+
test('argument defaults to `.`', function ()
83+
assert.are_equal(#system.ls('.'), #system.ls())
84+
end),
85+
test('fails when arg is not a directory', function ()
86+
assert.error_matches(
87+
function () system.ls('thisdoesnotexist') end,
88+
'thisdoesnotexist'
89+
)
90+
assert.error_matches(
91+
function () system.ls('README.md') end,
92+
'README%.md'
93+
)
94+
end)
95+
},
96+
97+
group 'mkdir' {
98+
test('create directory', in_tmpdir(function ()
99+
system.mkdir 'foo'
100+
assert.are_equal((system.ls())[1], 'foo')
101+
end)),
102+
test('create nested directories', in_tmpdir(function ()
103+
system.mkdir('foo/bar', true)
104+
assert.are_equal((system.ls())[1], 'foo')
105+
assert.are_equal((system.ls 'foo')[1], 'bar')
106+
end)),
107+
test('cannot create existing directory', in_tmpdir(function ()
108+
assert.error_matches(function () system.mkdir '.' end, '%.')
109+
end)),
110+
test('optionally ignores existing directories', in_tmpdir(function ()
111+
system.mkdir 'foo'
112+
system.mkdir('foo', true)
113+
end)),
114+
test('normal operation', in_tmpdir(function () system.mkdir 'foo' end)),
115+
},
116+
117+
118+
group 'rmdir' {
119+
test('remove empty directory', in_tmpdir(function ()
120+
system.mkdir 'remove-me'
121+
system.rmdir 'remove-me'
122+
assert.are_same(system.ls(), {})
123+
end)),
124+
test('fail if directory is not empty', in_tmpdir(function ()
125+
system.mkdir('outer/inner', true)
126+
assert.error_matches(function () system.rmdir('outer') end, '.')
127+
end)),
128+
test('optionally delete recursively', in_tmpdir(function ()
129+
system.mkdir('outer/inner', true)
130+
system.rmdir('outer', true)
131+
assert.are_same(system.ls(), {})
132+
end))
133+
},
134+
135+
group 'tmpdirname' {
136+
test('returns a string', function ()
137+
assert.are_equal(type(system.tmpdirname()), 'string')
138+
end)
139+
},
140+
141+
group 'with_env' {
142+
test('resets environment', function ()
143+
local outer_value = 'outer test value'
144+
local inner_value = 'inner test value'
145+
local inner_only = 'test #2'
146+
147+
function check_env ()
148+
assert.are_equal(os.getenv 'HSLUA_SYSTEM_TEST', inner_value)
149+
assert.are_equal(
150+
os.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY',
151+
inner_only
152+
)
153+
assert.is_nil(os.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY')
154+
end
155+
156+
local test_env = {
157+
HSLUA_SYSTEM_TEST = inner_value,
158+
HSLUA_SYSTEM_TEST_INNER_ONLY = inner_only
159+
}
160+
system.setenv('HSLUA_SYSTEM_TEST_OUTER_ONLY', outer_value)
161+
system.setenv('HSLUA_SYSTEM_TEST', outer_value)
162+
system.with_env(test_env, check_env)
163+
assert.are_equal(system.getenv 'HSLUA_SYSTEM_TEST', outer_value)
164+
assert.is_nil(system.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY')
165+
assert.are_equal(
166+
system.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY',
167+
outer_value
168+
)
169+
end)
170+
},
171+
172+
group 'with_tmpdir' {
173+
test('no base directory given', function ()
174+
assert.are_equal(system.with_tmpdir('foo', write_read_token), token)
175+
end),
176+
test('cwd as base directory', function ()
177+
assert.are_equal(system.with_tmpdir('.', 'foo', write_read_token), token)
178+
end),
179+
},
180+
181+
group 'with_wd' {
182+
test('can change to test directory', function ()
183+
system.with_wd('test', function ()
184+
local cwd = system.getwd()
185+
assert.is_truthy(cwd:match 'test$')
186+
end)
187+
end),
188+
test('returns to old directory once done', function ()
189+
local cwd = system.getwd()
190+
system.with_wd('test', function () end)
191+
assert.are_equal(system.getwd(), cwd)
192+
end),
193+
test('working directory is passed to callback', function ()
194+
system.with_wd('test', function (path)
195+
assert.is_truthy(system.getwd():match (path .. '$'))
196+
end)
197+
end),
198+
}
199+
}

0 commit comments

Comments
 (0)