Skip to content

Commit bab1954

Browse files
committed
test: add and improve tests in Lua
Remove `test.sh`. Vendor the lester test framework.
1 parent f6dd66c commit bab1954

File tree

7 files changed

+855
-11
lines changed

7 files changed

+855
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
version: ${{ matrix.os.version }}
3636
shell: bash
3737
run: |
38-
sudo ./test.sh
38+
sudo ./tests.lua

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FILES := lsblk.lua
1+
FILES := lsblk.lua tests.lua
22

33
.PHONY: check
44
check: format lint

test.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests.lua

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#! /usr/libexec/flua
2+
-- Tests for lsblk.lua.
3+
--
4+
-- Copyright (c) 2025 D. Bohdan
5+
--
6+
-- Redistribution and use in source and binary forms, with or without
7+
-- modification, are permitted provided that the following conditions
8+
-- are met:
9+
-- 1. Redistributions of source code must retain the above copyright
10+
-- notice, this list of conditions and the following disclaimer.
11+
-- 2. Redistributions in binary form must reproduce the above copyright
12+
-- notice, this list of conditions and the following disclaimer in the
13+
-- documentation and/or other materials provided with the distribution.
14+
--
15+
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
-- SUCH DAMAGE.
26+
27+
local lester = require("vendor/lester/lester")
28+
local describe, it, expect = lester.describe, lester.it, lester.expect
29+
30+
lester.parse_args()
31+
32+
local function run(cmd)
33+
local f = io.popen(cmd .. " 2>&1", "r")
34+
local output = f:read("*a")
35+
local _, _, code = f:close()
36+
return code, output
37+
end
38+
39+
describe("lsblk.lua", function()
40+
describe("CLI options", function()
41+
it("should show help", function()
42+
local code, output = run("./lsblk.lua -h")
43+
44+
expect.equal(code, 0)
45+
expect.truthy(output:find("List information about block devices"))
46+
expect.truthy(output:find("--help"))
47+
expect.truthy(output:find("--version"))
48+
end)
49+
50+
it("should show version", function()
51+
local code, output = run("./lsblk.lua -V")
52+
53+
expect.equal(code, 0)
54+
expect.truthy(output:match("^%d+%.%d+%.%d+\n$"))
55+
end)
56+
57+
it("should reject invalid options", function()
58+
local code, output = run("./lsblk.lua -Q")
59+
60+
expect.equal(code, 2)
61+
expect.truthy(output:find("invalid option"))
62+
end)
63+
64+
it("should reject positional arguments", function()
65+
local code, output = run("./lsblk.lua /dev/ada0")
66+
67+
expect.equal(code, 2)
68+
expect.truthy(output:find("arguments"))
69+
end)
70+
end)
71+
72+
describe("output formats", function()
73+
it("should show default output", function()
74+
local code, output = run("./lsblk.lua")
75+
76+
expect.equal(code, 0)
77+
expect.truthy(
78+
output:find("NAME +MAJ:MIN +SIZE +TYPE +FSTYPE +MOUNTPOINTS\n")
79+
)
80+
expect.truthy(output:find("disk"))
81+
expect.truthy(output:find("part"))
82+
end)
83+
84+
it("should show byte output", function()
85+
local code, output = run("./lsblk.lua -b")
86+
87+
expect.equal(code, 0)
88+
expect.truthy(output:find(" %d+ disk"))
89+
end)
90+
91+
it("should show geom output", function()
92+
local code, output = run("./lsblk.lua -g")
93+
94+
expect.equal(code, 0)
95+
expect.truthy(output:find("NAME"))
96+
expect.truthy(output:find("disk"))
97+
-- Should filter out ZFS.
98+
expect.falsy(output:find("zfs"))
99+
end)
100+
101+
it("should show ZFS output", function()
102+
local code, output = run("./lsblk.lua -z")
103+
104+
-- Only compare the code and the header
105+
-- because the test machine may not have ZFS pools.
106+
expect.equal(code, 0)
107+
expect.truthy(output:find("NAME"))
108+
end)
109+
end)
110+
end)
111+
112+
lester.report()
113+
lester.exit()

