Skip to content

Commit 875a8d9

Browse files
committed
accept empty json object
1 parent 7e74ec5 commit 875a8d9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

plan.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222

2323
func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
2424
var contents io.Reader = bytes.NewReader(input.PlanJSON)
25-
if len(input.PlanJSON) == 0 {
25+
// Also accept `{}` as an empty plan. If this is stored in postgres or another json
26+
// type, then `{}` is the "empty" value.
27+
if len(input.PlanJSON) == 0 || bytes.Compare(input.PlanJSON, []byte("{}")) == 0 {
2628
if input.PlanJSONPath == "" {
2729
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil
2830
}

plan_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package preview_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/preview"
10+
"github.com/coder/preview/types"
11+
)
12+
13+
func TestPlanJSONHook(t *testing.T) {
14+
t.Parallel()
15+
16+
t.Run("Empty plan", func(t *testing.T) {
17+
dirFS := os.DirFS("testdata/static")
18+
_, diags := preview.Preview(t.Context(), preview.Input{
19+
PlanJSONPath: "",
20+
PlanJSON: []byte("{}"),
21+
ParameterValues: nil,
22+
Owner: types.WorkspaceOwner{},
23+
}, dirFS)
24+
require.False(t, diags.HasErrors())
25+
})
26+
}

0 commit comments

Comments
 (0)