diff --git a/pkgs/by-name/tr/treefmt/check-wrapper.nix b/pkgs/by-name/tr/treefmt/check-wrapper.nix new file mode 100644 index 000000000000..a7f16e0bdf90 --- /dev/null +++ b/pkgs/by-name/tr/treefmt/check-wrapper.nix @@ -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" + '' diff --git a/pkgs/by-name/tr/treefmt/lib.nix b/pkgs/by-name/tr/treefmt/lib.nix index a38fb5354bca..d7515833ed6c 100644 --- a/pkgs/by-name/tr/treefmt/lib.nix +++ b/pkgs/by-name/tr/treefmt/lib.nix @@ -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 ``` diff --git a/pkgs/by-name/tr/treefmt/modules/wrapper.nix b/pkgs/by-name/tr/treefmt/modules/wrapper.nix index d736db5b5d77..b394912ce0b1 100644 --- a/pkgs/by-name/tr/treefmt/modules/wrapper.nix +++ b/pkgs/by-name/tr/treefmt/modules/wrapper.nix @@ -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" ''; - }; + }); } diff --git a/pkgs/by-name/tr/treefmt/tests.nix b/pkgs/by-name/tr/treefmt/tests.nix index 57bcfda45cd3..80b3a9e8712e 100644 --- a/pkgs/by-name/tr/treefmt/tests.nix +++ b/pkgs/by-name/tr/treefmt/tests.nix @@ -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" < "$out/file.nix" <