Skip to content

Commit

Permalink
engine: fix exclude path bug (#21)
Browse files Browse the repository at this point in the history
* engine: fix exclude path bug

If there was more than one exclude path, then every path would always be
included due to an uncaught logic bug.

* paths: moved CollectPathsToFormat to new package

I'm trying to break apart the `util.go` file into other relevant
packages since I hate util dumping grounds.
  • Loading branch information
braydonk committed Aug 23, 2022
1 parent 80328a4 commit 90fa1be
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 53 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Go test

on:
Expand Down
7 changes: 4 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/google/yamlfmt"
"github.com/google/yamlfmt/internal/diff"
"github.com/google/yamlfmt/internal/paths"
)

type Engine struct {
Expand All @@ -29,7 +30,7 @@ type Engine struct {
}

func (e *Engine) FormatAllFiles() error {
paths, err := CollectPathsToFormat(e.Include, e.Exclude)
paths, err := paths.CollectPathsToFormat(e.Include, e.Exclude)
if err != nil {
return err
}
Expand Down Expand Up @@ -62,7 +63,7 @@ func (e *Engine) FormatFile(path string) error {
}

func (e *Engine) LintAllFiles() error {
paths, err := CollectPathsToFormat(e.Include, e.Exclude)
paths, err := paths.CollectPathsToFormat(e.Include, e.Exclude)
if err != nil {
return err
}
Expand Down Expand Up @@ -98,7 +99,7 @@ func (e *Engine) LintFile(path string) error {
}

func (e *Engine) DryRunAllFiles() (string, error) {
paths, err := CollectPathsToFormat(e.Include, e.Exclude)
paths, err := paths.CollectPathsToFormat(e.Include, e.Exclude)
if err != nil {
return "", err
}
Expand Down
51 changes: 1 addition & 50 deletions engine/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,7 @@

package engine

import (
"fmt"
"strings"

"github.com/bmatcuk/doublestar/v4"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func CollectPathsToFormat(include, exclude []string) ([]string, error) {
includedPaths := []string{}
for _, pattern := range include {
globMatches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return nil, err
}
includedPaths = append(includedPaths, globMatches...)
}

pathsToFormatSet := map[string]struct{}{}
for _, path := range includedPaths {
if len(exclude) == 0 {
pathsToFormatSet[path] = struct{}{}
continue
}
for _, pattern := range exclude {
match, err := doublestar.Match(pattern, path)
if err != nil {
return nil, err
}
if !match {
pathsToFormatSet[path] = struct{}{}
}
}
}
pathsToFormat := []string{}
for path := range pathsToFormatSet {
pathsToFormat = append(pathsToFormat, path)
}
return pathsToFormat, nil
}

func MultilineStringDiff(a, b string) string {
return cmp.Diff(
a, b,
cmpopts.AcyclicTransformer("multiline", func(s string) []string {
return strings.Split(s, "\n")
}),
)
}
import "fmt"

type DryRunDiffs struct {
diffs map[string]string
Expand Down
56 changes: 56 additions & 0 deletions internal/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package paths

import (
"github.com/bmatcuk/doublestar/v4"
)

func CollectPathsToFormat(include, exclude []string) ([]string, error) {
includedPaths := []string{}
for _, pattern := range include {
globMatches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return nil, err
}
includedPaths = append(includedPaths, globMatches...)
}

pathsToFormatSet := map[string]struct{}{}
for _, path := range includedPaths {
if len(exclude) == 0 {
pathsToFormatSet[path] = struct{}{}
continue
}
excluded := false
for _, pattern := range exclude {
match, err := doublestar.Match(pattern, path)
if err != nil {
return nil, err
}
if match {
excluded = true
}
}
if !excluded {
pathsToFormatSet[path] = struct{}{}
}
}
pathsToFormat := []string{}
for path := range pathsToFormatSet {
pathsToFormat = append(pathsToFormat, path)
}
return pathsToFormat, nil
}

0 comments on commit 90fa1be

Please sign in to comment.