From f51e5522cbb4404c9352c01e43d7d7e923e665f0 Mon Sep 17 00:00:00 2001 From: braydonk Date: Fri, 3 May 2024 12:57:17 +0000 Subject: [PATCH 1/2] engine: return nil from dry run when no changes The `command` output actually relies on `nil` being return when there are no changes, and I forgot to add that behaviour to dry run which made there be no message printed out for confirmation. Also decided to make a slight change to the confirmation message so that it outputs nothing when the `-quiet` flag is enabled. Signed-off-by: braydonk --- command/command.go | 4 ++-- engine/consecutive_engine.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/command/command.go b/command/command.go index 51c68f4..7fc7862 100644 --- a/command/command.go +++ b/command/command.go @@ -146,8 +146,8 @@ func (c *Command) Run() error { } if out != nil { fmt.Print(out) - } else { - fmt.Print("No files will be changed.") + } else if !c.Quiet { + fmt.Println("No files will be changed.") } case yamlfmt.OperationStdin: stdinYaml, err := readFromStdin() diff --git a/engine/consecutive_engine.go b/engine/consecutive_engine.go index 3f85927..650d1b7 100644 --- a/engine/consecutive_engine.go +++ b/engine/consecutive_engine.go @@ -63,6 +63,9 @@ func (e *ConsecutiveEngine) DryRun(paths []string) (fmt.Stringer, error) { if len(formatErrs) > 0 { return nil, formatErrs } + if formatDiffs.ChangedCount() == 0 { + return nil, nil + } return getEngineOutput(e.OutputFormat, yamlfmt.OperationDry, formatDiffs, e.Quiet) } From 0da2a786a37eb1aa078f9a60cd5814d62a02c3c3 Mon Sep 17 00:00:00 2001 From: braydonk Date: Fri, 3 May 2024 13:44:49 +0000 Subject: [PATCH 2/2] add integration tests --- integrationtest/command/command_test.go | 16 ++++++++++++++++ .../command/testdata/dry/after/a.yaml | 1 + .../command/testdata/dry/before/a.yaml | 1 + .../command/testdata/dry/stdout/stderr.txt | 0 .../command/testdata/dry/stdout/stdout.txt | 1 + .../command/testdata/dry_quiet/after/a.yaml | 1 + .../command/testdata/dry_quiet/before/a.yaml | 1 + .../command/testdata/dry_quiet/stdout/stderr.txt | 0 .../command/testdata/dry_quiet/stdout/stdout.txt | 0 9 files changed, 21 insertions(+) create mode 100755 integrationtest/command/testdata/dry/after/a.yaml create mode 100644 integrationtest/command/testdata/dry/before/a.yaml create mode 100755 integrationtest/command/testdata/dry/stdout/stderr.txt create mode 100755 integrationtest/command/testdata/dry/stdout/stdout.txt create mode 100755 integrationtest/command/testdata/dry_quiet/after/a.yaml create mode 100644 integrationtest/command/testdata/dry_quiet/before/a.yaml create mode 100755 integrationtest/command/testdata/dry_quiet/stdout/stderr.txt create mode 100755 integrationtest/command/testdata/dry_quiet/stdout/stdout.txt diff --git a/integrationtest/command/command_test.go b/integrationtest/command/command_test.go index 74bf68a..9030b4a 100644 --- a/integrationtest/command/command_test.go +++ b/integrationtest/command/command_test.go @@ -70,3 +70,19 @@ func TestLineOutput(t *testing.T) { IsError: true, }.Run(t) } + +func TestDry(t *testing.T) { + TestCase{ + Dir: "dry", + Command: yamlfmtWithArgs("-dry ."), + Update: *updateFlag, + }.Run(t) +} + +func TestDryQuiet(t *testing.T) { + TestCase{ + Dir: "dry_quiet", + Command: yamlfmtWithArgs("-dry -quiet ."), + Update: *updateFlag, + }.Run(t) +} diff --git a/integrationtest/command/testdata/dry/after/a.yaml b/integrationtest/command/testdata/dry/after/a.yaml new file mode 100755 index 0000000..a8926a5 --- /dev/null +++ b/integrationtest/command/testdata/dry/after/a.yaml @@ -0,0 +1 @@ +a: 1 diff --git a/integrationtest/command/testdata/dry/before/a.yaml b/integrationtest/command/testdata/dry/before/a.yaml new file mode 100644 index 0000000..a8926a5 --- /dev/null +++ b/integrationtest/command/testdata/dry/before/a.yaml @@ -0,0 +1 @@ +a: 1 diff --git a/integrationtest/command/testdata/dry/stdout/stderr.txt b/integrationtest/command/testdata/dry/stdout/stderr.txt new file mode 100755 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/dry/stdout/stdout.txt b/integrationtest/command/testdata/dry/stdout/stdout.txt new file mode 100755 index 0000000..bc0cf0f --- /dev/null +++ b/integrationtest/command/testdata/dry/stdout/stdout.txt @@ -0,0 +1 @@ +No files will be changed. diff --git a/integrationtest/command/testdata/dry_quiet/after/a.yaml b/integrationtest/command/testdata/dry_quiet/after/a.yaml new file mode 100755 index 0000000..a8926a5 --- /dev/null +++ b/integrationtest/command/testdata/dry_quiet/after/a.yaml @@ -0,0 +1 @@ +a: 1 diff --git a/integrationtest/command/testdata/dry_quiet/before/a.yaml b/integrationtest/command/testdata/dry_quiet/before/a.yaml new file mode 100644 index 0000000..a8926a5 --- /dev/null +++ b/integrationtest/command/testdata/dry_quiet/before/a.yaml @@ -0,0 +1 @@ +a: 1 diff --git a/integrationtest/command/testdata/dry_quiet/stdout/stderr.txt b/integrationtest/command/testdata/dry_quiet/stdout/stderr.txt new file mode 100755 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/dry_quiet/stdout/stdout.txt b/integrationtest/command/testdata/dry_quiet/stdout/stdout.txt new file mode 100755 index 0000000..e69de29