Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for scripts/chwd #113

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/lua.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lua tests

on:
push:
paths-ignore:
- 'LICENSE'
- '*.md'
- '*.sh'
branches:
- master
pull_request:
branches:
- master

jobs:
sile:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Run Busted
uses: lunarmodules/[email protected]
with:
args: --no-keep-going .
22 changes: 15 additions & 7 deletions scripts/chwd
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ local function get_profile(profiles, name)

if #parents > 0 then
for _, parent in ipairs(parents) do
if profiles[parent].packages then
packages = profiles[parent].packages
if profiles[parent]['packages'] then
packages = profiles[parent]['packages']
end

for hook_name, hook in pairs(profiles[parent]["hooks"]) do
Expand All @@ -215,10 +215,6 @@ local function get_profile(profiles, name)
hooks = profile.hooks
end

if packages and not check_on_multilib() then
packages = packages:gsub("%s?(lib32-[A-Za-z0-9-]+)", "")
end

return packages, hooks
end

Expand Down Expand Up @@ -270,6 +266,10 @@ local function main()
die("Profile %s is not valid", profile_name)
end

if packages and not check_on_multilib() then
packages = packages:gsub("%s?(lib32-[A-Za-z0-9-]+)", "")
end

if options.install then
exec_hook(hooks["pre_install"])

Expand Down Expand Up @@ -297,4 +297,12 @@ local function main()
end
end

