2022-10-06 21:00:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFmtCommand(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
const inSuffix = ".in.hcl"
|
|
|
|
const expectedSuffix = ".out.hcl"
|
2023-01-17 14:15:34 +00:00
|
|
|
tests := [][]string{
|
|
|
|
{"nomad", "-check"},
|
|
|
|
{"nomad", ""},
|
|
|
|
{"job", "-check"},
|
|
|
|
{"job", ""},
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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")
|
2022-10-06 21:00:29 +00:00
|
|
|
|
|
|
|
input, err := os.ReadFile(inFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected, err := os.ReadFile(expectedFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, os.WriteFile(fmtFile, input, 0644))
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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)
|
2022-10-06 21:00:29 +00:00
|
|
|
|
|
|
|
actual, err := os.ReadFile(fmtFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, string(expected), string(actual))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_FromStdin(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
stdinFake := bytes.NewBuffer(fmtFixture.input)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
stdin: stdinFake,
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
assert.Equal(t, expectedCode, code)
|
|
|
|
assert.Contains(t, ui.OutputWriter.String(), string(fmtFixture.golden))
|
|
|
|
})
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_FromWorkingDirectory(t *testing.T) {
|
|
|
|
tmpDir := fmtFixtureWriteDir(t)
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Chdir(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
assert.Equal(t, expectedCode, code)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%s\n", fmtFixture.filename), ui.OutputWriter.String())
|
|
|
|
})
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_FromDirectoryArgument(t *testing.T) {
|
|
|
|
tmpDir := fmtFixtureWriteDir(t)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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
|
2022-10-06 21:00:29 +00:00
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedCode, code)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%s\n", filepath.Join(tmpDir, fmtFixture.filename)), ui.OutputWriter.String())
|
|
|
|
})
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_FromFileArgument(t *testing.T) {
|
|
|
|
tmpDir := fmtFixtureWriteDir(t)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
path := filepath.Join(tmpDir, fmtFixture.filename)
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
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
|
2022-10-06 21:00:29 +00:00
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedCode, code)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%s\n", path), ui.OutputWriter.String())
|
|
|
|
})
|
|
|
|
}
|
2022-10-06 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_FileDoesNotExist(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run([]string{"file/does/not/exist.hcl"})
|
|
|
|
assert.Equal(t, 1, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmtCommand_InvalidSyntax(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
stdinFake := bytes.NewBufferString(`client {enabled true }`)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &FormatCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
stdin: stdinFake,
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run([]string{"-"})
|
|
|
|
assert.Equal(t, 1, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtFixtureWriteDir(t *testing.T) string {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
2023-01-17 14:15:34 +00:00
|
|
|
err := os.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
|
2022-10-06 21:00:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
var fmtFixture = struct {
|
|
|
|
filename string
|
|
|
|
input []byte
|
|
|
|
golden []byte
|
|
|
|
}{
|
|
|
|
filename: "nomad.hcl",
|
|
|
|
input: []byte(`client {enabled = true}`),
|
|
|
|
golden: []byte(`client { enabled = true }`),
|
|
|
|
}
|