Skip to content

Commit

Permalink
Fix integration test files, add -no_global_conf
Browse files Browse the repository at this point in the history
The integration test files got accidentally formatted by some local
testing, this sets them back to what they should be. Also adds a new
flag that that disables usage of configuration file from system home.
  • Loading branch information
braydonk committed Feb 4, 2024
1 parent 7bb4348 commit d838831
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 39 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.PHONY: build
build:
go build -o dist/yamlfmt ./cmd/yamlfmt
go build -o dist/yamlfmt ./cmd/yamlfmt

.PHONY: test
test:
Expand All @@ -23,10 +23,15 @@ integrationtest_v:
$(MAKE) build
go test -v -tags=integration_test ./integrationtest/command

.PHONY: integrationtest_stdout
integrationtest_stdout:
$(MAKE) build
go test -v -tags=integration_test ./integrationtest/command -stdout

.PHONY: integrationtest_local_update
integrationtest_update:
$(MAKE) build
go test -tags=integration_test ./integrationtest/command -update
go test -tags=integration_test ./integrationtest/command -update

.PHONY: install
install:
Expand Down
15 changes: 9 additions & 6 deletions cmd/yamlfmt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ func getConfigPath() (string, error) {
return configPath, nil
}

// Third priority: in home config directory
configPath, err = getConfigPathFromConfigHome()
// In this scenario, no errors are considered a failure state,
// so we continue to the next fallback if there are no errors.
if err == nil {
return configPath, nil
if !*flagDisableGlobalConf {
// Third priority: in home config directory
configPath, err = getConfigPathFromConfigHome()
// In this scenario, no errors are considered a failure state,
// so we continue to the next fallback if there are no errors.
if err == nil {
return configPath, nil
}
}

// All else fails, no path and no error (signals to
Expand Down Expand Up @@ -183,6 +185,7 @@ func getConfigPathFromDir(dir string) (string, error) {
for filename := range configFileNames {
configPath := filepath.Join(dir, filename)
if err := validatePath(configPath); err == nil {
logger.Debug(logger.DebugCodeConfig, "Found config at %s", configPath)
return configPath, nil
}
}
Expand Down
30 changes: 15 additions & 15 deletions cmd/yamlfmt/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ var (
source yaml and formatted yaml.`)
flagDry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting
operation without performing it.`)
flagIn *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout")
flagVersion *bool = flag.Bool("version", false, "Print yamlfmt version")
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
flagGlobalConf *bool = flag.Bool("global_conf", false, globalConfFlagMessage())
flagDoublestar *bool = flag.Bool("dstar", false, "Use doublestar globs for include and exclude")
flagQuiet *bool = flag.Bool("quiet", false, "Print minimal output to stdout")
flagContinueOnError *bool = flag.Bool("continue_on_error", false, "Continue to format files that didn't fail instead of exiting with code 1.")
flagExclude = arrayFlag{}
flagFormatter = arrayFlag{}
flagExtensions = arrayFlag{}
flagDebug = arrayFlag{}
flagIn *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout")
flagVersion *bool = flag.Bool("version", false, "Print yamlfmt version")
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
flagGlobalConf *bool = flag.Bool("global_conf", false, fmt.Sprintf("Use global yamlfmt config from %s", globalConfFlagVar()))
flagDisableGlobalConf *bool = flag.Bool("no_global_conf", false, fmt.Sprintf("Disabled usage of global yamlfmt config from %s", globalConfFlagVar()))
flagDoublestar *bool = flag.Bool("dstar", false, "Use doublestar globs for include and exclude")
flagQuiet *bool = flag.Bool("quiet", false, "Print minimal output to stdout")
flagContinueOnError *bool = flag.Bool("continue_on_error", false, "Continue to format files that didn't fail instead of exiting with code 1.")
flagExclude = arrayFlag{}
flagFormatter = arrayFlag{}
flagExtensions = arrayFlag{}
flagDebug = arrayFlag{}
)

func bindArrayFlags() {
Expand Down Expand Up @@ -103,10 +104,9 @@ func isStdinArg() bool {
return arg == "-" || arg == "/dev/stdin"
}

func globalConfFlagMessage() string {
varName := "XDG_CONFIG_HOME"
func globalConfFlagVar() string {
if runtime.GOOS == "windows" {
varName = "LOCALAPPDATA"
return "LOCALAPPDATA"
}
return fmt.Sprintf("Use global yamlfmt config from %s", varName)
return "XDG_CONFIG_HOME"
}
19 changes: 10 additions & 9 deletions docs/command-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ These flags will configure the underlying behaviour of the command.

The string array flags can be a bit confusing. See the [String Array Flags](#string-array-flags) section for more information.

| Name | Flag | Type | Example | Description |
|:-----------------|:--------------|:---------|:----------------------------------------------------------|:------------|
| Config File Path | `-conf` | string | `yamlfmt -conf ./config/.yamlfmt` | Specify a path to read a [configuration file](./config-file.md) from. |
| Global Config | `-global_conf`| bool | `yamlfmt -global_conf ./config/.yamlfmt` | Force yamlfmt to use the configuration file from the system config directory. |
| Doublstar | `-dstar` | boolean | `yamlfmt -dstar "**/*.yaml"` | Enable [Doublstar](./paths.md#doublestar) path collection mode. |
| Exclude | `-exclude` | []string | `yamlfmt -exclude ./not/,these_paths.yaml` | Patterns to exclude from path collection. These add to exclude patterns specified in the [config file](./config-file.md) |
| Extensions | `-extensions` | []string | `yamlfmt -extensions yaml,yml` | Extensions to use in standard path collection. Has no effect in Doublestar mode. These add to extensions specified in the [config file](./config-file.md)
| Formatter Config | `-formatter` | []string | `yamlfmt -formatter indent=2,include_document_start=true` | Provide configuration values for the formatter. See [Formatter Configuration Options](./config-file.md#basic-formatter) for options. Each field is specified as `configkey=value`. |
| Debug Logging | `-debug` | []string | `yamlfmt -debug paths,config` | Enable debug logging. See [Debug Logging](#debug-logging) for more information. |
| Name | Flag | Type | Example | Description |
|:----------------------|:--------------|:---------|:----------------------------------------------------------|:------------|
| Config File Path | `-conf` | string | `yamlfmt -conf ./config/.yamlfmt` | Specify a path to read a [configuration file](./config-file.md) from. |
| Global Config | `-global_conf`| bool | `yamlfmt -global_conf`` | Force yamlfmt to use the configuration file from the system config directory. |
| Disable Global Config | `-global_conf`| bool | `yamlfmt -no_global_conf` | Disable looking for the configuration file from the system config directory. |
| Doublstar | `-dstar` | boolean | `yamlfmt -dstar "**/*.yaml"` | Enable [Doublstar](./paths.md#doublestar) path collection mode. |
| Exclude | `-exclude` | []string | `yamlfmt -exclude ./not/,these_paths.yaml` | Patterns to exclude from path collection. These add to exclude patterns specified in the [config file](./config-file.md) |
| Extensions | `-extensions` | []string | `yamlfmt -extensions yaml,yml` | Extensions to use in standard path collection. Has no effect in Doublestar mode. These add to extensions specified in the [config file](./config-file.md)
| Formatter Config | `-formatter` | []string | `yamlfmt -formatter indent=2,include_document_start=true` | Provide configuration values for the formatter. See [Formatter Configuration Options](./config-file.md#basic-formatter) for options. Each field is specified as `configkey=value`. |
| Debug Logging | `-debug` | []string | `yamlfmt -debug paths,config` | Enable debug logging. See [Debug Logging](#debug-logging) for more information. |

#### String Array Flags

Expand Down
6 changes: 4 additions & 2 deletions integrationtest/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import (

var (
updateFlag *bool = flag.Bool("update", false, "Whether to update the goldens.")
stdoutFlag *bool = flag.Bool("stdout", false, "Show stdout instead of diffing it.")
yamlfmtBin string
)

func init() {
func TestMain(m *testing.M) {
yamlfmtBinVar := os.Getenv("YAMLFMT_BIN")
if yamlfmtBinVar == "" {
fmt.Println("Must provide a YAMLFMT_BIN environment variable.")
os.Exit(1)
}
yamlfmtBin = yamlfmtBinVar
m.Run()
}

func TestPathArg(t *testing.T) {
Expand All @@ -42,5 +44,5 @@ func TestIncludeDocumentStart(t *testing.T) {
}

func yamlfmtWithArgs(args string) string {
return fmt.Sprintf("%s %s", yamlfmtBin, args)
return fmt.Sprintf("%s -no_global_conf %s", yamlfmtBin, args)
}
12 changes: 9 additions & 3 deletions integrationtest/command/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package command

import (
"bytes"
"fmt"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -18,9 +19,10 @@ const (
)

type TestCase struct {
Dir string
Command string
Update bool
Dir string
Command string
Update bool
ShowStdout bool
}

func (tc TestCase) Run(t *testing.T) {
Expand Down Expand Up @@ -69,6 +71,10 @@ func (tc TestCase) command(wd string, stdoutBuf *bytes.Buffer) *exec.Cmd {
}

func (tc TestCase) goldenStdout(stdoutResult []byte) error {
if !tc.ShowStdout {
fmt.Printf("Output for test %s:\n%s", tc.Dir, stdoutResult)
return nil
}
goldenCtx := tempfile.GoldenCtx{
Dir: tc.testFolderStdoutPath(),
Update: tc.Update,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
hello:
world: 1
2 changes: 1 addition & 1 deletion integrationtest/command/testdata/path_arg/before/x.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
6tark:
does: 64
does: 64
2 changes: 1 addition & 1 deletion internal/tempfile/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (g GoldenCtx) CompareGoldenFile(path string, gotContent []byte) error {
if gotContent == nil {
gotContent = []byte{}
}
diff := cmp.Diff(expectedContent, gotContent)
diff := cmp.Diff(string(expectedContent), string(gotContent))
// If there is no diff between the content, nothing to do in either mode.
if diff == "" {
return nil
Expand Down

0 comments on commit d838831

Please sign in to comment.