modify test case

This commit is contained in:
magodo 2021-07-26 11:25:47 +08:00
parent 67afee693b
commit 90e6a3dffb
No known key found for this signature in database
GPG Key ID: 2A3C3526EE99038E
1 changed files with 66 additions and 1 deletions

View File

@ -103,6 +103,45 @@ func TestAdd_basic(t *testing.T) {
}
})
t.Run("basic to existing file", func(t *testing.T) {
view, done := testView(t)
c := &AddCommand{
Meta: Meta{
testingOverrides: overrides,
View: view,
},
}
outPath := "add.tf"
args := []string{fmt.Sprintf("-out=%s", outPath), "test_instance.new"}
c.Run(args)
args = []string{fmt.Sprintf("-out=%s", outPath), "test_instance.new2"}
code := c.Run(args)
output := done(t)
if code != 0 {
fmt.Println(output.Stderr())
t.Fatalf("wrong exit status. Got %d, want 0", code)
}
expected := `resource "test_instance" "new" {
value = null # REQUIRED string
}
resource "test_instance" "new2" {
value = null # REQUIRED string
}
`
result, err := os.ReadFile(outPath)
if err != nil {
t.Fatalf("error reading result file %s: %s", outPath, err.Error())
}
// While the entire directory will get removed once the whole test suite
// is done, we remove this lest it gets in the way of another (not yet
// written) test.
os.Remove(outPath)
if !cmp.Equal(expected, string(result)) {
t.Fatalf("wrong output:\n%s", cmp.Diff(expected, string(result)))
}
})
t.Run("optionals", func(t *testing.T) {
view, done := testView(t)
c := &AddCommand{
@ -164,7 +203,8 @@ func TestAdd_basic(t *testing.T) {
View: view,
},
}
args := []string{"test_instance.exists"}
outPath := "add.tf"
args := []string{fmt.Sprintf("-out=%s", outPath), "test_instance.exists"}
code := c.Run(args)
if code != 1 {
t.Fatalf("wrong exit status. Got %d, want 0", code)
@ -176,6 +216,31 @@ func TestAdd_basic(t *testing.T) {
}
})
t.Run("output existing resource to stdout", func(t *testing.T) {
view, done := testView(t)
c := &AddCommand{
Meta: Meta{
testingOverrides: overrides,
View: view,
},
}
args := []string{"test_instance.exists"}
code := c.Run(args)
output := done(t)
if code != 0 {
fmt.Println(output.Stderr())
t.Fatalf("wrong exit status. Got %d, want 0", code)
}
expected := `resource "test_instance" "exists" {
value = null # REQUIRED string
}
`
if !cmp.Equal(output.Stdout(), expected) {
t.Fatalf("wrong output:\n%s", cmp.Diff(expected, output.Stdout()))
}
})
t.Run("provider not in configuration", func(t *testing.T) {
view, done := testView(t)
c := &AddCommand{