Skip to content

Commit

Permalink
Rework tests to not use lfs.link + refactor temp file creation
Browse files Browse the repository at this point in the history
 - This was the easy solution that worked at first when `tl build` was
   implemented, but making a bunch of random links can cause problems,
   so now we just run the `tl` command given its full path and tell
   busted to add the current dir to the lua path so `tl` can find
   `tl.lua`
 - write_tmp_file will now generate its own name and now
   accepts a parameter for the extension of the file it generates, i.e.
   previously: `write_tmp_file(finally, name, content)`
          now: `write_tmp_file(finally, content, extension)`
 - Add "tl", and ".busted" to .editorconfig
  • Loading branch information
euclidianAce committed Dec 31, 2020
1 parent 3ca2c69 commit d1d1750
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 149 deletions.
7 changes: 4 additions & 3 deletions .busted
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
default = {
lpath = "./?.lua";
}
default = {
lpath = "./?.lua;"
.. (require("lfs").currentdir()) .. "/?.lua;";
}
}
6 changes: 1 addition & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.lua]
indent_style = space
indent_size = 3

[*.tl]
[{*.lua,*.tl,tl,.busted}]
indent_style = space
indent_size = 3

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
tl.lua.1
tl.lua.2
tl.lua.bak
/luarocks
/lua
/lua_modules
/.luarocks
48 changes: 24 additions & 24 deletions spec/cli/check_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ local util = require("spec.util")
describe("tl check", function()
describe("on .tl files", function()
it("reports 0 errors and code 0 on success", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add(10, 20))
]])
local pd = io.popen("./tl check " .. name, "r")
local pd = io.popen(util.tl_cmd("check", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.match("0 errors detected", output, 1, true)
end)

it("reports number of errors in stderr and code 1 on type errors", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
Expand All @@ -32,46 +32,46 @@ describe("tl check", function()
end)

it("reports errors in multiple files", function()
local name1 = util.write_tmp_file(finally, "add.tl", [[
local name1 = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add("string", 20))
print(add(10, true))
]])
local name2 = util.write_tmp_file(finally, "foo.tl", [[
local name2 = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add("string", 20))
print(add(10, true))
]])
local pd = io.popen("./tl check " .. name1 .. " " .. name2 .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("check", name1, name2) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("add.tl:", output, 1, true)
assert.match("foo.tl:", output, 1, true)
assert.match(name1 .. ":", output, 1, true)
assert.match(name2 .. ":", output, 1, true)
end)

it("reports number of errors in stderr and code 1 on syntax errors", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = util.write_tmp_file(finally, [[
print(add("string", 20))))))
]])
local pd = io.popen("./tl check " .. name .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("check", name) .. "2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("1 syntax error:", output, 1, true)
end)

it("reports use of unknowns as errors in stderr and returns code 1", function()
local name = util.write_tmp_file(finally, "add.tl", [[
local name = util.write_tmp_file(finally, [[
local function unk(x, y): number, number
return a + b
end
]])
local pd = io.popen("./tl check " .. name .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("check", name) .. "2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("2 errors:", output, 1, true)
Expand All @@ -82,55 +82,55 @@ describe("tl check", function()

describe("on .lua files", function()
it("reports 0 errors and code 0 on success", function()
local name = util.write_tmp_file(finally, "add.lua", [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add(10, 20))
]])
local pd = io.popen("./tl check " .. name, "r")
]], "lua")
local pd = io.popen(util.tl_cmd("check", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.match("0 errors detected", output, 1, true)
end)

it("reports number of errors in stderr and code 1 on type errors", function()
local name = util.write_tmp_file(finally, "add.lua", [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add("string", 20))
print(add(10, true))
]])
local pd = io.popen("./tl check " .. name .. " 2>&1 1>/dev/null", "r")
]], "lua")
local pd = io.popen(util.tl_cmd("check", name) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("2 errors:", output, 1, true)
end)

it("reports number of errors in stderr and code 1 on syntax errors", function()
local name = util.write_tmp_file(finally, "add.lua", [[
local name = util.write_tmp_file(finally, [[
print(add("string", 20))))))
]])
local pd = io.popen("./tl check " .. name .. " 2>&1 1>/dev/null", "r")
]], "lua")
local pd = io.popen(util.tl_cmd("check", name) .. "2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("1 syntax error:", output, 1, true)
end)

it("reports unknowns variables in stderr and code 0 if no errors", function()
local name = util.write_tmp_file(finally, "add.lua", [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
local function sub(x, y): number
return x + y
end
]])
local pd = io.popen("./tl check " .. name .. " 2>&1 1>/dev/null", "r")
]], "lua")
local pd = io.popen(util.tl_cmd("check", name) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.match("2 unknown variables:", output, 1, true)
Expand Down
38 changes: 15 additions & 23 deletions spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,22 @@ local c = 100
]]

local function tl_to_lua(name)
return (name:gsub("%.tl$", ".lua"))
return (name:gsub("%.tl$", ".lua"):gsub("^/tmp/", ""))
end

