Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 11: Packages and tooling

Objectives: LO-11.1, LO-11.2, LO-11.3, LO-11.4
Prerequisites: the Module 11 reading, and LuaRocks, busted, luacheck, LuaCov, StyLua, and lua-language-server, installed as setting up describes
Time: about 2 hours

This lab works on files, not notebook cells: each exercise writes Lua files into the lab’s scratch/project folder, and Python cells run the community’s tools on them, as you would in a terminal. You will write a rockspec and build two rocks into a project tree, clean up a module until luacheck and StyLua have nothing to say, annotate a module so that lua-language-server catches the mistakes in its caller, and add tests until LuaCov finds no line they skip. As in the earlier labs, each check cell prints passed or fails with a message.

Setup

The first cell finds the tools and defines run, which runs one in a project folder and returns its exit status and output. If any tool shows NOT FOUND, install it as setting up describes, then run the cell again. The second cell writes the files the exercises start from, replacing any earlier copies, so run it again whenever you want to start over.

1. A rockspec, and a tree for the project

textutil is a finished rock. wordfreq uses it, and needs a rockspec.

Exercise L11.1 (LO-11.1, ★★, about 25 minutes)

Finish the rockspec for wordfreq, version 0.1, rockspec revision 1. It needs the fields package, version, source (any url; nothing fetches it here), description with a summary and a license, dependencies on Lua 5.4 and on textutil version 0.1 or later, and a builtin build that maps the module wordfreq to src/wordfreq.lua. Run the cell to write the file, then run the two checks.

The first check reads the rockspec as Lua, in an empty environment, and looks at its fields.

The second check lints the rockspec, builds textutil and then wordfreq into the project’s tree, scratch/project/lua_modules, and runs the installed module with lua.

2. Lint and layout

stats.lua below works for mean, but luacheck has eight findings, StyLua would change its layout, and one of busted’s two tests in spec/stats_spec.lua fails. Run the cell, and the cell after it, which runs all three tools and prints what they say.

Exercise L11.2 (LO-11.2, ★★, about 30 minutes)

Fix every luacheck finding in stats.lua by changing the code, not by silencing luacheck: no luacheck: comment and no change to .luacheckrc. One finding is the cause of the failing test. Then run the next cell, which lets StyLua lay the file out, and run the check. Rewrite the %%writefile cell above, not the file: running that cell again replaces the file, so run the StyLua cell after it each time.

3. Types the checker can see

shapes/caller.lua makes two mistakes: it scales by the string "2", and asks for the area of a number. Run the next cell, which writes shapes.lua without annotations, and the cell after it, which runs lua-language-server on the folder. It finds nothing, because nothing says what types the functions expect.

Exercise L11.3 (LO-11.3, ★★, about 25 minutes)

Annotate shapes.lua with LuaCATS: a class Rect with number fields width and height, and @param and @return annotations for all three functions, with a one-line description of each. Then run both cells again. The checker should report the two mistakes in caller.lua, on lines 3 and 4, and nothing in shapes.lua. Don’t change caller.lua.

Hint

A class is declared in comments: ---@class Rect, then one ---@field line per field. After that, ---@param rect Rect and ---@return Rect can name it.

4. Coverage

grades.lua turns scores into letter grades, and raises an error for a score out of range. The spec below tests only two cases. Run it, then the cell after it, which runs the tests with coverage and prints LuaCov’s report for grades.lua: every line marked *0 is a line no test ran.

Exercise L11.4 (LO-11.4, ★★, about 25 minutes)

Add tests to the spec until LuaCov marks no line of grades.lua with *0. Each test must check a result, not only call the function. Run the spec’s cell and the coverage cell after each change, and read which lines are still marked.

Summary

  • A rockspec lists a rock’s dependencies and modules; luarocks --tree … make builds rocks into a tree of the project’s own, resolving a dependency from that tree.

  • luacheck’s findings were real: a missing local and a shadowed variable that broke max. StyLua settled the layout without discussion.

  • Without annotations the checker had nothing to check against; with them, it caught both of the caller’s mistakes without running anything.

  • LuaCov pointed at each line the tests skipped, including the error branch.

Next, do the Module 11 practice, then start PA5.