fix bug in nomad fmt -check does not return error code (#15797)
This commit is contained in:
parent
a0652af5dd
commit
e2ae6d62e1
|
@ -0,0 +1,3 @@
|
||||||
|
```release-note:bug
|
||||||
|
cli: fix nomad fmt -check flag not returning error code
|
||||||
|
```
|
|
@ -35,6 +35,7 @@ type FormatCommand struct {
|
||||||
|
|
||||||
list bool
|
list bool
|
||||||
check bool
|
check bool
|
||||||
|
checkSuccess bool
|
||||||
recursive bool
|
recursive bool
|
||||||
write bool
|
write bool
|
||||||
paths []string
|
paths []string
|
||||||
|
@ -106,7 +107,7 @@ func (f *FormatCommand) Run(args []string) int {
|
||||||
if err := flags.Parse(args); err != nil {
|
if err := flags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
f.checkSuccess = true
|
||||||
f.parser = hclparse.NewParser()
|
f.parser = hclparse.NewParser()
|
||||||
|
|
||||||
color := terminal.IsTerminal(int(os.Stderr.Fd()))
|
color := terminal.IsTerminal(int(os.Stderr.Fd()))
|
||||||
|
@ -141,6 +142,9 @@ func (f *FormatCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !f.checkSuccess {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,6 +257,10 @@ func (f *FormatCommand) processFile(path string, r io.Reader) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.check {
|
||||||
|
f.checkSuccess = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !f.list && !f.write {
|
if !f.list && !f.write {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package command
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -19,15 +18,19 @@ func TestFmtCommand(t *testing.T) {
|
||||||
|
|
||||||
const inSuffix = ".in.hcl"
|
const inSuffix = ".in.hcl"
|
||||||
const expectedSuffix = ".out.hcl"
|
const expectedSuffix = ".out.hcl"
|
||||||
tests := []string{"nomad", "job"}
|
tests := [][]string{
|
||||||
|
{"nomad", "-check"},
|
||||||
|
{"nomad", ""},
|
||||||
|
{"job", "-check"},
|
||||||
|
{"job", ""},
|
||||||
|
}
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
for _, testName := range tests {
|
for _, test := range tests {
|
||||||
t.Run(testName, func(t *testing.T) {
|
t.Run(test[0]+test[1], func(t *testing.T) {
|
||||||
inFile := filepath.Join("testdata", "fmt", testName+inSuffix)
|
inFile := filepath.Join("testdata", "fmt", test[0]+inSuffix)
|
||||||
expectedFile := filepath.Join("testdata", "fmt", testName+expectedSuffix)
|
expectedFile := filepath.Join("testdata", "fmt", test[0]+expectedSuffix)
|
||||||
fmtFile := filepath.Join(tmpDir, testName+".hcl")
|
fmtFile := filepath.Join(tmpDir, test[0]+".hcl")
|
||||||
|
|
||||||
input, err := os.ReadFile(inFile)
|
input, err := os.ReadFile(inFile)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -42,8 +45,16 @@ func TestFmtCommand(t *testing.T) {
|
||||||
Meta: Meta{Ui: ui},
|
Meta: Meta{Ui: ui},
|
||||||
}
|
}
|
||||||
|
|
||||||
code := cmd.Run([]string{fmtFile})
|
var code int
|
||||||
assert.Equal(t, 0, code)
|
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)
|
actual, err := os.ReadFile(fmtFile)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -64,11 +75,25 @@ func TestFmtCommand_FromStdin(t *testing.T) {
|
||||||
stdin: stdinFake,
|
stdin: stdinFake,
|
||||||
}
|
}
|
||||||
|
|
||||||
if code := cmd.Run([]string{"-"}); code != 0 {
|
tests := []string{"-check", ""}
|
||||||
t.Fatalf("expected code 0, got %d", code)
|
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, expectedCode, code)
|
||||||
assert.Contains(t, ui.OutputWriter.String(), string(fmtFixture.golden))
|
assert.Contains(t, ui.OutputWriter.String(), string(fmtFixture.golden))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
|
func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
|
||||||
|
@ -86,10 +111,25 @@ func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
|
||||||
Meta: Meta{Ui: ui},
|
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
|
||||||
|
|
||||||
assert.Equal(t, 0, code)
|
if checkFlag == "-check" {
|
||||||
|
code = cmd.Run([]string{checkFlag})
|
||||||
|
expectedCode = 1
|
||||||
|
} else {
|
||||||
|
code = cmd.Run([]string{})
|
||||||
|
expectedCode = 0
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, expectedCode, code)
|
||||||
assert.Equal(t, fmt.Sprintf("%s\n", fmtFixture.filename), ui.OutputWriter.String())
|
assert.Equal(t, fmt.Sprintf("%s\n", fmtFixture.filename), ui.OutputWriter.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
|
func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
|
||||||
|
@ -100,10 +140,25 @@ func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
|
||||||
Meta: Meta{Ui: ui},
|
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
|
||||||
|
|
||||||
assert.Equal(t, 0, code)
|
if checkFlag == "-check" {
|
||||||
|
code = cmd.Run([]string{checkFlag, tmpDir})
|
||||||
|
expectedCode = 1
|
||||||
|
} else {
|
||||||
|
code = cmd.Run([]string{tmpDir})
|
||||||
|
expectedCode = 0
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, expectedCode, code)
|
||||||
assert.Equal(t, fmt.Sprintf("%s\n", filepath.Join(tmpDir, fmtFixture.filename)), ui.OutputWriter.String())
|
assert.Equal(t, fmt.Sprintf("%s\n", filepath.Join(tmpDir, fmtFixture.filename)), ui.OutputWriter.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmtCommand_FromFileArgument(t *testing.T) {
|
func TestFmtCommand_FromFileArgument(t *testing.T) {
|
||||||
|
@ -113,13 +168,26 @@ func TestFmtCommand_FromFileArgument(t *testing.T) {
|
||||||
cmd := &FormatCommand{
|
cmd := &FormatCommand{
|
||||||
Meta: Meta{Ui: ui},
|
Meta: Meta{Ui: ui},
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join(tmpDir, fmtFixture.filename)
|
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
|
||||||
|
|
||||||
assert.Equal(t, 0, code)
|
if checkFlag == "-check" {
|
||||||
|
code = cmd.Run([]string{checkFlag, path})
|
||||||
|
expectedCode = 1
|
||||||
|
} else {
|
||||||
|
code = cmd.Run([]string{path})
|
||||||
|
expectedCode = 0
|
||||||
|
|
||||||
|
}
|
||||||
|
assert.Equal(t, expectedCode, code)
|
||||||
assert.Equal(t, fmt.Sprintf("%s\n", path), ui.OutputWriter.String())
|
assert.Equal(t, fmt.Sprintf("%s\n", path), ui.OutputWriter.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmtCommand_FileDoesNotExist(t *testing.T) {
|
func TestFmtCommand_FileDoesNotExist(t *testing.T) {
|
||||||
|
@ -152,7 +220,7 @@ func TestFmtCommand_InvalidSyntax(t *testing.T) {
|
||||||
func fmtFixtureWriteDir(t *testing.T) string {
|
func fmtFixtureWriteDir(t *testing.T) string {
|
||||||
dir := t.TempDir()
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return dir
|
return dir
|
||||||
|
|
Loading…
Reference in New Issue