describe("tl gen", function()
setup(util.chdir_setup)
teardown(util.chdir_teardown)
describe("on .tl files", function()
it("reports 0 errors and code 0 on success", function()
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add(10, 20))
]])
local pd = io.popen("./tl gen " .. name, "r")
local pd = io.popen(util.tl_cmd("gen", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -99,16 +98,15 @@ describe("tl gen", function()
end)

it("ignores type errors", function()
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
end
print(add("string", 20))
print(add(10, true))
]])
local pd = io.popen("./tl gen " .. name .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.same("", output)
Expand All @@ -124,24 +122,22 @@ describe("tl gen", function()
end)

it("reports number of errors in stderr and code 1 on syntax errors", function()
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
print(add("string", 20))))))
]])
local pd = io.popen("./tl gen " .. name .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(nil, "exit", 1, pd:close())
assert.match("1 syntax error:", output, 1, true)
end)

it("ignores unknowns code 0 if no errors", function()
local name = "add.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
local function unk(x, y): number, number
return a + b
end
]])
local pd = io.popen("./tl gen " .. name .. " 2>&1 1>/dev/null", "r")
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>/dev/null", "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.same("", output)
Expand All @@ -154,9 +150,8 @@ describe("tl gen", function()
end)

it("does not mess up the indentation (#109)", function()
local name = "add.tl"
util.write_tmp_file(finally, name, input_file)
local pd = io.popen("./tl gen " .. name, "r")
local name = util.write_tmp_file(finally, input_file)
local pd = io.popen(util.tl_cmd("gen", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -167,12 +162,11 @@ describe("tl gen", function()

describe("with --skip-compat53", function()
it("does not add compat53 insertions", function()
local name = "test.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
local t = {1, 2, 3, 4}
print(table.unpack(t))
]])
local pd = io.popen("./tl --skip-compat53 gen " .. name, "r")
local pd = io.popen(util.tl_cmd("gen", "--skip-compat53", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -186,12 +180,11 @@ describe("tl gen", function()

describe("without --skip-compat53", function()
it("adds compat53 insertions by default", function()
local name = "test.tl"
util.write_tmp_file(finally, name, [[
local name = util.write_tmp_file(finally, [[
local t = {1, 2, 3, 4}
print(table.unpack(t))
]])
local pd = io.popen("./tl gen " .. name, "r")
local pd = io.popen(util.tl_cmd("gen", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -202,5 +195,4 @@ describe("tl gen", function()
]], util.read_file(lua_name))
end)
end)

end)
7 changes: 4 additions & 3 deletions spec/cli/include_dir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ local util = require("spec.util")

describe("-I --include-dir argument", function()
it("adds a directory to package.path", function()
local name = util.write_tmp_file(finally, "foo.tl", [[
local name = util.write_tmp_file(finally, [[
require("add")
local x: number = add(1, 2)
assert(x == 3)
]])

local pd = io.popen("./tl -I spec check " .. name, "r")
local pd = io.popen(util.tl_cmd("check", "-I", "spec", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
assert.match("0 errors detected", output, 1, true)
end)
it("adds a directory to package.cpath", function()
local name = util.write_tmp_file(finally, "foo.lua", [[
local name = util.write_tmp_file(finally, [[
print(package.cpath:match("spec/cli/") ~= nil)
]])
local pd = io.popen("./tl run -I spec/cli/ " .. name, "r")
local pd = io.popen(util.tl_cmd("run", "-I", "spec/cli/", name), "r")
local output = pd:read("*a")
util.assert_popen_close(true, "exit", 0, pd:close())
util.assert_line_by_line([[
Expand Down
10 changes: 5 additions & 5 deletions spec/cli/output_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("-o --output", function()
},
generated_files = { "foo.lua" },
cmd = "gen",
args = "bar/foo.tl",
args = { "bar/foo.tl" },
popen = {
status = true,
exit = "exit",
Expand All @@ -23,7 +23,7 @@ describe("-o --output", function()
dir_structure = {a={b={c={["foo.tl"] = [[print 'hey']]}}}},
generated_files = { "foo.lua" },
cmd = "gen",
args = "a/b/c/foo.tl",
args = { "a/b/c/foo.tl" },
popen = {
status = true,
exit = "exit",
Expand All @@ -33,7 +33,7 @@ describe("-o --output", function()
end)
it("should write to the given filename", function()
util.run_mock_project(finally, {
args = "foo.tl -o my_output_file.lua",
args = { "foo.tl", "-o", "my_output_file.lua" },
dir_structure = { ["foo.tl"] = [[print 'hey']] },
generated_files = { "my_output_file.lua" },
cmd = "gen",
Expand All @@ -46,7 +46,7 @@ describe("-o --output", function()
end)
it("should write to the given filename in a directory", function()
util.run_mock_project(finally, {
args = "foo.tl -o a/b/c/d.lua",
args = { "foo.tl", "-o", "a/b/c/d.lua" },
dir_structure = {
["foo.tl"] = [[print 'hey']],
a={b={c={}}},
Expand All @@ -64,7 +64,7 @@ describe("-o --output", function()
end)
it("should gracefully error when the output directory doesn't exist", function()
util.run_mock_project(finally, {
args = "foo.tl -o a/b/c/d.lua",
args = { "foo.tl", "-o", "a/b/c/d.lua" },
dir_structure = {
["foo.tl"] = [[print 'hey']],
},
Expand Down
Loading

0 comments on commit d1d1750

Please sign in to comment.