Skip to content

Commit eb930f9

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

File tree

7 files changed

+2818
-11
lines changed

7 files changed

+2818
-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.

0 commit comments

Comments
 (0)