github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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/mitchellh/cli"
    13  )
    14  
    15  func TestFmt_errorReporting(t *testing.T) {
    16  	tempDir, err := fmtFixtureWriteDir()
    17  	if err != nil {
    18  		t.Fatalf("err: %s", err)
    19  	}
    20  	defer os.RemoveAll(tempDir)
    21  
    22  	ui := new(cli.MockUi)
    23  	c := &FmtCommand{
    24  		Meta: Meta{
    25  			testingOverrides: metaOverridesForProvider(testProvider()),
    26  			Ui:               ui,
    27  		},
    28  	}
    29  
    30  	dummy_file := filepath.Join(tempDir, "doesnotexist")
    31  	args := []string{dummy_file}
    32  	if code := c.Run(args); code != 2 {
    33  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
    34  	}
    35  
    36  	expected := fmt.Sprintf("Error running fmt: stat %s: no such file or directory", dummy_file)
    37  	if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
    38  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
    39  	}
    40  }
    41  
    42  func TestFmt_tooManyArgs(t *testing.T) {
    43  	ui := new(cli.MockUi)
    44  	c := &FmtCommand{
    45  		Meta: Meta{
    46  			testingOverrides: metaOverridesForProvider(testProvider()),
    47  			Ui:               ui,
    48  		},
    49  	}
    50  
    51  	args := []string{
    52  		"one",
    53  		"two",
    54  	}
    55  	if code := c.Run(args); code != 1 {
    56  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
    57  	}
    58  
    59  	expected := "The fmt command expects at most one argument."
    60  	if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
    61  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
    62  	}
    63  }
    64  
    65  func TestFmt_workingDirectory(t *testing.T) {
    66  	tempDir, err := fmtFixtureWriteDir()
    67  	if err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  	defer os.RemoveAll(tempDir)
    71  
    72  	cwd, err := os.Getwd()
    73  	if err != nil {
    74  		t.Fatalf("err: %s", err)
    75  	}
    76  	err = os.Chdir(tempDir)
    77  	if err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  	defer os.Chdir(cwd)
    81  
    82  	ui := new(cli.MockUi)
    83  	c := &FmtCommand{
    84  		Meta: Meta{
    85  			testingOverrides: metaOverridesForProvider(testProvider()),
    86  			Ui:               ui,
    87  		},
    88  	}
    89  
    90  	args := []string{}
    91  	if code := c.Run(args); code != 0 {
    92  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
    93  	}
    94  
    95  	expected := fmt.Sprintf("%s\n", fmtFixture.filename)
    96  	if actual := ui.OutputWriter.String(); actual != expected {
    97  		t.Fatalf("got: %q\nexpected: %q", actual, expected)
    98  	}
    99  }
   100  
   101  func TestFmt_directoryArg(t *testing.T) {
   102  	tempDir, err := fmtFixtureWriteDir()
   103  	if err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  	defer os.RemoveAll(tempDir)
   107  
   108  	ui := new(cli.MockUi)
   109  	c := &FmtCommand{
   110  		Meta: Meta{
   111  			testingOverrides: metaOverridesForProvider(testProvider()),
   112  			Ui:               ui,
   113  		},
   114  	}
   115  
   116  	args := []string{tempDir}
   117  	if code := c.Run(args); code != 0 {
   118  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   119  	}
   120  
   121  	expected := fmt.Sprintf("%s\n", filepath.Join(tempDir, fmtFixture.filename))
   122  	if actual := ui.OutputWriter.String(); actual != expected {
   123  		t.Fatalf("got: %q\nexpected: %q", actual, expected)
   124  	}
   125  }
   126  
   127  func TestFmt_stdinArg(t *testing.T) {
   128  	input := new(bytes.Buffer)
   129  	input.Write(fmtFixture.input)
   130  
   131  	ui := new(cli.MockUi)
   132  	c := &FmtCommand{
   133  		Meta: Meta{
   134  			testingOverrides: metaOverridesForProvider(testProvider()),
   135  			Ui:               ui,
   136  		},
   137  		input: input,
   138  	}
   139  
   140  	args := []string{"-"}
   141  	if code := c.Run(args); code != 0 {
   142  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   143  	}
   144  
   145  	expected := fmtFixture.golden
   146  	if actual := ui.OutputWriter.Bytes(); !bytes.Equal(actual, expected) {
   147  		t.Fatalf("got: %q\nexpected: %q", actual, expected)
   148  	}
   149  }
   150  
   151  func TestFmt_nonDefaultOptions(t *testing.T) {
   152  	tempDir, err := fmtFixtureWriteDir()
   153  	if err != nil {
   154  		t.Fatalf("err: %s", err)
   155  	}
   156  	defer os.RemoveAll(tempDir)
   157  
   158  	ui := new(cli.MockUi)
   159  	c := &FmtCommand{
   160  		Meta: Meta{
   161  			testingOverrides: metaOverridesForProvider(testProvider()),
   162  			Ui:               ui,
   163  		},
   164  	}
   165  
   166  	args := []string{
   167  		"-list=false",
   168  		"-write=false",
   169  		"-diff",
   170  		tempDir,
   171  	}
   172  	if code := c.Run(args); code != 0 {
   173  		t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
   174  	}
   175  
   176  	expected := fmt.Sprintf("-%s+%s", fmtFixture.input, fmtFixture.golden)
   177  	if actual := ui.OutputWriter.String(); !strings.Contains(actual, expected) {
   178  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
   179  	}
   180  }
   181  
   182  func TestFmt_check(t *testing.T) {
   183  	tempDir, err := fmtFixtureWriteDir()
   184  	if err != nil {
   185  		t.Fatalf("err: %s", err)
   186  	}
   187  	defer os.RemoveAll(tempDir)
   188  
   189  	ui := new(cli.MockUi)
   190  	c := &FmtCommand{
   191  		Meta: Meta{
   192  			testingOverrides: metaOverridesForProvider(testProvider()),
   193  			Ui:               ui,
   194  		},
   195  	}
   196  
   197  	args := []string{
   198  		"-check",
   199  		tempDir,
   200  	}
   201  	if code := c.Run(args); code != 3 {
   202  		t.Fatalf("wrong exit code. expected 3")
   203  	}
   204  
   205  	if actual := ui.OutputWriter.String(); !strings.Contains(actual, tempDir) {
   206  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, tempDir)
   207  	}
   208  }
   209  
   210  func TestFmt_checkStdin(t *testing.T) {
   211  	input := new(bytes.Buffer)
   212  	input.Write(fmtFixture.input)
   213  
   214  	ui := new(cli.MockUi)
   215  	c := &FmtCommand{
   216  		Meta: Meta{
   217  			testingOverrides: metaOverridesForProvider(testProvider()),
   218  			Ui:               ui,
   219  		},
   220  		input: input,
   221  	}
   222  
   223  	args := []string{
   224  		"-check",
   225  		"-",
   226  	}
   227  	if code := c.Run(args); code != 3 {
   228  		t.Fatalf("wrong exit code. expected 3, got %d", code)
   229  	}
   230  
   231  	if ui.OutputWriter != nil {
   232  		t.Fatalf("expected no output, got: %q", ui.OutputWriter.String())
   233  	}
   234  }
   235  
   236  var fmtFixture = struct {
   237  	filename      string
   238  	input, golden []byte
   239  }{
   240  	"main.tf",
   241  	[]byte(`  foo  =  "bar"
   242  `),
   243  	[]byte(`foo = "bar"
   244  `),
   245  }
   246  
   247  func fmtFixtureWriteDir() (string, error) {
   248  	dir, err := ioutil.TempDir("", "tf")
   249  	if err != nil {
   250  		return "", err
   251  	}
   252  
   253  	err = ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644)
   254  	if err != nil {
   255  		os.RemoveAll(dir)
   256  		return "", err
   257  	}
   258  
   259  	return dir, nil
   260  }