Files
nixpkgs/pkgs/by-name/tr/treefmt/tests.nix
Matt Sturgeon 134bfa753c treefmt.withConfig: add check function
Inspired by treefmt-nix's `config.build.check` option, add a similar
check function to `treefmt.withConfig`'s wrapper.

`check` is a function of `Path → Derivation`, which returns a derivation
that builds ok when the path is already formatted, or fails to build if
the wrapped-treefmt would change the path's formatting.

Add the `check` function, tests for it, and initial docs.

See treefmt-nix: https://github.com/numtide/treefmt-nix/blob/db947814/module-options.nix#L301-L349
2026-06-09 07:37:38 +01:00

149 lines
3.3 KiB
Nix

{
lib,
runCommand,
runCommandLocal,
testers,
treefmt,
nixfmt,
}:
let
inherit (treefmt) buildConfig withConfig;
testEqualContents =
args:
testers.testEqualContents (
args
// lib.optionalAttrs (builtins.isString args.expected) {
expected = builtins.toFile "expected" args.expected;
}
);
nixfmtExampleConfig = {
on-unmatched = "info";
tree-root-file = ".git/index";
formatter.nixfmt = {
command = "nixfmt";
includes = [ "*.nix" ];
};
};
nixfmtExamplePackage = withConfig {
settings = nixfmtExampleConfig;
runtimeInputs = [ nixfmt ];
};
wellFormattedTree = runCommandLocal "well-formatted-project" { } ''
mkdir "$out"
cat > "$out/file.nix" <<EOF
{
foo = "bar";
attrs = { };
list = [ ];
}
EOF
'';
unformattedTree = runCommandLocal "unformatted-project" { } ''
mkdir "$out"
cat > "$out/file.nix" <<EOF
{
foo="bar";
attrs={};
list=[];
}
EOF
'';
in
{
buildConfigEmpty = testEqualContents {
assertion = "`buildConfig { }` builds an empty config file";
actual = buildConfig { };
expected = "";
};
buildConfigExample = testEqualContents {
assertion = "`buildConfig` builds the example config";
actual = buildConfig nixfmtExampleConfig;
expected = ''
on-unmatched = "info"
tree-root-file = ".git/index"
[formatter.nixfmt]
command = "nixfmt"
includes = ["*.nix"]
'';
};
buildConfigModules = testEqualContents {
assertion = "`buildConfig` evaluates modules to build a config";
actual = buildConfig [
nixfmtExampleConfig
{ tree-root-file = lib.mkForce "overridden"; }
];
expected = ''
on-unmatched = "info"
tree-root-file = "overridden"
[formatter.nixfmt]
command = "nixfmt"
includes = ["*.nix"]
'';
};
nixfmtExampleCheckPasses = nixfmtExamplePackage.check wellFormattedTree;
nixfmtExampleCheckFails = testers.testBuildFailure' {
drv = nixfmtExamplePackage.check unformattedTree;
expectedBuilderExitCode = 1;
expectedBuilderLogEntries = [
"diff --git a/file.nix b/file.nix"
"- foo=\"bar\";"
"+ foo = \"bar\";"
"- attrs={};"
"+ attrs = { };"
"- list=[];"
"+ list = [ ];"
];
};
runNixfmtExample =
runCommand "run-nixfmt-example"
{
nativeBuildInputs = [ nixfmtExamplePackage ];
input = ''
{
foo="bar";
attrs={};
list=[];
}
'';
expected = ''
{
foo = "bar";
attrs = { };
list = [ ];
}
'';
__structuredAttrs = true;
}
''
export XDG_CACHE_HOME=$(mktemp -d)
# The example config assumes the tree root has a .git/index file
mkdir .git && touch .git/index
# Create the input file, then format it using the wrapped treefmt
printf "%s" "$input" > input.nix
treefmt
# Assert that input.nix now matches expected
if diff -u <(printf "%s" "$expected") input.nix; then
touch $out
else
echo
echo "treefmt did not format input.nix as expected"
exit 1
fi
'';
}