chore(*) use busted for specs

it does no longer hang
This commit is contained in:
eskerda 2020-12-13 18:54:19 +01:00 committed by Enrique García Cota
parent 3757048d27
commit ddbc7e12cc
2 changed files with 29 additions and 31 deletions

View File

@ -120,11 +120,9 @@ This library is released under the MIT license. See MIT-LICENSE.txt for details
Specs Specs
===== =====
This project uses [telescope](https://github.com/norman/telescope) for its specs. In order to run them, install it and then: This project uses [busted](https://github.com/Olivine-Labs/busted) for its specs. In order to run them, install it and then:
``` ```
cd /path/to/where/the/spec/folder/is cd /path/to/where/the/spec/folder/is
tsc spec/* busted spec/*
``` ```
I would love to use [busted](http://olivinelabs.com/busted/), but it has some incompatibility with `debug.sethook(f, "", quota)` and the tests just hanged up.

View File

@ -5,44 +5,44 @@ describe('sandbox.run', function()
describe('when handling base cases', function() describe('when handling base cases', function()
it('can run harmless functions', function() it('can run harmless functions', function()
local r = sandbox.run(function() return 'hello' end) local r = sandbox.run(function() return 'hello' end)
assert_equal(r, 'hello') assert.equal(r, 'hello')
end) end)
it('can run harmless strings', function() it('can run harmless strings', function()
local r = sandbox.run("return 'hello'") local r = sandbox.run("return 'hello'")
assert_equal(r, 'hello') assert.equal(r, 'hello')
end) end)
it('has access to safe methods', function() it('has access to safe methods', function()
assert_equal(10, sandbox.run("return tonumber('10')")) assert.equal(10, sandbox.run("return tonumber('10')"))
assert_equal('HELLO', sandbox.run("return string.upper('hello')")) assert.equal('HELLO', sandbox.run("return string.upper('hello')"))
assert_equal(1, sandbox.run("local a = {3,2,1}; table.sort(a); return a[1]")) assert.equal(1, sandbox.run("local a = {3,2,1}; table.sort(a); return a[1]"))
assert_equal(10, sandbox.run("return math.max(1,10)")) assert.equal(10, sandbox.run("return math.max(1,10)"))
end) end)
it('does not allow access to not-safe stuff', function() it('does not allow access to not-safe stuff', function()
assert_error(function() sandbox.run('return setmetatable({}, {})') end) assert.error(function() sandbox.run('return setmetatable({}, {})') end)
assert_error(function() sandbox.run('return string.rep("hello", 5)') end) assert.error(function() sandbox.run('return string.rep("hello", 5)') end)
end) end)
end) end)
describe('when handling string.rep', function() describe('when handling string.rep', function()
it('does not allow pesky string:rep', function() it('does not allow pesky string:rep', function()
assert_error(function() sandbox.run('return ("hello"):rep(5)') end) assert.error(function() sandbox.run('return ("hello"):rep(5)') end)
end) end)
it('restores the value of string.rep', function() it('restores the value of string.rep', function()
sandbox.run("") sandbox.run("")
assert_equal('hellohello', string.rep('hello', 2)) assert.equal('hellohello', string.rep('hello', 2))
end) end)
it('restores string.rep even if there is an error', function() it('restores string.rep even if there is an error', function()
assert_error(function() sandbox.run("error('foo')") end) assert.error(function() sandbox.run("error('foo')") end)
assert_equal('hellohello', string.rep('hello', 2)) assert.equal('hellohello', string.rep('hello', 2))
end) end)
it('passes parameters to the function', function() it('passes parameters to the function', function()
assert_equal(sandbox.run(function(a,b) return a + b end, {}, 1,2), 3) assert.equal(sandbox.run(function(a,b) return a + b end, {}, 1,2), 3)
end) end)
end) end)
@ -50,19 +50,19 @@ describe('sandbox.run', function()
describe('when the sandboxed function tries to modify the base environment', function() describe('when the sandboxed function tries to modify the base environment', function()
it('does not allow modifying the modules', function() it('does not allow modifying the modules', function()
assert_error(function() sandbox.run("string.foo = 1") end) assert.error(function() sandbox.run("string.foo = 1") end)
assert_error(function() sandbox.run("string.char = 1") end) assert.error(function() sandbox.run("string.char = 1") end)
end) end)
it('does not persist modifications of base functions', function() it('does not persist modifications of base functions', function()
sandbox.run('error = function() end') sandbox.run('error = function() end')
assert_error(function() sandbox.run("error('this should be raised')") end) assert.error(function() sandbox.run("error('this should be raised')") end)
end) end)
it('DOES persist modification to base functions when they are provided by the base env', function() it('DOES persist modification to base functions when they are provided by the base env', function()
local env = {['next'] = 'hello'} local env = {['next'] = 'hello'}
sandbox.run('next = "bye"', {env=env}) sandbox.run('next = "bye"', {env=env})
assert_equal(env['next'], 'bye') assert.equal(env['next'], 'bye')
end) end)
end) end)
@ -70,21 +70,21 @@ describe('sandbox.run', function()
describe('when given infinite loops', function() describe('when given infinite loops', function()
it('throws an error with infinite loops', function() it('throws an error with infinite loops', function()
assert_error(function() sandbox.run("while true do end") end) assert.error(function() sandbox.run("while true do end") end)
end) end)
it('restores string.rep even after a while true', function() it('restores string.rep even after a while true', function()
assert_error(function() sandbox.run("while true do end") end) assert.error(function() sandbox.run("while true do end") end)
assert_equal('hellohello', string.rep('hello', 2)) assert.equal('hellohello', string.rep('hello', 2))
end) end)
it('accepts a quota param', function() it('accepts a quota param', function()
assert_not_error(function() sandbox.run("for i=1,100 do end") end) assert.has_no.errors(function() sandbox.run("for i=1,100 do end") end)
assert_error(function() sandbox.run("for i=1,100 do end", {quota = 20}) end) assert.error(function() sandbox.run("for i=1,100 do end", {quota = 20}) end)
end) end)
it('does not use quotes if the quote param is false', function() it('does not use quotes if the quote param is false', function()
assert_not_error(function() sandbox.run("for i=1,1000000 do end", {quota = false}) end) assert.has_no.errors(function() sandbox.run("for i=1,1000000 do end", {quota = false}) end)
end) end)
end) end)
@ -93,18 +93,18 @@ describe('sandbox.run', function()
describe('when given an env option', function() describe('when given an env option', function()
it('is available on the sandboxed env as the _G variable', function() it('is available on the sandboxed env as the _G variable', function()
local env = {foo = 1} local env = {foo = 1}
assert_equal(1, sandbox.run("return foo", {env = env})) assert.equal(1, sandbox.run("return foo", {env = env}))
assert_equal(env, sandbox.run("return _G", {env = env})) assert.equal(env, sandbox.run("return _G", {env = env}))
end) end)
it('does not hide base env', function() it('does not hide base env', function()
assert_equal('HELLO', sandbox.run("return string.upper(foo)", {env = {foo = 'hello'}})) assert.equal('HELLO', sandbox.run("return string.upper(foo)", {env = {foo = 'hello'}}))
end) end)
it('can modify the env', function() it('can modify the env', function()
local env = {foo = 1} local env = {foo = 1}
sandbox.run("foo = 2", {env = env}) sandbox.run("foo = 2", {env = env})
assert_equal(env.foo, 2) assert.equal(env.foo, 2)
end) end)
end) end)