From 90817368ab928b77a4b4c1ee1d94d8ff6a4029ab Mon Sep 17 00:00:00 2001 From: Vasiliy Stelmachenok Date: Mon, 17 Jun 2024 13:03:00 +0300 Subject: [PATCH 1/6] Move test profiles to a separate directory --- src/misc.rs | 2 +- src/profile.rs | 6 +++--- .../profiles/graphic_drivers-invalid-profiles-test.toml | 0 .../profiles/graphic_drivers-profiles-test.toml | 0 nvidia-test-ids.ids => tests/profiles/nvidia-test-ids.ids | 0 .../profiles/profile-raw-escaped-strings-test.toml | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename graphic_drivers-invalid-profiles-test.toml => tests/profiles/graphic_drivers-invalid-profiles-test.toml (100%) rename graphic_drivers-profiles-test.toml => tests/profiles/graphic_drivers-profiles-test.toml (100%) rename nvidia-test-ids.ids => tests/profiles/nvidia-test-ids.ids (100%) rename profile-raw-escaped-strings-test.toml => tests/profiles/profile-raw-escaped-strings-test.toml (100%) diff --git a/src/misc.rs b/src/misc.rs index ef3f227..aa5afd7 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -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()); diff --git a/src/profile.rs b/src/profile.rs index 03d23f4..7b5eda0 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -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()); @@ -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(); @@ -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(); diff --git a/graphic_drivers-invalid-profiles-test.toml b/tests/profiles/graphic_drivers-invalid-profiles-test.toml similarity index 100% rename from graphic_drivers-invalid-profiles-test.toml rename to tests/profiles/graphic_drivers-invalid-profiles-test.toml diff --git a/graphic_drivers-profiles-test.toml b/tests/profiles/graphic_drivers-profiles-test.toml similarity index 100% rename from graphic_drivers-profiles-test.toml rename to tests/profiles/graphic_drivers-profiles-test.toml diff --git a/nvidia-test-ids.ids b/tests/profiles/nvidia-test-ids.ids similarity index 100% rename from nvidia-test-ids.ids rename to tests/profiles/nvidia-test-ids.ids diff --git a/profile-raw-escaped-strings-test.toml b/tests/profiles/profile-raw-escaped-strings-test.toml similarity index 100% rename from profile-raw-escaped-strings-test.toml rename to tests/profiles/profile-raw-escaped-strings-test.toml From e09940378408d73daceb59c881335549bdac0ead Mon Sep 17 00:00:00 2001 From: Vasiliy Stelmachenok Date: Mon, 17 Jun 2024 16:32:19 +0300 Subject: [PATCH 2/6] Add tests for scripts/chwd --- scripts/chwd | 22 +++-- tests/chwd_spec.lua | 98 +++++++++++++++++++ ...graphic_drivers-invalid-profiles-test.toml | 3 + .../graphic_drivers-profiles-test.toml | 5 - 4 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 tests/chwd_spec.lua diff --git a/scripts/chwd b/scripts/chwd index f858842..e17a6eb 100755 --- a/scripts/chwd +++ b/scripts/chwd @@ -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 @@ -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 @@ -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"]) @@ -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 diff --git a/tests/chwd_spec.lua b/tests/chwd_spec.lua new file mode 100644 index 0000000..f577811 --- /dev/null +++ b/tests/chwd_spec.lua @@ -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 </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) diff --git a/tests/profiles/graphic_drivers-invalid-profiles-test.toml b/tests/profiles/graphic_drivers-invalid-profiles-test.toml index d9652d1..13f6cb3 100644 --- a/tests/profiles/graphic_drivers-invalid-profiles-test.toml +++ b/tests/profiles/graphic_drivers-invalid-profiles-test.toml @@ -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" diff --git a/tests/profiles/graphic_drivers-profiles-test.toml b/tests/profiles/graphic_drivers-profiles-test.toml index e5bf78a..b641881 100644 --- a/tests/profiles/graphic_drivers-profiles-test.toml +++ b/tests/profiles/graphic_drivers-profiles-test.toml @@ -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="" @@ -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] From 0a065fe84e7a4177ba7ca628bbecd126ef6cc9ad Mon Sep 17 00:00:00 2001 From: Vasiliy Stelmachenok Date: Mon, 17 Jun 2024 16:40:51 +0300 Subject: [PATCH 3/6] tests/chwd: Add running CI tests --- .github/workflows/lua.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/lua.yml diff --git a/.github/workflows/lua.yml b/.github/workflows/lua.yml new file mode 100644 index 0000000..c604799 --- /dev/null +++ b/.github/workflows/lua.yml @@ -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/busted@v0 + with: + args: --no-keep-going From c8250e467cd8ee5fc97ccaeb24620ced74617667 Mon Sep 17 00:00:00 2001 From: Vasiliy Stelmachenok Date: Mon, 17 Jun 2024 16:46:16 +0300 Subject: [PATCH 4/6] tests/chwd: Bump new action version --- .github/workflows/lua.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lua.yml b/.github/workflows/lua.yml index c604799..f26c761 100644 --- a/.github/workflows/lua.yml +++ b/.github/workflows/lua.yml @@ -19,6 +19,6 @@ jobs: - name: Checkout uses: actions/checkout@v3 - name: Run Busted - uses: lunarmodules/busted@v0 + uses: lunarmodules/busted@v2.2.0 with: args: --no-keep-going From 1034507195e61add69fa489270d9ac70d8d1defc Mon Sep 17 00:00:00 2001 From: Vasiliy Stelmachenok Date: Mon, 17 Jun 2024 16:49:04 +0300 Subject: [PATCH 5/6] tests/chwd: Add explicit path indication --- .github/workflows/lua.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lua.yml b/.github/workflows/lua.yml index f26c761..b9268a6 100644 --- a/.github/workflows/lua.yml +++ b/.github/workflows/lua.yml @@ -21,4 +21,4 @@ jobs: - name: Run Busted uses: lunarmodules/busted@v2.2.0 with: - args: --no-keep-going + args: --no-keep-going . From 2473634bab0af29ee56dfdaee56d6512414e406b Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Mon, 17 Jun 2024 18:22:26 +0400 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=9A=A7=20tests:=20fix=20path=20to=20d?= =?UTF-8?q?evices=20ids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/profiles/graphic_drivers-profiles-test.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/profiles/graphic_drivers-profiles-test.toml b/tests/profiles/graphic_drivers-profiles-test.toml index b641881..4af07f4 100644 --- a/tests/profiles/graphic_drivers-profiles-test.toml +++ b/tests/profiles/graphic_drivers-profiles-test.toml @@ -62,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+'