2015-11-05 15:47:08 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2020-12-11 19:09:25 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2017-08-28 21:01:11 +02:00
|
|
|
"os"
|
2020-12-11 19:09:25 +01:00
|
|
|
"path"
|
2015-11-05 15:47:08 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-02-08 23:04:24 +01:00
|
|
|
|
2020-12-11 19:09:25 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-10-13 18:33:18 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2021-03-18 16:14:58 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2021-01-12 22:13:10 +01:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2015-11-05 15:47:08 +01:00
|
|
|
)
|
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
func setupTest(t *testing.T, fixturepath string, args ...string) (*terminal.TestOutput, int) {
|
|
|
|
view, done := testView(t)
|
2018-10-13 18:33:18 +02:00
|
|
|
p := testProvider()
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 22:13:10 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-13 18:33:18 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"network_interface": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
"name": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-13 18:33:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-11-05 15:47:08 +01:00
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: Meta{
|
2018-10-13 18:33:18 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
2021-03-18 16:14:58 +01:00
|
|
|
View: view,
|
2015-11-05 15:47:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
args = append(args, "-no-color")
|
2017-07-05 18:32:29 +02:00
|
|
|
args = append(args, testFixturePath(fixturepath))
|
2015-11-05 15:47:08 +01:00
|
|
|
|
|
|
|
code := c.Run(args)
|
2021-03-18 16:14:58 +01:00
|
|
|
return done(t), code
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2017-07-05 18:32:29 +02:00
|
|
|
|
2015-11-05 15:47:08 +01:00
|
|
|
func TestValidateCommand(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
if output, code := setupTest(t, "validate-valid"); code != 0 {
|
|
|
|
t.Fatalf("unexpected non-successful exit code %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 21:01:11 +02:00
|
|
|
func TestValidateCommandWithTfvarsFile(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty because this test
|
|
|
|
// requires scanning the current working directory by validate command.
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("validate-valid/with-tfvars-file"), td)
|
2017-08-28 21:01:11 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
view, done := testView(t)
|
2017-08-28 21:01:11 +02:00
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
2021-03-18 16:14:58 +01:00
|
|
|
View: view,
|
2017-08-28 21:01:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
2021-03-18 16:14:58 +01:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad %d\n\n%s", code, output.Stderr())
|
2017-08-28 21:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 15:47:08 +01:00
|
|
|
func TestValidateFailingCommand(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
if output, code := setupTest(t, "validate-invalid"); code != 1 {
|
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateFailingCommandMissingQuote(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_quote")
|
2015-11-05 15:47:08 +01:00
|
|
|
|
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2020-12-11 19:09:25 +01:00
|
|
|
wantError := "Error: Invalid reference"
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateFailingCommandMissingVariable(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_var")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2020-12-11 19:09:25 +01:00
|
|
|
wantError := "Error: Reference to undeclared input variable"
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameProviderMutipleTimesShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_providers")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError := "Error: Duplicate provider configuration"
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameModuleMultipleTimesShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_modules")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError := "Error: Duplicate module call"
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameResourceMultipleTimesShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/multiple_resources")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError := `Error: Duplicate resource "aws_instance" configuration`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutputWithoutValueShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/outputs")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2018-11-24 21:46:49 +01:00
|
|
|
wantError := `The argument "value" is required, but no definition was found.`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
}
|
2018-11-24 21:46:49 +01:00
|
|
|
wantError = `An argument named "values" is not expected here. Did you mean "value"?`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModuleWithIncorrectNameShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/incorrectmodulename")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError := `Error: Invalid module instance name`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError = `Error: Variables not allowed`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWronglyUsedInterpolationShouldFail(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/interpolation")
|
2015-11-05 15:47:08 +01:00
|
|
|
if code != 1 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
wantError := `Error: Variables not allowed`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
2019-06-18 23:58:48 +02:00
|
|
|
wantError = `A single static variable reference is required`
|
2021-03-18 16:14:58 +01:00
|
|
|
if !strings.Contains(output.Stderr(), wantError) {
|
|
|
|
t.Fatalf("Missing error string %q\n\n'%s'", wantError, output.Stderr())
|
2015-11-05 15:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-05 18:32:29 +02:00
|
|
|
|
|
|
|
func TestMissingDefinedVar(t *testing.T) {
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, "validate-invalid/missing_defined_var")
|
command: beginnings of new config loader in "terraform validate"
As part of some light reorganization of our commands, this new
implementation no longer does validation of variables and will thus avoid
the need to spin up a fully-valid context. Instead, its focus is on
validating the configuration itself, regardless of any variables, state,
etc.
This change anticipates us later adding a -validate-only flag to
"terraform plan" which will then take over the related use-case of
checking if a particular execution of Terraform is valid, _including_ the
state, variables, etc.
Although leaving variables out of validate feels pretty arbitrary today
while all of the variable sources are local anyway, we have plans to
allow per-workspace variables to be stored in the backend in future and
at that point it will no longer be possible to fully validate variables
without accessing the backend. The "terraform plan" command explicitly
requires access to the backend, while "terraform validate" is now
explicitly for local-only validation of a single module.
In a future commit this will be extended to do basic type checking of
the configuration based on provider schemas, etc.
2018-03-01 02:14:05 +01:00
|
|
|
// This is allowed because validate tests only that variables are referenced
|
|
|
|
// correctly, not that they all have defined values.
|
2017-07-05 18:32:29 +02:00
|
|
|
if code != 0 {
|
2021-03-18 16:14:58 +01:00
|
|
|
t.Fatalf("Should have passed: %d\n\n%s", code, output.Stderr())
|
2017-07-05 18:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 19:09:25 +01:00
|
|
|
|
|
|
|
func TestValidate_json(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{"validate-valid", true},
|
|
|
|
{"validate-invalid", false},
|
|
|
|
{"validate-invalid/missing_quote", false},
|
|
|
|
{"validate-invalid/missing_var", false},
|
|
|
|
{"validate-invalid/multiple_providers", false},
|
|
|
|
{"validate-invalid/multiple_modules", false},
|
|
|
|
{"validate-invalid/multiple_resources", false},
|
|
|
|
{"validate-invalid/outputs", false},
|
|
|
|
{"validate-invalid/incorrectmodulename", false},
|
|
|
|
{"validate-invalid/interpolation", false},
|
|
|
|
{"validate-invalid/missing_defined_var", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
|
|
|
var want, got map[string]interface{}
|
|
|
|
|
|
|
|
wantFile, err := os.Open(path.Join(testFixturePath(tc.path), "output.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open output file: %s", err)
|
|
|
|
}
|
|
|
|
defer wantFile.Close()
|
|
|
|
wantBytes, err := ioutil.ReadAll(wantFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read output file: %s", err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(wantBytes), &want)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal expected JSON: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
output, code := setupTest(t, tc.path, "-json")
|
2020-12-11 19:09:25 +01:00
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
gotString := output.Stdout()
|
2020-12-11 19:09:25 +01:00
|
|
|
err = json.Unmarshal([]byte(gotString), &got)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal actual JSON: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmp.Equal(got, want) {
|
|
|
|
t.Errorf("wrong output:\n %v\n", cmp.Diff(got, want))
|
|
|
|
t.Errorf("raw output:\n%s\n", gotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.valid && code != 0 {
|
|
|
|
t.Errorf("wrong exit code: want 0, got %d", code)
|
|
|
|
} else if !tc.valid && code != 1 {
|
|
|
|
t.Errorf("wrong exit code: want 1, got %d", code)
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:14:58 +01:00
|
|
|
if errorOutput := output.Stderr(); errorOutput != "" {
|
2020-12-11 19:09:25 +01:00
|
|
|
t.Errorf("unexpected error output:\n%s", errorOutput)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|