From 8005ad1ccc46af07367517628ed32a9a8f11f58c Mon Sep 17 00:00:00 2001 From: Snorre Date: Thu, 3 Apr 2025 12:50:54 +0200 Subject: [PATCH] added tests --- flake.nix | 24 ++++++++++++++-- lib/default.nix | 56 ++++++++++++++++++------------------- modules/default.nix | 5 +++- modules/options/default.nix | 6 ++-- tests/minimal.nix | 26 +++++++++++++++++ 5 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 tests/minimal.nix diff --git a/flake.nix b/flake.nix index 405eaf2..54e7f4d 100644 --- a/flake.nix +++ b/flake.nix @@ -9,11 +9,28 @@ ... }: let forAllSystems = nixpkgs.lib.genAttrs [ - "aarch64-linux" "x86_64-linux" "x86_64-darwin" + "aarch64-linux" "aarch64-darwin" ]; + + # Create a test for every file in `tests/` + tests = system: + builtins.listToAttrs (builtins.map + (x: let + test = import (./tests + "/${x}") {pkgs = import nixpkgs {inherit system;};}; + in { + name = test.name; + value = test.test; + }) + ( + builtins.filter (x: x != null) ((nixpkgs.lib.attrsets.mapAttrsToList (name: value: + if value == "regular" + then name + else null)) + (builtins.readDir ./tests)) + )); in { formatter = forAllSystems ( system: let @@ -25,6 +42,9 @@ declarative-jellyfin = import ./modules; default = declarative-jellyfin; }; - nixosModule = self.nixosModules.default; # compatiblilty + + # Run all tests for all systems + hydraJobs = forAllSystems tests; + checks = forAllSystems tests; }; } diff --git a/lib/default.nix b/lib/default.nix index 45563de..0f67cb0 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,11 +1,11 @@ -{nixpkgs, ...}: -nixpkgs.lib.extend ( - final: prev: { - toXMLGeneric = let +{ nixpkgs, ... }: +{ + toXMLGeneric = + let toXMLRecursive = toXmlRecursive' - "\n" - 0; + "\n" + 0; indent = depth: ( if (depth <= 0) @@ -13,33 +13,33 @@ nixpkgs.lib.extend ( else (" " + (indent (depth - 1))) ); - toXmlRecursive' = str: depth: xml: let - # depth = if (builtins.isInt depth) then depth else (throw "DEPTH ISNT AN INT??? WTFFF"); - parseTag = str: depth: xml: (builtins.concatStringsSep "" [ - str - "${indent depth}<${xml.name}${ + toXmlRecursive' = str: depth: xml: + let + parseTag = str: depth: xml: (builtins.concatStringsSep "" [ + str + "${indent depth}<${xml.name}${ if (builtins.hasAttr "content" xml) then ">" else " " }" - ( - if (builtins.hasAttr "properties" xml) - then - (" " - + builtins.concatStringsSep " " (nixpkgs.lib.attrsets.mapAttrsToList + ( + if (builtins.hasAttr "properties" xml) + then + (" " + + builtins.concatStringsSep " " (nixpkgs.lib.attrsets.mapAttrsToList (name: value: "${name}=\"${nixpkgs.lib.strings.escapeXML value}\"") xml.properties)) - else "" - ) + else "" + ) - ( - if builtins.hasAttr "content" xml - then ((toXmlRecursive' "\n" (depth + 1) xml.content) + "") - else "/>" - ) - ]); - in + ( + if builtins.hasAttr "content" xml + then ((toXmlRecursive' "\n" (depth + 1) xml.content) + "") + else "/>" + ) + ]); + in if (builtins.isAttrs xml) then "${parseTag str depth xml}\n${indent (depth - 1)}" else if (builtins.isList xml) @@ -50,6 +50,6 @@ nixpkgs.lib.extend ( then xml else throw "Cannot convert a ${builtins.typeOf xml} to XML"; in - toXMLRecursive; - } -) + toXMLRecursive; +} + diff --git a/modules/default.nix b/modules/default.nix index 0a9b728..97dc847 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1,3 +1,6 @@ {...}: { - imports = [./.]; + imports = [ + ./config.nix + ./options + ]; } diff --git a/modules/options/default.nix b/modules/options/default.nix index c9157d4..86ef233 100644 --- a/modules/options/default.nix +++ b/modules/options/default.nix @@ -1,10 +1,12 @@ { config, - lib, + nixpkgs, pkgs, + lib, ... }: let cfg = config.services.declarative-jellyfin; + toXml = (import ../../lib {inherit nixpkgs;}).toXMLGeneric; in with lib; { imports = [ @@ -36,7 +38,7 @@ in else (listOfStrPrepass value) ) xml)); - toXml = name: x: (toXMLGeneric { + toXml = name: x: (toXml { inherit name; properties = { "xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance"; diff --git a/tests/minimal.nix b/tests/minimal.nix new file mode 100644 index 0000000..7fb8420 --- /dev/null +++ b/tests/minimal.nix @@ -0,0 +1,26 @@ +{pkgs ? import {}, ...}: let + name = "minimal"; +in { + inherit name; + test = pkgs.nixosTest { + inherit name; + nodes = { + machine = { + config, + pkgs, + ... + }: { + imports = [ + ../modules/default.nix + ]; + + virtualisation.memorySize = 1024; + }; + }; + + testScript = '' + machine.start() + machine.wait_for_unit("multi-user.target"); + ''; + }; +}