diff --git a/spec/cli/gen_spec.lua b/spec/cli/gen_spec.lua index 7f537e10..2f0cb177 100644 --- a/spec/cli/gen_spec.lua +++ b/spec/cli/gen_spec.lua @@ -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 @@ -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) @@ -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) @@ -140,7 +144,7 @@ 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) @@ -148,7 +152,22 @@ describe("tl gen", function() 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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -252,7 +271,7 @@ 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") @@ -260,7 +279,7 @@ describe("tl gen", function() 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) @@ -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) @@ -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) diff --git a/tl b/tl index 98a26cfc..71c49d57 100755 --- a/tl +++ b/tl @@ -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 instead.") :argname("") @@ -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