main()
---@diagnostic disable-next-line
if _TEST then -- luacheck: ignore
return {
get_profile = get_profile,
parse_profiles = parse_profiles
}
else
main()
end
2 changes: 1 addition & 1 deletion src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod tests {

#[test]
fn profile_find() {
let prof_path = "graphic_drivers-profiles-test.toml";
let prof_path = "tests/profiles/graphic_drivers-profiles-test.toml";
let profiles = profile::parse_profiles(prof_path).expect("failed");

assert!(misc::find_profile("nvidia-dkms", &profiles).is_some());
Expand Down
6 changes: 3 additions & 3 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ mod tests {

#[test]
fn graphics_profiles_correct() {
let prof_path = "graphic_drivers-profiles-test.toml";
let prof_path = "tests/profiles/graphic_drivers-profiles-test.toml";
let parsed_profiles = parse_profiles(prof_path);
assert!(parsed_profiles.is_ok());

Expand Down Expand Up @@ -360,7 +360,7 @@ mod tests {

#[test]
fn graphics_profiles_invalid() {
let prof_path = "graphic_drivers-invalid-profiles-test.toml";
let prof_path = "tests/profiles/graphic_drivers-invalid-profiles-test.toml";
let parsed_profiles = crate::profile::get_invalid_profiles(prof_path);
assert!(parsed_profiles.is_ok());
let parsed_profiles = parsed_profiles.unwrap();
Expand All @@ -371,7 +371,7 @@ mod tests {

#[test]
fn profile_write_test() {
let prof_path = "profile-raw-escaped-strings-test.toml";
let prof_path = "tests/profiles/profile-raw-escaped-strings-test.toml";
let parsed_profiles = parse_profiles(prof_path);
assert!(parsed_profiles.is_ok());
let parsed_profiles = parsed_profiles.unwrap();
Expand Down
98 changes: 98 additions & 0 deletions tests/chwd_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
describe("Profile parsing", function()
_G._TEST = true
package.path = 'scripts/?;' .. package.path
local chwd = require("chwd")

describe("Valid cases", function()
local profiles = chwd.parse_profiles("tests/profiles/graphic_drivers-profiles-test.toml")
local name = "nvidia-dkms"

it("Profiles are available", function()
assert.are_not.same(profiles, {})
end)

local profile = profiles[name]
it("Search for profile", function()
assert.truthy(profile)
end)

describe("Attributes", function()
local packages, hooks = chwd.get_profile(profiles, name)
it("Packages", function()
assert.are.equals(packages,
"nvidia-utils egl-wayland nvidia-settings opencl-nvidia lib32-opencl-nvidia lib32-nvidia-utils libva-nvidia-driver vulkan-icd-loader lib32-vulkan-icd-loader")
end)
it("Hooks", function()
assert.truthy(hooks)
end)
it("Post remove hook", function()
assert.are.equals(hooks['post_remove'], [[
rm -f /etc/mkinitcpio.conf.d/10-chwd.conf
mkinitcpio -P
]])
end)
it("Post install hook", function()
assert.are.equals(hooks['post_install'], [[
cat <<EOF >/etc/mkinitcpio.conf.d/10-chwd.conf
# This file is automatically generated by chwd. PLEASE DO NOT EDIT IT.
MODULES+=(nvidia nvidia_modeset nvidia_uvm nvidia_drm)
EOF
mkinitcpio -P
]])
end)
it("Conditional packages hook", function()
assert.are.equals(hooks['conditional_packages'], [[
kernels="$(pacman -Qqs "^linux-cachyos")"
modules=""

for kernel in $kernels; do
case "$kernel" in
*-headers|*-zfs);;
*-nvidia) modules+=" ${kernel}";;
*) modules+=" ${kernel}-nvidia";;
esac
done

# Fallback if there are no kernels with pre-built modules
[ -z "$modules" ] && modules="nvidia-dkms"

echo "$modules"
]])
end)
end)

local child_name = "nvidia-dkms.40xxcards"
local child_profile = profiles[child_name]
it("Search for child profile", function()
assert.truthy(child_profile)
end)

describe("Inheritance", function()
local packages, hooks = chwd.get_profile(profiles, child_name)
it("Inherit parent packages", function ()
assert.are.equals(packages,
"nvidia-utils egl-wayland nvidia-settings opencl-nvidia lib32-opencl-nvidia lib32-nvidia-utils libva-nvidia-driver vulkan-icd-loader lib32-vulkan-icd-loader")
end)
it("Inherit some parent hook", function ()
assert.are.equals(hooks['post_remove'], [[
rm -f /etc/mkinitcpio.conf.d/10-chwd.conf
mkinitcpio -P
]])
end)
end)
end)

describe("Invalid cases", function()
it("Profiles are not available", function()
assert.are.same(chwd.parse_profiles("/dev/null"), {})
end)

local profiles = chwd.parse_profiles("tests/profiles/graphic_drivers-invalid-profiles-test.toml")
it("Non-existing profile", function()
assert.is.falsy(profiles['unknown'])
end)
it("Unspecified packages", function()
assert.is.falsy(profiles['invalid'].packages)
end)
end)
end)
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ post_remove = """
mkinitcpio -P
"""
device_ids = '>/some/path/to/file/nvidia-invalid-ids.ids'

[invalid]
desc = "Invalid profile for testing only"
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[nvidia-dkms.40xxcards]
desc = 'Closed source NVIDIA drivers(40xx series) for Linux (Latest)'
priority = 9
packages = 'nvidia-utils egl-wayland nvidia-settings opencl-nvidia lib32-opencl-nvidia lib32-nvidia-utils libva-nvidia-driver vulkan-icd-loader lib32-vulkan-icd-loader'
conditional_packages = """
kernels="$(pacman -Qqs "^linux-cachyos")"
modules=""
Expand All @@ -26,10 +25,6 @@ MODULES+=(nvidia nvidia_modeset nvidia_uvm nvidia_drm)
EOF
mkinitcpio -P
"""
post_remove = """
rm -f /etc/mkinitcpio.conf.d/10-chwd.conf
mkinitcpio -P
"""
device_name_pattern = '(AD)\w+'

[nvidia-dkms]
Expand Down Expand Up @@ -67,5 +62,5 @@ post_remove = """
rm -f /etc/mkinitcpio.conf.d/10-chwd.conf
mkinitcpio -P
"""
device_ids = '>nvidia-test-ids.ids'
device_ids = '>tests/profiles/nvidia-test-ids.ids'
hwd_product_name_pattern = '(Ally)\w+'
File renamed without changes.
Loading