From db7b30547cc3ceb2b9cdd9c7a3988dfe7a1f6db1 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Wed, 6 Nov 2024 08:38:00 +0000 Subject: [PATCH] fix: unmatched reporting global excludes Improved the test which should have caught this regression and fixed the logic in composite formatter. Closes #468 Signed-off-by: Brian McGee --- cmd/root_test.go | 24 +++++++++++++++++++----- format/composite.go | 14 ++++++++++---- nix/packages/docs.nix | 6 +++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 13c12de2..db5c7cf9 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -2,6 +2,7 @@ package cmd_test import ( "bufio" + "bytes" "fmt" "io" "os" @@ -32,12 +33,13 @@ func TestOnUnmatched(t *testing.T) { test.ChangeWorkDir(t, tempDir) - paths := []string{ + expectedPaths := []string{ "go/go.mod", "haskell/haskell.cabal", + "haskell-frontend/haskell-frontend.cabal", "html/scripts/.gitkeep", "python/requirements.txt", - // these should not be reported as they're in the global excludes + // these should not be reported, they are in the global excludes // - "nixpkgs.toml" // - "touch.toml" // - "treefmt.toml" @@ -51,10 +53,22 @@ func TestOnUnmatched(t *testing.T) { checkOutput := func(level log.Level) func([]byte) { logPrefix := strings.ToUpper(level.String())[:4] + regex := regexp.MustCompile(fmt.Sprintf(`^%s no formatter for path: (.*)$`, logPrefix)) + return func(out []byte) { - for _, p := range paths { - as.Contains(string(out), fmt.Sprintf("%s no formatter for path: %s", logPrefix, p)) + var paths []string + + scanner := bufio.NewScanner(bytes.NewReader(out)) + for scanner.Scan() { + matches := regex.FindStringSubmatch(scanner.Text()) + if len(matches) != 2 { + continue + } + + paths = append(paths, matches[1]) } + + as.Equal(expectedPaths, paths) } } @@ -66,7 +80,7 @@ func TestOnUnmatched(t *testing.T) { // should exit with error when using fatal t.Run("fatal", func(t *testing.T) { errorFn := func(err error) { - as.ErrorContains(err, fmt.Sprintf("no formatter for path: %s", paths[0])) + as.ErrorContains(err, fmt.Sprintf("no formatter for path: %s", expectedPaths[0])) } treefmt(t, withArgs("--on-unmatched", "fatal"), withError(errorFn)) diff --git a/format/composite.go b/format/composite.go index 2bfe209d..7a8dd34a 100644 --- a/format/composite.go +++ b/format/composite.go @@ -36,12 +36,12 @@ type CompositeFormatter struct { } // match filters the file against global excludes and returns a list of formatters that want to process the file. -func (c *CompositeFormatter) match(file *walk.File) []*Formatter { +func (c *CompositeFormatter) match(file *walk.File) (bool, []*Formatter) { // first check if this file has been globally excluded if pathMatches(file.RelPath, c.globalExcludes) { log.Debugf("path matched global excludes: %s", file.RelPath) - return nil + return true, nil } // a list of formatters that match this file @@ -54,7 +54,7 @@ func (c *CompositeFormatter) match(file *walk.File) []*Formatter { } } - return matches + return false, matches } // Apply applies the configured formatters to the given files. @@ -62,7 +62,13 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro var toRelease []*walk.File for _, file := range files { - matches := c.match(file) // match the file against the formatters + // match the file against the formatters + globalExclude, matches := c.match(file) + + // if the file is globally excluded, we do not emit a warning + if globalExclude { + continue + } // check if there were no matches if len(matches) == 0 { diff --git a/nix/packages/docs.nix b/nix/packages/docs.nix index 441b171f..4071574c 100644 --- a/nix/packages/docs.nix +++ b/nix/packages/docs.nix @@ -1,4 +1,8 @@ -{ pkgs, perSystem, ... }: +{ + pkgs, + perSystem, + ... +}: pkgs.stdenvNoCC.mkDerivation { name = "docs";