Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug in nomad fmt -check does not return error code #15797

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/15797.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: fix nomad fmt -check flag not returning error code
```
20 changes: 14 additions & 6 deletions command/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ type FormatCommand struct {

errs *multierror.Error

list bool
check bool
recursive bool
write bool
paths []string
list bool
check bool
checkSuccess bool
recursive bool
write bool
paths []string

stdin io.Reader
}
Expand Down Expand Up @@ -106,7 +107,7 @@ func (f *FormatCommand) Run(args []string) int {
if err := flags.Parse(args); err != nil {
return 1
}

f.checkSuccess = true
f.parser = hclparse.NewParser()

color := terminal.IsTerminal(int(os.Stderr.Fd()))
Expand Down Expand Up @@ -141,6 +142,9 @@ func (f *FormatCommand) Run(args []string) int {
return 1
}

if !f.checkSuccess {
return 1
}
return 0
}

Expand Down Expand Up @@ -253,6 +257,10 @@ func (f *FormatCommand) processFile(path string, r io.Reader) {
return
}
}

if f.check {
f.checkSuccess = false
}
}

if !f.list && !f.write {
Expand Down
118 changes: 93 additions & 25 deletions command/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -19,15 +18,19 @@ func TestFmtCommand(t *testing.T) {

const inSuffix = ".in.hcl"
const expectedSuffix = ".out.hcl"
tests := []string{"nomad", "job"}

tests := [][]string{
{"nomad", "-check"},
{"nomad", ""},
{"job", "-check"},
{"job", ""},
}
tmpDir := t.TempDir()

for _, testName := range tests {
t.Run(testName, func(t *testing.T) {
inFile := filepath.Join("testdata", "fmt", testName+inSuffix)
expectedFile := filepath.Join("testdata", "fmt", testName+expectedSuffix)
fmtFile := filepath.Join(tmpDir, testName+".hcl")
for _, test := range tests {
t.Run(test[0]+test[1], func(t *testing.T) {
inFile := filepath.Join("testdata", "fmt", test[0]+inSuffix)
expectedFile := filepath.Join("testdata", "fmt", test[0]+expectedSuffix)
fmtFile := filepath.Join(tmpDir, test[0]+".hcl")

input, err := os.ReadFile(inFile)
require.NoError(t, err)
Expand All @@ -42,8 +45,16 @@ func TestFmtCommand(t *testing.T) {
Meta: Meta{Ui: ui},
}

code := cmd.Run([]string{fmtFile})
assert.Equal(t, 0, code)
var code int
var expectedCode int
if test[1] == "-check" {
code = cmd.Run([]string{test[1], fmtFile})
expectedCode = 1
} else {
code = cmd.Run([]string{fmtFile})
expectedCode = 0
}
assert.Equal(t, expectedCode, code)

actual, err := os.ReadFile(fmtFile)
require.NoError(t, err)
Expand All @@ -64,11 +75,25 @@ func TestFmtCommand_FromStdin(t *testing.T) {
stdin: stdinFake,
}

if code := cmd.Run([]string{"-"}); code != 0 {
t.Fatalf("expected code 0, got %d", code)
}
tests := []string{"-check", ""}
for _, checkFlag := range tests {
t.Run(checkFlag, func(t *testing.T) {
var code int
var expectedCode int

if checkFlag == "-check" {
code = cmd.Run([]string{checkFlag, "-"})
expectedCode = 1
} else {
code = cmd.Run([]string{"-"})
expectedCode = 0

}

assert.Contains(t, ui.OutputWriter.String(), string(fmtFixture.golden))
assert.Equal(t, expectedCode, code)
assert.Contains(t, ui.OutputWriter.String(), string(fmtFixture.golden))
})
}
}

func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
Expand All @@ -86,10 +111,25 @@ func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
Meta: Meta{Ui: ui},
}

code := cmd.Run([]string{})
tests := []string{"-check", ""}
for _, checkFlag := range tests {
t.Run(checkFlag, func(t *testing.T) {
var code int
var expectedCode int

if checkFlag == "-check" {
code = cmd.Run([]string{checkFlag})
expectedCode = 1
} else {
code = cmd.Run([]string{})
expectedCode = 0

}

assert.Equal(t, 0, code)
assert.Equal(t, fmt.Sprintf("%s\n", fmtFixture.filename), ui.OutputWriter.String())
assert.Equal(t, expectedCode, code)
assert.Equal(t, fmt.Sprintf("%s\n", fmtFixture.filename), ui.OutputWriter.String())
})
}
}

func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
Expand All @@ -100,10 +140,25 @@ func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
Meta: Meta{Ui: ui},
}

code := cmd.Run([]string{tmpDir})
tests := []string{"-check", ""}
for _, checkFlag := range tests {
t.Run(checkFlag, func(t *testing.T) {
var code int
var expectedCode int

if checkFlag == "-check" {
code = cmd.Run([]string{checkFlag, tmpDir})
expectedCode = 1
} else {
code = cmd.Run([]string{tmpDir})
expectedCode = 0

assert.Equal(t, 0, code)
assert.Equal(t, fmt.Sprintf("%s\n", filepath.Join(tmpDir, fmtFixture.filename)), ui.OutputWriter.String())
}

assert.Equal(t, expectedCode, code)
assert.Equal(t, fmt.Sprintf("%s\n", filepath.Join(tmpDir, fmtFixture.filename)), ui.OutputWriter.String())
})
}
}

func TestFmtCommand_FromFileArgument(t *testing.T) {
Expand All @@ -113,13 +168,26 @@ func TestFmtCommand_FromFileArgument(t *testing.T) {
cmd := &FormatCommand{
Meta: Meta{Ui: ui},
}

path := filepath.Join(tmpDir, fmtFixture.filename)

code := cmd.Run([]string{path})
tests := []string{"-check", ""}
for _, checkFlag := range tests {
t.Run(checkFlag, func(t *testing.T) {
var code int
var expectedCode int

if checkFlag == "-check" {
code = cmd.Run([]string{checkFlag, path})
expectedCode = 1
} else {
code = cmd.Run([]string{path})
expectedCode = 0

assert.Equal(t, 0, code)
assert.Equal(t, fmt.Sprintf("%s\n", path), ui.OutputWriter.String())
}
assert.Equal(t, expectedCode, code)
assert.Equal(t, fmt.Sprintf("%s\n", path), ui.OutputWriter.String())
})
}
}

func TestFmtCommand_FileDoesNotExist(t *testing.T) {
Expand Down Expand Up @@ -152,7 +220,7 @@ func TestFmtCommand_InvalidSyntax(t *testing.T) {
func fmtFixtureWriteDir(t *testing.T) string {
dir := t.TempDir()

err := ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
err := os.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
require.NoError(t, err)

return dir
Expand Down