From 9f64d4856a38372d22aae7d6684b16bda00972bd Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Fri, 30 Mar 2018 10:55:21 -0400 Subject: [PATCH] Update unit-tests to use requirements instead of manual checks. --- command/validate/validate_test.go | 74 +++++++++++-------------------- 1 file changed, 26 insertions(+), 48 deletions(-) diff --git a/command/validate/validate_test.go b/command/validate/validate_test.go index f6bd6717ac..27126d7877 100644 --- a/command/validate/validate_test.go +++ b/command/validate/validate_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + require "github.com/stretchr/testify/require" "github.com/hashicorp/consul/testutil" "github.com/mitchellh/cli" ) @@ -26,9 +27,8 @@ func TestValidateCommand_FailOnEmptyFile(t *testing.T) { cmd := New(cli.NewMockUi()) args := []string{tmpFile.Name()} - if code := cmd.Run(args); code == 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.NotEqualf(t, 0, code, "bad: %d", code) } func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) { @@ -38,16 +38,13 @@ func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) { fp := filepath.Join(td, "config.json") err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) cmd := New(cli.NewMockUi()) args := []string{fp} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d", code) } func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t *testing.T) { @@ -57,16 +54,13 @@ func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t *testing.T) { fp := filepath.Join(td, "json.conf") err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) cmd := New(cli.NewMockUi()) args := []string{"--config-format", "json", fp} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d", code) } func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) { @@ -76,16 +70,14 @@ func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) { fp := filepath.Join(td, "hcl.conf") err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) + cmd := New(cli.NewMockUi()) args := []string{"--config-format", "hcl", fp} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d", code) } func TestValidateCommand_SucceedWithJSONAsHCL(t *testing.T) { @@ -95,16 +87,13 @@ func TestValidateCommand_SucceedWithJSONAsHCL(t *testing.T) { fp := filepath.Join(td, "json.conf") err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) cmd := New(cli.NewMockUi()) args := []string{"--config-format", "hcl", fp} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d", code) } func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) { @@ -113,16 +102,13 @@ func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) { defer os.RemoveAll(td) err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) cmd := New(cli.NewMockUi()) args := []string{td} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d", code) } func TestValidateCommand_FailForInvalidJSONConfigFormat(t *testing.T) { @@ -132,16 +118,13 @@ func TestValidateCommand_FailForInvalidJSONConfigFormat(t *testing.T) { fp := filepath.Join(td, "hcl.conf") err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) cmd := New(cli.NewMockUi()) args := []string{"--config-format", "json", fp} - if code := cmd.Run(args); code == 0 { - t.Fatalf("bad: %d", code) - } + code := cmd.Run(args) + require.NotEqualf(t, 0, code, "bad: %d", code) } func TestValidateCommand_Quiet(t *testing.T) { @@ -151,18 +134,13 @@ func TestValidateCommand_Quiet(t *testing.T) { fp := filepath.Join(td, "config.json") err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) - if err != nil { - t.Fatalf("err: %s", err) - } + require.Nilf(t, err, "err: %s", err) ui := cli.NewMockUi() cmd := New(ui) args := []string{"-quiet", td} - if code := cmd.Run(args); code != 0 { - t.Fatalf("bad: %d, %s", code, ui.ErrorWriter.String()) - } - if ui.OutputWriter.String() != "" { - t.Fatalf("bad: %v", ui.OutputWriter.String()) - } + code := cmd.Run(args) + require.Equalf(t, 0, code, "bad: %d, %s", code, ui.ErrorWriter.String()) + require.Equalf(t, "", ui.OutputWriter.String(), "bad: %v", ui.OutputWriter.String()) }