Skip to content
Snippets Groups Projects
Commit 0e2ffb5d authored by Marek Vavruša's avatar Marek Vavruša
Browse files

Added luacheck for linting Lua files and static analysis

This is super useful for checking things like misusing undefined
variables or modifying globals, especially in modules when it's
not immediately visible which variables are in the global
namespace and which are not.

I added several exceptions for files in daemon/lua and tests,
as for example sandbox module needs to legitimately modify
global namespace.

There's a lot of things failing, so I didn't make it part of the
standard `make check`, but we should eventually enable it to
improve code quality and spot problems with CI.
parent d6c55c8a
No related branches found
No related tags found
1 merge request!398Added luacheck for linting Lua files and static analysis
Pipeline #
std = 'luajit'
new_read_globals = {
'help',
'quit',
'hostname',
'moduledir',
'user',
'verbose',
'resolve',
'tojson',
'todname',
'map',
'net',
'cache',
'modules',
'trust_anchors',
'worker',
'event',
'_hint_root_file',
-- Sandbox declarations
'kB',
'MB',
'GB',
'sec',
'second',
'minute',
'min',
'hour',
'day',
'panic',
'warn',
'log',
'mode',
'trust_anchors',
'reorder_RR',
'option',
'env',
'kres',
'trustanchor',
'libknot_SONAME',
'libzscanner_SONAME',
'table_print',
'__engine',
'_ENV',
'_SANDBOX',
}
new_globals = {
-- Modules are allowed to be set and accessed from global namespace
'policy',
'view',
'stats',
'http',
}
-- Luacheck < 0.18 doesn't support new_read_globals
for _, v in ipairs(new_read_globals) do
table.insert(new_globals, v)
end
-- Ignore test files
exclude_files = {
'modules/policy/lua-aho-corasick', -- Vendored
}
-- Ignore some pedantic checks
ignore = {
'4.1/err', -- Shadowing err
'4.1/.', -- Shadowing one letter variables
}
-- Sandbox can set global variables
files['daemon/lua'].ignore = {'111', '121', '122'}
-- Tests and scripts can use global variables
files['scripts'].ignore = {'111', '112', '113'}
files['tests'].ignore = {'111', '112', '113'}
\ No newline at end of file
...@@ -8,6 +8,8 @@ check: all tests ...@@ -8,6 +8,8 @@ check: all tests
clean: contrib-clean lib-clean daemon-clean client-clean modules-clean \ clean: contrib-clean lib-clean daemon-clean client-clean modules-clean \
tests-clean doc-clean bench-clean tests-clean doc-clean bench-clean
doc: doc-html doc: doc-html
lint:
luacheck --codes .
.PHONY: all install check clean doc info .PHONY: all install check clean doc info
# Options # Options
......
...@@ -62,6 +62,7 @@ There are also *optional* packages that enable specific functionality in Knot DN ...@@ -62,6 +62,7 @@ There are also *optional* packages that enable specific functionality in Knot DN
"libprotobuf_ 3.0+", "``modules/dnstap``", "Protocol Buffers support for dnstap_." "libprotobuf_ 3.0+", "``modules/dnstap``", "Protocol Buffers support for dnstap_."
"`libprotobuf-c`_ 1.0+", "``modules/dnstap``", "C bindings for Protobuf." "`libprotobuf-c`_ 1.0+", "``modules/dnstap``", "C bindings for Protobuf."
"libfstrm_ 0.2+", "``modules/dnstap``", "Frame Streams data transport protocol." "libfstrm_ 0.2+", "``modules/dnstap``", "Frame Streams data transport protocol."
"luacheck_", "``lint``", "Syntax and static analysis checker for Lua."
.. [#] Requires C99, ``__attribute__((cleanup))`` and ``-MMD -MP`` for dependency file generation. GCC, Clang and ICC are supported. .. [#] Requires C99, ``__attribute__((cleanup))`` and ``-MMD -MP`` for dependency file generation. GCC, Clang and ICC are supported.
.. [#] You can use variables ``<dependency>_CFLAGS`` and ``<dependency>_LIBS`` to configure dependencies manually (i.e. ``libknot_CFLAGS`` and ``libknot_LIBS``). .. [#] You can use variables ``<dependency>_CFLAGS`` and ``<dependency>_LIBS`` to configure dependencies manually (i.e. ``libknot_CFLAGS`` and ``libknot_LIBS``).
...@@ -252,6 +253,7 @@ The project can be built with code coverage tracking using the ``COVERAGE=1`` va ...@@ -252,6 +253,7 @@ The project can be built with code coverage tracking using the ``COVERAGE=1`` va
Running unit and integration tests Running unit and integration tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The linter requires luacheck_ and is executed by ``make lint``.
The unit tests require cmocka_ and are executed by ``make check``. The unit tests require cmocka_ and are executed by ``make check``.
Tests for the dnstap module need go and are executed by ``make ckeck-dnstap``. Tests for the dnstap module need go and are executed by ``make ckeck-dnstap``.
...@@ -319,5 +321,6 @@ You can hack on the container by changing the container entrypoint to shell like ...@@ -319,5 +321,6 @@ You can hack on the container by changing the container entrypoint to shell like
.. _libprotobuf: https://developers.google.com/protocol-buffers/ .. _libprotobuf: https://developers.google.com/protocol-buffers/
.. _libprotobuf-c: https://github.com/protobuf-c/protobuf-c/wiki .. _libprotobuf-c: https://github.com/protobuf-c/protobuf-c/wiki
.. _libfstrm: https://github.com/farsightsec/fstrm .. _libfstrm: https://github.com/farsightsec/fstrm
.. _luacheck: http://luacheck.readthedocs.io
.. _DESTDIR: https://www.gnu.org/prep/standards/html_node/DESTDIR.html .. _DESTDIR: https://www.gnu.org/prep/standards/html_node/DESTDIR.html
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment