[Backport release-26.05] treefmt.withConfig: add check function (#529809)

This commit is contained in:
Matt Sturgeon 2026-06-09 08:05:34 +00:00 committed by GitHub
commit ae22029dd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 2 deletions

View file

@ -0,0 +1,50 @@
{
lib,
gitMinimal,
runCommandLocal,
stdenv,
wrapper,
}:
/**
Test that the given project tree is formatted with the treefmt config.
Input argument is the path to the project tree.
*/
project:
runCommandLocal "${lib.getName wrapper}-check"
{
__structuredAttrs = true;
strictDeps = true;
nativeBuildInputs = [
gitMinimal
wrapper
];
inherit project;
env = {
LANG = if stdenv.buildPlatform.isDarwin then "en_US.UTF-8" else "C.UTF-8";
LC_ALL = if stdenv.buildPlatform.isDarwin then "en_US.UTF-8" else "C.UTF-8";
};
meta.description = "Check that the project tree is formatted";
}
''
# Copy project files into the build-dir
cp -r "$project" project
chmod -R a+w project
cd project
# Setup a git repo
git init --initial-branch main
git config user.name nixbld
git config user.email nixbld@example.com
git add .
git commit -m init --quiet
# Run treefmt
treefmt --version
treefmt --no-cache
# Ensure nothing changed
git status
git --no-pager diff --exit-code
touch "$out"
''

View file

@ -40,6 +40,20 @@
/**
Wrap treefmt, configured using structured settings.
# Check
The resulting package has a `check` attribute of type `Path -> Derivation`.
The derivation returned will only build if the path supplied is already formatted correctly.
```
{ pkgs }:
let
myTreefmt = pkgs.treefmt.withConfig ./treefmt-config.nix;
project = ../.;
in
myTreefmt.check project
```
# Type
```

View file

@ -15,7 +15,7 @@
internal = true;
};
config.result = pkgs.symlinkJoin {
config.result = pkgs.symlinkJoin (finalAttrs: {
pname = config.name;
inherit (config.package) meta version;
nativeBuildInputs = [ pkgs.makeBinaryWrapper ];
@ -27,11 +27,14 @@
passthru = {
inherit (config) runtimeInputs;
inherit config options;
check = pkgs.callPackage ../check-wrapper.nix {
wrapper = finalAttrs.finalPackage;
};
};
postBuild = ''
wrapProgram "$out/bin/treefmt" \
--prefix PATH : "$binPath" \
--add-flags "--config-file $configFile"
'';
};
});
}

View file

@ -1,6 +1,7 @@
{
lib,
runCommand,
runCommandLocal,
testers,
treefmt,
nixfmt,
@ -31,6 +32,28 @@ let
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 {
@ -68,6 +91,22 @@ in
'';
};
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"
{