From e2078ef31e1a02e23fe261a9a07fe419fd4a2192 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 2 Jan 2025 22:38:08 +0100 Subject: [PATCH] tests.nixosOptionsDoc: init --- nixos/lib/make-options-doc/default.nix | 2 + nixos/lib/make-options-doc/tests.nix | 59 ++++++++++++++++++++++++++ pkgs/test/default.nix | 2 + 3 files changed, 63 insertions(+) create mode 100644 nixos/lib/make-options-doc/tests.nix diff --git a/nixos/lib/make-options-doc/default.nix b/nixos/lib/make-options-doc/default.nix index 25af32773a74..ea24c4004e55 100644 --- a/nixos/lib/make-options-doc/default.nix +++ b/nixos/lib/make-options-doc/default.nix @@ -1,3 +1,5 @@ +# Tests: ./tests.nix + /** Generates documentation for [nix modules](https://nix.dev/tutorials/module-system/index.html). diff --git a/nixos/lib/make-options-doc/tests.nix b/nixos/lib/make-options-doc/tests.nix new file mode 100644 index 000000000000..795c0ff4fe00 --- /dev/null +++ b/nixos/lib/make-options-doc/tests.nix @@ -0,0 +1,59 @@ +# Run tests: nix-build -A tests.nixosOptionsDoc + +{ + lib, + nixosOptionsDoc, + runCommand, +}: +let + inherit (lib) mkOption types; + + eval = lib.evalModules { + modules = [ + { + options.foo.bar.enable = mkOption { + type = types.bool; + default = false; + description = '' + Enable the foo bar feature. + ''; + }; + } + ]; + }; + + doc = nixosOptionsDoc { + inherit (eval) options; + }; +in +{ + /** + Test that + - the `nixosOptionsDoc` function can be invoked + - integration of the module system and `nixosOptionsDoc` (limited coverage) + + The more interesting tests happen in the `nixos-render-docs` package. + */ + commonMark = + runCommand "test-nixosOptionsDoc-commonMark" + { + commonMarkDefault = doc.optionsCommonMark; + commonMarkAnchors = doc.optionsCommonMark.overrideAttrs { + extraArgs = [ + "--anchor-prefix" + "my-opt-" + "--anchor-style" + "legacy" + ]; + }; + } + '' + env | grep ^commonMark | sed -e 's/=/ = /' + ( + set -x + grep -F 'foo\.bar\.enable' $commonMarkDefault >/dev/null + grep -F '{#my-opt-foo.bar.enable}' $commonMarkAnchors >/dev/null + ) + touch $out + ''; +} diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index 5f5e1bc07ba5..78b1ad61ea8e 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -155,6 +155,8 @@ with pkgs; nixos-functions = callPackage ./nixos-functions { }; + nixosOptionsDoc = callPackage ../../nixos/lib/make-options-doc/tests.nix { }; + overriding = callPackage ./overriding.nix { }; texlive = callPackage ./texlive { };