teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.02k stars 101 forks source link

It appears that Issue #646 is not fixed by commit 2fc72bf #729

Closed JLPLabs closed 6 months ago

JLPLabs commented 6 months ago

Issue #646 pertains to hashbang on line 1 surviving through to the generated Lua script.

This note shows:

  1. Demonstration of problem
  2. How the spec ("spec/cli/gen_spec.lua") could be updated to catch the error

Demonstration of Problem

Note insertion of blank line above hashbang in the generated Lua file

tl> cat -n test_1print.tl
     1  #!/usr/bin/env lua
     2  print("hello world")
     3  

tl> tl gen --keep-hashbang test_1print.tl
Wrote: test_1print.lua

tl> cat -n test_1print.lua               
     1
     2  #!/usr/bin/env lua
     3  print("hello world")

Current and Updated Spec

Current spec compares lines, it doesn't seem to catch a blank line being inserted

CURRENT -- This test "passes"

   it("preserves hashbang with --keep-hashbang", function()
      local name = util.write_tmp_file(finally, script_with_hashbang)
      local pd = io.popen(util.tl_cmd("gen", "--keep-hashbang", name), "r")
      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)
      util.assert_line_by_line(script_with_hashbang, util.read_file(lua_name))
   end)

change the assert and now the error (inserted blank line) is detected

PROPOSED -- This test "fails" (as it should, given the hashbang isn't on line 1)

   it("*equality test* to confirm preserves hashbang with --keep-hashbang", function()
      local name = util.write_tmp_file(finally, script_with_hashbang)
      local pd = io.popen(util.tl_cmd("gen", "--keep-hashbang", name), "r")
      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.equal(script_with_hashbang, util.read_file(lua_name))
   end)

The proposed spec generates this error

tl gen *equality test* to confirm preserves hashbang with --keep-hashbang
spec/cli/gen_spec.lua:262: Expected objects to be equal.
Passed in:
(string) '
#!/usr/bin/env lua
print("hello world")
'
Expected:
(string) '#!/usr/bin/env lua
print("hello world")
'