github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/fmt_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/mitchellh/cli"
    14  )
    15  
    16  func TestFmt(t *testing.T) {
    17  	const inSuffix = "_in.tf"
    18  	const outSuffix = "_out.tf"
    19  	const gotSuffix = "_got.tf"
    20  	entries, err := ioutil.ReadDir("testdata/fmt")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	tmpDir, err := filepath.EvalSymlinks(t.TempDir())
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	for _, info := range entries {
    31  		if info.IsDir() {
    32  			continue
    33  		}
    34  		filename := info.Name()
    35  		if !strings.HasSuffix(filename, inSuffix) {
    36  			continue
    37  		}
    38  		testName := filename[:len(filename)-len(inSuffix)]
    39  		t.Run(testName, func(t *testing.T) {
    40  			inFile := filepath.Join("testdata", "fmt", testName+inSuffix)
    41  			wantFile := filepath.Join("testdata", "fmt", testName+outSuffix)
    42  			gotFile := filepath.Join(tmpDir, testName+gotSuffix)
    43  			input, err := ioutil.ReadFile(inFile)
    44  			if err != nil {
    45  				t.Fatal(err)
    46  			}
    47  			want, err := ioutil.ReadFile(wantFile)
    48  			if err != nil {
    49  				t.Fatal(err)
    50  			}
    51  			err = ioutil.WriteFile(gotFile, input, 0700)
    52  			if err != nil {
    53  				t.Fatal(err)
    54  			}
    55  
    56  			ui := cli.NewMockUi()
    57  			c := &FmtCommand{
    58  				Meta: Meta{
    59  					testingOverrides: metaOverridesForProvider(testProvider()),
    60  					Ui:               ui,
    61  				},
    62  			}
    63  			args := []string{gotFile}
    64  			if code := c.Run(args); code != 0 {
    65  				t.Fatalf("fmt command was unsuccessful:\n%s", ui.ErrorWriter.String())
    66  			}
    67  
    68  			got, err := ioutil.ReadFile(gotFile)
    69  			if err != nil {
    70  				t.Fatal(err)
    71  			}
    72  
    73  			if diff := cmp.Diff(string(want), string(got)); diff != "" {
    74  				t.Errorf("wrong result\n%s", diff)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestFmt_nonexist(t *testing.T) {
    81  	tempDir := fmtFixtureWriteDir(t)
    82  
    83  	ui := new(cli.MockUi)
    84  	c := &FmtCommand{
    85  		Meta: Meta{
    86  			testingOverrides: metaOverridesForProvider(testProvider()),
    87  			Ui:               ui,
    88  		},
    89  	}
    90  
    91  	missingDir := filepath.Join(tempDir, "doesnotexist")
    92  	args := []string{missingDir}
    93  	if code := c.Run(args); code != 2 {
    94  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
    95  	}
    96  
    97  	expected := "No file or directory at"
    98  	if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
    99  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
   100  	}
   101  }
   102  
   103  func TestFmt_syntaxError(t *testing.T) {
   104  	tempDir := testTempDir(t)
   105  
   106  	invalidSrc := `
   107  a = 1 +
   108  `
   109  
   110  	err := ioutil.WriteFile(filepath.Join(tempDir, "invalid.tf"), []byte(invalidSrc), 0644)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	ui := new(cli.MockUi)
   116  	c := &FmtCommand{
   117  		Meta: Meta{
   118  			testingOverrides: metaOverridesForProvider(testProvider()),
   119  			Ui:               ui,
   120  		},
   121  	}
   122  
   123  	args := []string{tempDir}
   124  	if code := c.Run(args); code != 2 {
   125  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   126  	}
   127  
   128  	expected := "Invalid expression"
   129  	if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
   130  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
   131  	}
   132  }
   133  
   134  func TestFmt_snippetInError(t *testing.T) {
   135  	tempDir := testTempDir(t)
   136  
   137  	backendSrc := `terraform {backend "s3" {}}`
   138  
   139  	err := ioutil.WriteFile(filepath.Join(tempDir, "backend.tf"), []byte(backendSrc), 0644)
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	ui := new(cli.MockUi)
   145  	c := &FmtCommand{
   146  		Meta: Meta{
   147  			testingOverrides: metaOverridesForProvider(testProvider()),
   148  			Ui:               ui,
   149  		},
   150  	}
   151  
   152  	args := []string{tempDir}
   153  	if code := c.Run(args); code != 2 {
   154  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   155  	}
   156  
   157  	substrings := []string{
   158  		"Argument definition required",
   159  		"line 1, in terraform",
   160  		`1: terraform {backend "s3" {}}`,
   161  	}
   162  	for _, substring := range substrings {
   163  		if actual := ui.ErrorWriter.String(); !strings.Contains(actual, substring) {
   164  			t.Errorf("expected:\n%s\n\nto include: %q", actual, substring)
   165  		}
   166  	}
   167  }
   168  
   169  func TestFmt_manyArgs(t *testing.T) {
   170  	tempDir := fmtFixtureWriteDir(t)
   171  	// Add a second file
   172  	secondSrc := `locals { x = 1 }`
   173  
   174  	err := ioutil.WriteFile(filepath.Join(tempDir, "second.tf"), []byte(secondSrc), 0644)
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	ui := new(cli.MockUi)
   180  	c := &FmtCommand{
   181  		Meta: Meta{
   182  			testingOverrides: metaOverridesForProvider(testProvider()),
   183  			Ui:               ui,
   184  		},
   185  	}
   186  
   187  	args := []string{
   188  		filepath.Join(tempDir, "main.tf"),
   189  		filepath.Join(tempDir, "second.tf"),
   190  	}
   191  	if code := c.Run(args); code != 0 {
   192  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   193  	}
   194  
   195  	got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
   196  	if err != nil {
   197  		t.Fatal(err)
   198  	}
   199  	want := filepath.Join(tempDir, fmtFixture.filename)
   200  
   201  	if got != want {
   202  		t.Fatalf("wrong output\ngot:  %s\nwant: %s", got, want)
   203  	}
   204  }
   205  
   206  func TestFmt_workingDirectory(t *testing.T) {
   207  	tempDir := fmtFixtureWriteDir(t)
   208  
   209  	cwd, err := os.Getwd()
   210  	if err != nil {
   211  		t.Fatalf("err: %s", err)
   212  	}
   213  	err = os.Chdir(tempDir)
   214  	if err != nil {
   215  		t.Fatalf("err: %s", err)
   216  	}
   217  	defer os.Chdir(cwd)
   218  
   219  	ui := new(cli.MockUi)
   220  	c := &FmtCommand{
   221  		Meta: Meta{
   222  			testingOverrides: metaOverridesForProvider(testProvider()),
   223  			Ui:               ui,
   224  		},
   225  	}
   226  
   227  	args := []string{}
   228  	if code := c.Run(args); code != 0 {
   229  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   230  	}
   231  
   232  	expected := fmt.Sprintf("%s\n", fmtFixture.filename)
   233  	if actual := ui.OutputWriter.String(); actual != expected {
   234  		t.Fatalf("got: %q\nexpected: %q", actual, expected)
   235  	}
   236  }
   237  
   238  func TestFmt_directoryArg(t *testing.T) {
   239  	tempDir := fmtFixtureWriteDir(t)
   240  
   241  	ui := new(cli.MockUi)
   242  	c := &FmtCommand{
   243  		Meta: Meta{
   244  			testingOverrides: metaOverridesForProvider(testProvider()),
   245  			Ui:               ui,
   246  		},
   247  	}
   248  
   249  	args := []string{tempDir}
   250  	if code := c.Run(args); code != 0 {
   251  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   252  	}
   253  
   254  	got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
   255  	if err != nil {
   256  		t.Fatal(err)
   257  	}
   258  	want := filepath.Join(tempDir, fmtFixture.filename)
   259  
   260  	if got != want {
   261  		t.Fatalf("wrong output\ngot:  %s\nwant: %s", got, want)
   262  	}
   263  }
   264  
   265  func TestFmt_fileArg(t *testing.T) {
   266  	tempDir := fmtFixtureWriteDir(t)
   267  
   268  	ui := new(cli.MockUi)
   269  	c := &FmtCommand{
   270  		Meta: Meta{
   271  			testingOverrides: metaOverridesForProvider(testProvider()),
   272  			Ui:               ui,
   273  		},
   274  	}
   275  
   276  	args := []string{filepath.Join(tempDir, fmtFixture.filename)}
   277  	if code := c.Run(args); code != 0 {
   278  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   279  	}
   280  
   281  	got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
   282  	if err != nil {
   283  		t.Fatal(err)
   284  	}
   285  	want := filepath.Join(tempDir, fmtFixture.filename)
   286  
   287  	if got != want {
   288  		t.Fatalf("wrong output\ngot:  %s\nwant: %s", got, want)
   289  	}
   290  }
   291  
   292  func TestFmt_stdinArg(t *testing.T) {
   293  	input := new(bytes.Buffer)
   294  	input.Write(fmtFixture.input)
   295  
   296  	ui := new(cli.MockUi)
   297  	c := &FmtCommand{
   298  		Meta: Meta{
   299  			testingOverrides: metaOverridesForProvider(testProvider()),
   300  			Ui:               ui,
   301  		},
   302  		input: input,
   303  	}
   304  
   305  	args := []string{"-"}
   306  	if code := c.Run(args); code != 0 {
   307  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   308  	}
   309  
   310  	expected := fmtFixture.golden
   311  	if actual := ui.OutputWriter.Bytes(); !bytes.Equal(actual, expected) {
   312  		t.Fatalf("got: %q\nexpected: %q", actual, expected)
   313  	}
   314  }
   315  
   316  func TestFmt_nonDefaultOptions(t *testing.T) {
   317  	tempDir := fmtFixtureWriteDir(t)
   318  
   319  	ui := new(cli.MockUi)
   320  	c := &FmtCommand{
   321  		Meta: Meta{
   322  			testingOverrides: metaOverridesForProvider(testProvider()),
   323  			Ui:               ui,
   324  		},
   325  	}
   326  
   327  	args := []string{
   328  		"-list=false",
   329  		"-write=false",
   330  		"-diff",
   331  		tempDir,
   332  	}
   333  	if code := c.Run(args); code != 0 {
   334  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   335  	}
   336  
   337  	expected := fmt.Sprintf("-%s+%s", fmtFixture.input, fmtFixture.golden)
   338  	if actual := ui.OutputWriter.String(); !strings.Contains(actual, expected) {
   339  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
   340  	}
   341  }
   342  
   343  func TestFmt_check(t *testing.T) {
   344  	tempDir := fmtFixtureWriteDir(t)
   345  
   346  	ui := new(cli.MockUi)
   347  	c := &FmtCommand{
   348  		Meta: Meta{
   349  			testingOverrides: metaOverridesForProvider(testProvider()),
   350  			Ui:               ui,
   351  		},
   352  	}
   353  
   354  	args := []string{
   355  		"-check",
   356  		tempDir,
   357  	}
   358  	if code := c.Run(args); code != 3 {
   359  		t.Fatalf("wrong exit code. expected 3")
   360  	}
   361  
   362  	// Given that we give relative paths back to the user, normalize this temp
   363  	// dir so that we're comparing against a relative-ized (normalized) path
   364  	tempDir = c.normalizePath(tempDir)
   365  
   366  	if actual := ui.OutputWriter.String(); !strings.Contains(actual, tempDir) {
   367  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, tempDir)
   368  	}
   369  }
   370  
   371  func TestFmt_checkStdin(t *testing.T) {
   372  	input := new(bytes.Buffer)
   373  	input.Write(fmtFixture.input)
   374  
   375  	ui := new(cli.MockUi)
   376  	c := &FmtCommand{
   377  		Meta: Meta{
   378  			testingOverrides: metaOverridesForProvider(testProvider()),
   379  			Ui:               ui,
   380  		},
   381  		input: input,
   382  	}
   383  
   384  	args := []string{
   385  		"-check",
   386  		"-",
   387  	}
   388  	if code := c.Run(args); code != 3 {
   389  		t.Fatalf("wrong exit code. expected 3, got %d", code)
   390  	}
   391  
   392  	if ui.OutputWriter != nil {
   393  		t.Fatalf("expected no output, got: %q", ui.OutputWriter.String())
   394  	}
   395  }
   396  
   397  var fmtFixture = struct {
   398  	filename      string
   399  	input, golden []byte
   400  }{
   401  	"main.tf",
   402  	[]byte(`  foo  =  "bar"
   403  `),
   404  	[]byte(`foo = "bar"
   405  `),
   406  }
   407  
   408  func fmtFixtureWriteDir(t *testing.T) string {
   409  	dir := testTempDir(t)
   410  
   411  	err := ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
   412  	if err != nil {
   413  		t.Fatal(err)
   414  	}
   415  
   416  	return dir
   417  }