Skip to content

Commit

Permalink
fix: unmatched reporting global excludes
Browse files Browse the repository at this point in the history
Improved the test which should have caught this regression and fixed the logic in composite formatter.

Closes #468

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Nov 6, 2024
1 parent 10a4616 commit db7b305
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
24 changes: 19 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd_test

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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"
Expand All @@ -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)
}
}

Expand All @@ -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))
Expand Down
14 changes: 10 additions & 4 deletions format/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,15 +54,21 @@ func (c *CompositeFormatter) match(file *walk.File) []*Formatter {
}
}

return matches
return false, matches
}

// Apply applies the configured formatters to the given files.
func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) error {
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 {
Expand Down
6 changes: 5 additions & 1 deletion nix/packages/docs.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{ pkgs, perSystem, ... }:
{
pkgs,
perSystem,
...
}:
pkgs.stdenvNoCC.mkDerivation {
name = "docs";

Expand Down

0 comments on commit db7b305

Please sign in to comment.