2016-02-02 23:10:22 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-02-02 23:10:22 +01:00
|
|
|
"bytes"
|
2016-02-02 23:10:22 +01:00
|
|
|
"fmt"
|
2016-02-02 23:10:22 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
command/fmt: Restore some opinionated behaviors
In Terraform 0.11 and earlier, the "terraform fmt" command was very
opinionated in the interests of consistency. While that remains its goal,
for pragmatic reasons Terraform 0.12 significantly reduced the number
of formatting behaviors in the fmt command. We've held off on introducing
0.12-and-later-flavored cleanups out of concern it would make it harder
to maintain modules that are cross-compatible with both Terraform 0.11
and 0.12, but with this aimed to land in 0.14 -- two major releases
later -- our new goal is to help those who find older Terraform language
examples learn about the more modern idiom.
More rules may follow later, now that the implementation is set up to
allow modifications to tokens as well as modifications to whitespace, but
for this initial pass the command will now apply the following formatting
conventions:
- 0.11-style quoted variable type constraints will be replaced with their
0.12 syntax equivalents. For example, "string" becomes just string.
(This change quiets a deprecation warning.)
- Collection type constraints that don't specify an element type will
be rewritten to specify the "any" element type explicitly, so
list becomes list(any).
- Arguments whose expressions consist of a quoted string template with
only a single interpolation sequence inside will be "unwrapped" to be
the naked expression instead, which is functionally equivalent.
(This change quiets a deprecation warning.)
- Block labels are given in quotes.
Two of the rules above are coming from a secondary motivation of
continuing down the deprecation path for two existing warnings, so authors
can have two active deprecation warnings quieted automatically by
"terraform fmt", without the need to run any third-party tools.
All of these rules match with current documented idiom as shown in the
Terraform documentation, so anyone who follows the documented style should
see no changes as a result of this. Those who have adopted other local
style will see their configuration files rewritten to the standard
Terraform style, but it should not make any changes that affect the
functionality of the configuration.
There are some further similar rewriting rules that could be added in
future, such as removing 0.11-style quotes around various keyword or
static reference arguments, but this initial pass focused only on some
rules that have been proven out in the third-party tool
terraform-clean-syntax, from which much of this commit is a direct port.
For now this doesn't attempt to re-introduce any rules about vertical
whitespace, even though the 0.11 "terraform fmt" would previously apply
such changes. We'll be more cautious about those because the results of
those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
to re-introduce those with some care to the implications for those who
may be using vertical formatting differences for some semantic purpose,
like grouping together related arguments.
2020-09-26 02:16:26 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2016-02-02 23:10:22 +01:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
command/fmt: Restore some opinionated behaviors
In Terraform 0.11 and earlier, the "terraform fmt" command was very
opinionated in the interests of consistency. While that remains its goal,
for pragmatic reasons Terraform 0.12 significantly reduced the number
of formatting behaviors in the fmt command. We've held off on introducing
0.12-and-later-flavored cleanups out of concern it would make it harder
to maintain modules that are cross-compatible with both Terraform 0.11
and 0.12, but with this aimed to land in 0.14 -- two major releases
later -- our new goal is to help those who find older Terraform language
examples learn about the more modern idiom.
More rules may follow later, now that the implementation is set up to
allow modifications to tokens as well as modifications to whitespace, but
for this initial pass the command will now apply the following formatting
conventions:
- 0.11-style quoted variable type constraints will be replaced with their
0.12 syntax equivalents. For example, "string" becomes just string.
(This change quiets a deprecation warning.)
- Collection type constraints that don't specify an element type will
be rewritten to specify the "any" element type explicitly, so
list becomes list(any).
- Arguments whose expressions consist of a quoted string template with
only a single interpolation sequence inside will be "unwrapped" to be
the naked expression instead, which is functionally equivalent.
(This change quiets a deprecation warning.)
- Block labels are given in quotes.
Two of the rules above are coming from a secondary motivation of
continuing down the deprecation path for two existing warnings, so authors
can have two active deprecation warnings quieted automatically by
"terraform fmt", without the need to run any third-party tools.
All of these rules match with current documented idiom as shown in the
Terraform documentation, so anyone who follows the documented style should
see no changes as a result of this. Those who have adopted other local
style will see their configuration files rewritten to the standard
Terraform style, but it should not make any changes that affect the
functionality of the configuration.
There are some further similar rewriting rules that could be added in
future, such as removing 0.11-style quotes around various keyword or
static reference arguments, but this initial pass focused only on some
rules that have been proven out in the third-party tool
terraform-clean-syntax, from which much of this commit is a direct port.
For now this doesn't attempt to re-introduce any rules about vertical
whitespace, even though the 0.11 "terraform fmt" would previously apply
such changes. We'll be more cautious about those because the results of
those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
to re-introduce those with some care to the implications for those who
may be using vertical formatting differences for some semantic purpose,
like grouping together related arguments.
2020-09-26 02:16:26 +02:00
|
|
|
func TestFmt(t *testing.T) {
|
|
|
|
const inSuffix = "_in.tf"
|
|
|
|
const outSuffix = "_out.tf"
|
|
|
|
const gotSuffix = "_got.tf"
|
|
|
|
entries, err := ioutil.ReadDir("testdata/fmt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:46:51 +02:00
|
|
|
td, err := ioutil.TempDir("", "terraform-fmt-test")
|
command/fmt: Restore some opinionated behaviors
In Terraform 0.11 and earlier, the "terraform fmt" command was very
opinionated in the interests of consistency. While that remains its goal,
for pragmatic reasons Terraform 0.12 significantly reduced the number
of formatting behaviors in the fmt command. We've held off on introducing
0.12-and-later-flavored cleanups out of concern it would make it harder
to maintain modules that are cross-compatible with both Terraform 0.11
and 0.12, but with this aimed to land in 0.14 -- two major releases
later -- our new goal is to help those who find older Terraform language
examples learn about the more modern idiom.
More rules may follow later, now that the implementation is set up to
allow modifications to tokens as well as modifications to whitespace, but
for this initial pass the command will now apply the following formatting
conventions:
- 0.11-style quoted variable type constraints will be replaced with their
0.12 syntax equivalents. For example, "string" becomes just string.
(This change quiets a deprecation warning.)
- Collection type constraints that don't specify an element type will
be rewritten to specify the "any" element type explicitly, so
list becomes list(any).
- Arguments whose expressions consist of a quoted string template with
only a single interpolation sequence inside will be "unwrapped" to be
the naked expression instead, which is functionally equivalent.
(This change quiets a deprecation warning.)
- Block labels are given in quotes.
Two of the rules above are coming from a secondary motivation of
continuing down the deprecation path for two existing warnings, so authors
can have two active deprecation warnings quieted automatically by
"terraform fmt", without the need to run any third-party tools.
All of these rules match with current documented idiom as shown in the
Terraform documentation, so anyone who follows the documented style should
see no changes as a result of this. Those who have adopted other local
style will see their configuration files rewritten to the standard
Terraform style, but it should not make any changes that affect the
functionality of the configuration.
There are some further similar rewriting rules that could be added in
future, such as removing 0.11-style quotes around various keyword or
static reference arguments, but this initial pass focused only on some
rules that have been proven out in the third-party tool
terraform-clean-syntax, from which much of this commit is a direct port.
For now this doesn't attempt to re-introduce any rules about vertical
whitespace, even though the 0.11 "terraform fmt" would previously apply
such changes. We'll be more cautious about those because the results of
those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
to re-introduce those with some care to the implications for those who
may be using vertical formatting differences for some semantic purpose,
like grouping together related arguments.
2020-09-26 02:16:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-29 14:46:51 +02:00
|
|
|
tmpDir, err := filepath.EvalSymlinks(td)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
command/fmt: Restore some opinionated behaviors
In Terraform 0.11 and earlier, the "terraform fmt" command was very
opinionated in the interests of consistency. While that remains its goal,
for pragmatic reasons Terraform 0.12 significantly reduced the number
of formatting behaviors in the fmt command. We've held off on introducing
0.12-and-later-flavored cleanups out of concern it would make it harder
to maintain modules that are cross-compatible with both Terraform 0.11
and 0.12, but with this aimed to land in 0.14 -- two major releases
later -- our new goal is to help those who find older Terraform language
examples learn about the more modern idiom.
More rules may follow later, now that the implementation is set up to
allow modifications to tokens as well as modifications to whitespace, but
for this initial pass the command will now apply the following formatting
conventions:
- 0.11-style quoted variable type constraints will be replaced with their
0.12 syntax equivalents. For example, "string" becomes just string.
(This change quiets a deprecation warning.)
- Collection type constraints that don't specify an element type will
be rewritten to specify the "any" element type explicitly, so
list becomes list(any).
- Arguments whose expressions consist of a quoted string template with
only a single interpolation sequence inside will be "unwrapped" to be
the naked expression instead, which is functionally equivalent.
(This change quiets a deprecation warning.)
- Block labels are given in quotes.
Two of the rules above are coming from a secondary motivation of
continuing down the deprecation path for two existing warnings, so authors
can have two active deprecation warnings quieted automatically by
"terraform fmt", without the need to run any third-party tools.
All of these rules match with current documented idiom as shown in the
Terraform documentation, so anyone who follows the documented style should
see no changes as a result of this. Those who have adopted other local
style will see their configuration files rewritten to the standard
Terraform style, but it should not make any changes that affect the
functionality of the configuration.
There are some further similar rewriting rules that could be added in
future, such as removing 0.11-style quotes around various keyword or
static reference arguments, but this initial pass focused only on some
rules that have been proven out in the third-party tool
terraform-clean-syntax, from which much of this commit is a direct port.
For now this doesn't attempt to re-introduce any rules about vertical
whitespace, even though the 0.11 "terraform fmt" would previously apply
such changes. We'll be more cautious about those because the results of
those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
to re-introduce those with some care to the implications for those who
may be using vertical formatting differences for some semantic purpose,
like grouping together related arguments.
2020-09-26 02:16:26 +02:00
|
|
|
|
|
|
|
for _, info := range entries {
|
|
|
|
if info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filename := info.Name()
|
|
|
|
if !strings.HasSuffix(filename, inSuffix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testName := filename[:len(filename)-len(inSuffix)]
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
inFile := filepath.Join("testdata", "fmt", testName+inSuffix)
|
|
|
|
wantFile := filepath.Join("testdata", "fmt", testName+outSuffix)
|
|
|
|
gotFile := filepath.Join(tmpDir, testName+gotSuffix)
|
|
|
|
input, err := ioutil.ReadFile(inFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want, err := ioutil.ReadFile(wantFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(gotFile, input, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{gotFile}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("fmt command was unsuccessful:\n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := ioutil.ReadFile(gotFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(string(want), string(got)); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
func TestFmt_nonexist(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
missingDir := filepath.Join(tempDir, "doesnotexist")
|
|
|
|
args := []string{missingDir}
|
2016-02-02 23:10:22 +01:00
|
|
|
if code := c.Run(args); code != 2 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:58:59 +01:00
|
|
|
expected := "No file or directory at"
|
2016-02-02 23:10:22 +01:00
|
|
|
if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:44:37 +01:00
|
|
|
func TestFmt_syntaxError(t *testing.T) {
|
|
|
|
tempDir := testTempDir(t)
|
|
|
|
|
|
|
|
invalidSrc := `
|
|
|
|
a = 1 +
|
|
|
|
`
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(filepath.Join(tempDir, "invalid.tf"), []byte(invalidSrc), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{tempDir}
|
|
|
|
if code := c.Run(args); code != 2 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Invalid expression"
|
|
|
|
if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 18:58:20 +01:00
|
|
|
func TestFmt_snippetInError(t *testing.T) {
|
|
|
|
tempDir := testTempDir(t)
|
|
|
|
|
|
|
|
backendSrc := `terraform {backend "s3" {}}`
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(filepath.Join(tempDir, "backend.tf"), []byte(backendSrc), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{tempDir}
|
|
|
|
if code := c.Run(args); code != 2 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
substrings := []string{
|
|
|
|
"Argument definition required",
|
|
|
|
"line 1, in terraform",
|
|
|
|
`1: terraform {backend "s3" {}}`,
|
|
|
|
}
|
|
|
|
for _, substring := range substrings {
|
|
|
|
if actual := ui.ErrorWriter.String(); !strings.Contains(actual, substring) {
|
|
|
|
t.Errorf("expected:\n%s\n\nto include: %q", actual, substring)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
func TestFmt_tooManyArgs(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
args := []string{
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
}
|
2016-02-02 23:10:22 +01:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
expected := "The fmt command expects at most one argument."
|
2016-02-02 23:10:22 +01:00
|
|
|
if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmt_workingDirectory(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = os.Chdir(tempDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
expected := fmt.Sprintf("%s\n", fmtFixture.filename)
|
|
|
|
if actual := ui.OutputWriter.String(); actual != expected {
|
2016-02-02 23:10:22 +01:00
|
|
|
t.Fatalf("got: %q\nexpected: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
func TestFmt_directoryArg(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:22 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{tempDir}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-10-13 17:19:54 +02:00
|
|
|
got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := filepath.Join(tempDir, fmtFixture.filename)
|
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("wrong output\ngot: %s\nwant: %s", got, want)
|
2016-02-02 23:10:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:58:59 +01:00
|
|
|
func TestFmt_fileArg(t *testing.T) {
|
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(tempDir, fmtFixture.filename)}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := filepath.Join(tempDir, fmtFixture.filename)
|
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("wrong output\ngot: %s\nwant: %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
func TestFmt_stdinArg(t *testing.T) {
|
|
|
|
input := new(bytes.Buffer)
|
|
|
|
input.Write(fmtFixture.input)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:22 +01:00
|
|
|
},
|
|
|
|
input: input,
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:23 +01:00
|
|
|
args := []string{"-"}
|
2016-02-02 23:10:22 +01:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := fmtFixture.golden
|
|
|
|
if actual := ui.OutputWriter.Bytes(); !bytes.Equal(actual, expected) {
|
|
|
|
t.Fatalf("got: %q\nexpected: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:23 +01:00
|
|
|
func TestFmt_nonDefaultOptions(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
2016-02-02 23:10:23 +01:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2016-02-02 23:10:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-list=false",
|
|
|
|
"-write=false",
|
|
|
|
"-diff",
|
|
|
|
tempDir,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := fmt.Sprintf("-%s+%s", fmtFixture.input, fmtFixture.golden)
|
|
|
|
if actual := ui.OutputWriter.String(); !strings.Contains(actual, expected) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 18:10:28 +02:00
|
|
|
func TestFmt_check(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
tempDir := fmtFixtureWriteDir(t)
|
2017-06-23 18:10:28 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-check",
|
|
|
|
tempDir,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 3 {
|
|
|
|
t.Fatalf("wrong exit code. expected 3")
|
|
|
|
}
|
|
|
|
|
2019-06-18 22:43:28 +02:00
|
|
|
// Given that we give relative paths back to the user, normalize this temp
|
|
|
|
// dir so that we're comparing against a relative-ized (normalized) path
|
|
|
|
tempDir = c.normalizePath(tempDir)
|
|
|
|
|
2017-06-23 18:10:28 +02:00
|
|
|
if actual := ui.OutputWriter.String(); !strings.Contains(actual, tempDir) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, tempDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmt_checkStdin(t *testing.T) {
|
|
|
|
input := new(bytes.Buffer)
|
|
|
|
input.Write(fmtFixture.input)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &FmtCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
input: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-check",
|
|
|
|
"-",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 3 {
|
|
|
|
t.Fatalf("wrong exit code. expected 3, got %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
t.Fatalf("expected no output, got: %q", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:10:22 +01:00
|
|
|
var fmtFixture = struct {
|
|
|
|
filename string
|
|
|
|
input, golden []byte
|
|
|
|
}{
|
|
|
|
"main.tf",
|
|
|
|
[]byte(` foo = "bar"
|
|
|
|
`),
|
|
|
|
[]byte(`foo = "bar"
|
|
|
|
`),
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
func fmtFixtureWriteDir(t *testing.T) string {
|
|
|
|
dir := testTempDir(t)
|
2016-02-02 23:10:22 +01:00
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
err := ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
|
2016-02-02 23:10:22 +01:00
|
|
|
if err != nil {
|
2018-03-28 19:08:38 +02:00
|
|
|
t.Fatal(err)
|
2016-02-02 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
return dir
|
2016-02-02 23:10:22 +01:00
|
|
|
}
|