keymap-drawer: init tests (#452948)

This commit is contained in:
Matt Sturgeon
2025-10-19 00:26:49 +00:00
committed by GitHub
4 changed files with 105 additions and 1 deletions
+5
View File
@@ -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
+3 -1
View File
@@ -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!'
@@ -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 = {
@@ -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;
};
}