|
| 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() |
0 commit comments