diff --git a/doc/build-helpers/testers.chapter.md b/doc/build-helpers/testers.chapter.md index e4e8ddbb2c17..49407c6e9b8e 100644 --- a/doc/build-helpers/testers.chapter.md +++ b/doc/build-helpers/testers.chapter.md @@ -421,6 +421,11 @@ Check that two paths have the same contents. : A message that is printed last if the file system object contents at the two paths don't match exactly. +`checkMetadata` (boolean) + +: Whether to fail on metadata differences, such as permissions or ownership. + Defaults to `true`. + :::{.example #ex-testEqualContents-toyexample} # Check that two paths have the same contents diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index c35e03cada76..ac31c22ab4a2 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -57,6 +57,7 @@ actual, expected, postFailureMessage ? null, + checkMetadata ? true, }: runCommand "equal-contents-${lib.strings.toLower assertion}" { @@ -66,12 +67,13 @@ expected postFailureMessage ; + excludeMetadata = if checkMetadata then "no" else "yes"; nativeBuildInputs = [ diffoscopeMinimal ]; } '' echo "Checking:" printf '%s\n' "$assertion" - if ! diffoscope --no-progress --text-color=always --exclude-directory-metadata=no -- "$actual" "$expected" + if ! diffoscope --no-progress --text-color=always --exclude-directory-metadata="$excludeMetadata" -- "$actual" "$expected" then echo echo 'Contents must be equal, but were not!' diff --git a/pkgs/development/python-modules/keymap-drawer/default.nix b/pkgs/development/python-modules/keymap-drawer/default.nix index fb6083a90802..fffc10aa8252 100644 --- a/pkgs/development/python-modules/keymap-drawer/default.nix +++ b/pkgs/development/python-modules/keymap-drawer/default.nix @@ -2,9 +2,11 @@ lib, buildPythonPackage, + callPackages, fetchFromGitHub, pythonOlder, + keymap-drawer, nix-update-script, pcpp, platformdirs, @@ -59,6 +61,12 @@ buildPythonPackage { versionCheckProgram = "${placeholder "out"}/bin/keymap"; versionCheckProgramArg = "--version"; + passthru.tests = callPackages ./tests { + # Explicitly pass the correctly scoped package. + # The top-level package will still resolve to itself, because the way + # `toPythonApplication` interacts with scopes is weird. + inherit keymap-drawer; + }; passthru.updateScript = nix-update-script { }; meta = { diff --git a/pkgs/development/python-modules/keymap-drawer/tests/default.nix b/pkgs/development/python-modules/keymap-drawer/tests/default.nix new file mode 100644 index 000000000000..7c94185c41f5 --- /dev/null +++ b/pkgs/development/python-modules/keymap-drawer/tests/default.nix @@ -0,0 +1,89 @@ +{ + lib, + fetchFromGitHub, + runCommand, + stdenv, + testers, + + keymap-drawer, + yamllint, +}: +let + runKeymapDrawer = + name: + runCommand "keymap-drawer-${name}" { + nativeBuildInputs = [ keymap-drawer ]; + }; + + MattSturgeon-example = fetchFromGitHub { + owner = "MattSturgeon"; + repo = "glove80-config"; + rev = "d55267dd26593037256b35a5d6ebba0f75541da5"; + hash = "sha256-MV6cNpgHBuaGvpu2aR1aBNMpwPnDqOSbGf+2ykxocP4="; + nonConeMode = true; + sparseCheckout = [ + "config" + "img" + ]; + }; + + # MattSturgeon's example requires MDI icons + mdi = fetchFromGitHub { + owner = "Templarian"; + repo = "MaterialDesign-SVG"; + tag = "v7.4.47"; + hash = "sha256-NoSSRT1ID38MT70IZ+7h/gMVCNsjNs3A2RX6ePGwuQ0="; + }; +in +{ + dump-config = runKeymapDrawer "dump-config" '' + keymap dump-config --output "$out" + + if [ ! -s "$out" ]; then + >&2 echo 'Expected `dump-config` to have content.' + exit 1 + fi + + ${lib.getExe yamllint} --strict --config-data relaxed "$out" + ''; + + parse-zmk = testers.testEqualContents { + assertion = "keymap parse --zmk-keymap produces expected YAML"; + expected = "${MattSturgeon-example}/img/glove80.yaml"; + actual = runKeymapDrawer "parse" '' + keymap \ + --config ${MattSturgeon-example}/config/keymap_drawer.yaml \ + parse --zmk-keymap ${MattSturgeon-example}/config/glove80.keymap \ + --output "$out" + ''; + checkMetadata = stdenv.buildPlatform.isLinux; + }; + + draw = testers.testEqualContents { + assertion = "keymap draw produces expected SVG"; + expected = "${MattSturgeon-example}/img/glove80.svg"; + actual = runKeymapDrawer "draw" '' + ${lib.optionalString stdenv.buildPlatform.isLinux '' + export XDG_CACHE_HOME="$PWD/cache" + glyphs="$XDG_CACHE_HOME/keymap-drawer/glyphs" + ''} + ${lib.optionalString stdenv.buildPlatform.isDarwin '' + export HOME="$PWD/home" + glyphs="$HOME/Library/Caches/keymap-drawer/glyphs" + ''} + mkdir -p "$glyphs" + + # Unpack MDI icons into the cache + for file in ${mdi}/svg/* + do + ln -s "$file" "$glyphs/mdi:$(basename "$file")" + done + + keymap \ + --config ${MattSturgeon-example}/config/keymap_drawer.yaml \ + draw ${MattSturgeon-example}/img/glove80.yaml \ + --output "$out" + ''; + checkMetadata = stdenv.buildPlatform.isLinux; + }; +}