vendor/lester/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021-2023 Eduardo Bart (https://github.com/edubart)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

vendor/lester/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Lester
2+
3+
Minimal Lua test framework.
4+
5+
Lester is a minimal unit testing framework for Lua with a focus on being simple to use.
6+
7+
It is highly inspired by
8+
[Busted](http://olivinelabs.com/busted/) and [Lust](https://github.com/bjornbytes/lust).
9+
It was mainly created to replace Busted without dependencies in the
10+
[Nelua](https://github.com/edubart/nelua-lang) compiler.
11+
12+
[![asciicast](https://asciinema.org/a/GihfI07vCt9Q7cvL6xCtnoNl1.svg)](https://asciinema.org/a/GihfI07vCt9Q7cvL6xCtnoNl1)
13+
14+
## Features
15+
16+
* Minimal, just one file.
17+
* Self contained, no external dependencies.
18+
* Simple and hackable when needed.
19+
* Use `describe` and `it` blocks to describe tests.
20+
* Supports `before` and `after` handlers.
21+
* Supports marking tests as disabled to be skipped.
22+
* Colored output.
23+
* Configurable via the script or with environment variables.
24+
* Quiet mode, to use in live development.
25+
* Optionally filter tests by name.
26+
* Show traceback on errors.
27+
* Show time to complete tests.
28+
* Works with Lua 5.1+.
29+
* Efficient.
30+
31+
## Usage
32+
33+
Copy `lester.lua` file to a project and require it,
34+
which returns a table that includes all of the functionality:
35+
36+
```lua
37+
local lester = require 'lester'
38+
local describe, it, expect = lester.describe, lester.it, lester.expect
39+
40+
-- Customize lester configuration.
41+
lester.show_traceback = false
42+
43+
describe('my project', function()
44+
lester.before(function()
45+
-- This function is run before every test.
46+
end)
47+
48+
describe('module1', function() -- Describe blocks can be nested.
49+
it('feature1', function()
50+
expect.equal('something', 'something') -- Pass.
51+
end)
52+
53+
it('feature2', function()
54+
expect.truthy(false) -- Fail.
55+
end)
56+
57+
local feature3_test_enabled = false
58+
it('feature3', function() -- This test will be skipped.
59+
expect.truthy(false) -- Fail.
60+
end, feature3_test_enabled)
61+
end)
62+
end)
63+
64+
lester.report() -- Print overall statistic of the tests run.
65+
lester.exit() -- Exit with success if all tests passed.
66+
```
67+
68+
## Customizing output with environment variables
69+
70+
To customize the output of lester externally,
71+
you can set the following environment variables before running a test suite:
72+
73+
* `LESTER_QUIET="true"`, omit print of passed tests.
74+
* `LESTER_COLORED="false"`, disable colored output.
75+
* `LESTER_SHOW_TRACEBACK="false"`, disable traceback on test failures.
76+
* `LESTER_SHOW_ERROR="false"`, omit print of error description of failed tests.
77+
* `LESTER_STOP_ON_FAIL="true"`, stop on first test failure.
78+
* `LESTER_UTF8TERM="false"`, disable printing of UTF-8 characters.
79+
* `LESTER_FILTER="some text"`, filter the tests that should be run.
80+
81+
Note that these configurations can be changed via script too, check the documentation.
82+
83+
## Customizing output with command line arguments
84+
85+
You can also customize output using command line arguments
86+
if `lester.parse_args()` is called at startup.
87+
88+
The following command line arguments are available:
89+
90+
* `--quiet`, omit print of passed tests.
91+
* `--no-quiet`, show print of passed tests.
92+
* `--no-color`, disable colored output.
93+
* `--no-show-traceback`, disable traceback on test failures.
94+
* `--no-show-error`, omit print of error description of failed tests.
95+
* `--stop-on-fail`, stop on first test failure.
96+
* `--no-utf8term`, disable printing of UTF-8 characters.
97+
* `--filter="some text"`, filter the tests that should be run.
98+
99+
## Documentation
100+
101+
The full API reference and documentation can be viewed in the
102+
[documentation website](https://edubart.github.io/lester/).
103+
104+
## Install
105+
106+
You can use luarocks to install quickly:
107+
108+
```bash
109+
luarocks install lester
110+
```
111+
112+
Or just copy the `lester.lua` file, the library is self contained in this single file with no dependencies.
113+
114+
## Tests
115+
116+
To check if everything is working as expected under your machine run `lua tests.lua` or `make test`.
117+
118+
## License
119+
120+
MIT, see LICENSE for details.

0 commit comments

Comments
 (0)