Skip to content

Commit

Permalink
gen: change behavior to check by default, add --no-check
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 9, 2025
1 parent 78c7fbb commit 07c1c25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
55 changes: 37 additions & 18 deletions spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local util = require("spec.util")

local function popen_tl(...)
return io.popen(util.tl_cmd(...) .. " 2>&1", "r")
end

local input_file = [[
global type1 = 2
Expand Down Expand Up @@ -108,7 +112,7 @@ describe("tl gen", function()
describe("on .tl files", function()
it("works on empty files", function()
local name = util.write_tmp_file(finally, [[]])
local pd = io.popen(util.tl_cmd("gen", name), "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -124,7 +128,7 @@ describe("tl gen", function()
print(add(10, 20))
]])
local pd = io.popen(util.tl_cmd("gen", name), "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -140,15 +144,30 @@ describe("tl gen", function()

it("handles Unix newlines on every OS", function()
local name = util.write_tmp_file(finally, "print'1'--comment\nprint'2'\nprint'3'\n")
local pd = io.popen(util.tl_cmd("gen", name), "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
assert.same("print('1')\nprint('2')\nprint('3')\n", util.read_file(lua_name))
end)

it("ignores type errors", function()
it("catches type errors by default", function()
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 = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(1, pd:close())
assert.match("2 errors", output, 1, true)
end)

it("ignores type errors with --no-check", function()
local name = util.write_tmp_file(finally, [[
local function add(a: number, b: number): number
return a + b
Expand All @@ -157,10 +176,10 @@ describe("tl gen", function()
print(add("string", 20))
print(add(10, true))
]])
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>" .. util.os_null, "r")
local pd = popen_tl("gen", "--no-check", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
assert.same("", output)
assert.match("Wrote:", output, 1, true)
local lua_name = tl_to_lua(name)
util.assert_line_by_line([[
local function add(a, b)
Expand All @@ -176,22 +195,22 @@ describe("tl gen", function()
local name = util.write_tmp_file(finally, [[
print(add("string", 20))))))
]])
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>" .. util.os_null, "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(1, pd:close())
assert.match("1 syntax error:", output, 1, true)
end)

it("ignores unknowns code 0 if no errors", function()
it("ignores unknowns with --no-check", function()
local name = util.write_tmp_file(finally, [[
local function unk(x, y): number, number
return a + b
end
]])
local pd = io.popen(util.tl_cmd("gen", name) .. " 2>&1 1>" .. util.os_null, "r")
local pd = popen_tl("gen", "--no-check", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
assert.same("", output)
assert.match("Wrote:", output, 1, true)
local lua_name = tl_to_lua(name)
util.assert_line_by_line([[
local function unk(x, y)
Expand All @@ -202,7 +221,7 @@ describe("tl gen", function()

it("does not mess up the indentation (#109)", function()
local name = util.write_tmp_file(finally, input_file)
local pd = io.popen(util.tl_cmd("gen", name), "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -214,7 +233,7 @@ describe("tl gen", function()
for i, case in ipairs(hashbang_cases) do
it("[" .. i .. "] preserves hashbang with --keep-hashbang", function()
local name = util.write_tmp_file(finally, case.with_hashbang)
local pd = io.popen(util.tl_cmd("gen", "--keep-hashbang", name), "r")
local pd = popen_tl("gen", "--keep-hashbang", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -224,7 +243,7 @@ describe("tl gen", function()

it("[" .. i .. "] drops hashbang when not using --keep-hashbang", function()
local name = util.write_tmp_file(finally, case.with_hashbang)
local pd = io.popen(util.tl_cmd("gen", name), "r")
local pd = popen_tl("gen", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -240,7 +259,7 @@ describe("tl gen", function()
local x = 2 // 3
local y = 2 << 3
]])
local pd = io.popen(util.tl_cmd("gen", "--gen-target=5.1", name), "r")
local pd = popen_tl("gen", "--gen-target=5.1", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -252,15 +271,15 @@ describe("tl gen", function()
]], util.read_file(lua_name))
end)

it("generates bit32 operations even for invalid variables (regression test for #673)", function()
it("with --no-check generates bit32 operations even for invalid variables (regression test for #673)", function()
local name = util.write_tmp_file(finally, [[
local foo = require("nonexisting")
local y = 2 | (foo.wat << 9)
local x = ~y
local z = aa // bb
]])
local pd = io.popen(util.tl_cmd("gen", "--gen-target=5.1", name), "r")
local pd = popen_tl("gen", "--no-check", "--gen-target=5.1", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand All @@ -281,7 +300,7 @@ describe("tl gen", function()
local x = 2 // 3
local y = 2 << 3
]])
local pd = io.popen(util.tl_cmd("gen", "--gen-target=5.3", name), "r")
local pd = popen_tl("gen", "--gen-target=5.3", name)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand Down Expand Up @@ -371,7 +390,7 @@ describe("tl gen", function()

local function run_gen_with_flag(finally, flag, output_code)
local name = util.write_tmp_file(finally, input_code)
local pd = io.popen(util.tl_cmd("gen", name, flag), "r")
local pd = popen_tl("gen", name, flag)
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
Expand Down
9 changes: 5 additions & 4 deletions tl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ local function get_args_parser()

local gen_command = parser:command("gen", "Generate a Lua file for one or more Teal files.")
gen_command:argument("file", "The Teal source file."):args("+")
gen_command:flag("-c --check", "Type check and fail on type errors.")
gen_command:flag("-c --check", "Type check and fail on type errors. This is the default."):hidden(true)
gen_command:flag("--no-check", "Do not fail on type errors, only on syntax errors.")
gen_command:flag("--keep-hashbang", "Preserve hashbang line (#!) at the top of file if present.")
gen_command:option("-o --output", "Write to <filename> instead.")
:argname("<filename>")
Expand Down Expand Up @@ -806,14 +807,14 @@ commands["gen"] = function(tlconfig, args)
check_collect(i)
end

local ok, serr, terr = report_all_errors(tlconfig, env, args["no_check"])

for _, res in ipairs(results) do
if #res.tl_result.syntax_errors == 0 then
if (not serr) and (args["no_check"] or not terr) then
write_out(tlconfig, res.tl_result, args["output"] or res.output_file, pp_opts)
end
end

local ok = report_all_errors(tlconfig, env, not args["check"])

os.exit(ok and 0 or 1)
end

Expand Down

0 comments on commit 07c1c25

Please sign in to comment.