These exercises practice the Module 11 reading. They aren’t graded. Answer each one
yourself before you open its answer, and then confirm it with the tools, from a terminal where
source .venv/lua/bin/activate has run.
Worked example: one command that checks everything¶
A project should be checked the same way on every machine and in continuous integration. Write a
script, check.sh, that runs the formatter’s check, the linter, the type checker, and the tests
with coverage, and stops at the first that fails.
Stop on failure.
set -emakes a shell script exit as soon as a command fails, and each tool exits with a nonzero status when it finds a problem.Check, don’t change. In a script that verifies, StyLua runs with
--check; reformatting belongs on the developer’s machine.Order by speed. The formatter and the linter take a fraction of a second, so they run first, and a typo stops the script before the slower steps.
#!/bin/sh
set -e
stylua --check src spec
luacheck src spec
lua-language-server --check=src --checklevel=Warning
busted --coverage
luacovThe script ends after luacov writes luacov.report.out. It doesn’t fail on low coverage; a
project that wants a minimum must check the report’s last line itself.
Exercise 11.1 (LO-11.1, ★, about 10 minutes)¶
Find three problems in this rockspec, which luarocks lint or luarocks make would reject.
package = "csvtool"
version = "1.0"
source = { url = "git+https://example.org/csvtool.git" }
description = { summary = "Prints statistics for the columns of a CSV file." }
dependencies = { "lua >= 5.4" }
build = { type = "builtin", modules = { csvtool = "csvtool.lua" } }The module’s source is src/csvtool.lua.
Answer
The version needs a rockspec revision,
"1.0-1", which matches the file namecsvtool-1.0-1.rockspec.description.licenseis missing, whichluarocks lintrejects.The module maps to
csvtool.lua, but the file issrc/csvtool.lua, so the build can’t find it.
Exercise 11.2 (LO-11.2, ★★, about 15 minutes)¶
For each luacheck finding, say whether it is most likely a bug or dead code, and how to fix it.
unused variable 'result', for a linelocal result = parse(line)followed byreturn true.setting non-standard global variable 'count', inside a function.unused argument 'opts', infunction M.render(text, opts).shadowing upvalue 'err', forlocal ok, err = pcall(f)inside a function that already has anerr.
Answer
Probably a bug: the function parses the line and ignores the result. Return or use
result; if the call is there only for its error, writeparse(line)without the variable.A bug: a missing
local. Declarelocal countin the function.Either dead code or an unfinished feature. If
optsis part of a public interface that a later version will use, keep it and silence only that warning on that line, with a comment saying why; otherwise remove the parameter.A likely bug: the inner
errhides the outer one, so code after the block may read the wrong one. Rename the inner variable.
Exercise 11.3 (LO-11.2, ★★, about 10 minutes)¶
A teammate’s pull request changes 300 lines, but only five do anything; the rest is indentation their editor changed. What would have prevented that, and what does the project need?
Answer
A formatter that every contributor runs, with its settings in the project: a stylua.toml, and stylua --check in continuous integration, which fails the pull request until the code matches the agreed layout. Then layout changes never appear in a diff, and the reviewer sees only the five lines that matter.
Exercise 11.4 (LO-11.3, ★★, about 15 minutes)¶
Annotate this function with LuaCATS. name is a string; greeting is an optional string; the
result is a string.
function M.greet(name, greeting)
return (greeting or "Hello") .. ", " .. name
endAnswer
---Returns a greeting for `name`.
---@param name string
---@param greeting? string The word to greet with; "Hello" if absent.
---@return string
function M.greet(name, greeting)
return (greeting or "Hello") .. ", " .. name
endThe ? after the parameter’s name marks it optional, so M.greet("Ada") passes the check, and M.greet(42) fails it with param-type-mismatch.
Exercise 11.5 (LO-11.4, ★★, about 10 minutes)¶
A module’s coverage report shows 100%, yet this function’s error has never been tested. How can both be true, and how would you make the report show it?
function M.parse(text) if text == "" then error("empty input", 2) end return text:upper() endAnswer
LuaCov counts lines, and the whole function, if and error included, is one line, which counts as covered whenever any test calls parse. Let StyLua lay it out, which puts the error on a line of its own; the report then marks that line *0 until a test calls parse("") and checks the error.