Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/cmd/preview-test/composable-schema-invalid-root.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "./composable-schema-user.zed"

definition resource {
relation and: user
permission viewer = and
}
6 changes: 6 additions & 0 deletions internal/cmd/preview-test/composable-schema-root.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "./composable-schema-user.zed"

definition resource {
relation user: user
permission view = user
}
1 change: 1 addition & 0 deletions internal/cmd/preview-test/composable-schema-user.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition user {}
2 changes: 1 addition & 1 deletion internal/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var schemaCompileCmd = &cobra.Command{
Write to stdout:
zed preview schema compile root.zed
Write to an output file:
zed preview schema compile --out compiled.zed
zed preview schema compile root.zed --out compiled.zed
`,
ValidArgsFunction: commands.FileExtensionCompletions("zed"),
RunE: schemaCompileCmdFunc,
Expand Down
69 changes: 69 additions & 0 deletions internal/cmd/preview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

zedtesting "github.com/authzed/zed/internal/testing"
)

func TestPreview(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
files []string
out string
expectErr string
expectStr string
}{
`file_not_found`: {
files: []string{
filepath.Join("preview-test", "nonexistent.zed"),
},
expectErr: `no such file or directory`,
},
`happy_path`: {
files: []string{
filepath.Join("preview-test", "composable-schema-root.zed"),
},
expectStr: `definition user {}

definition resource {
relation user: user
permission view = user
}
`,
},
`cannot_be_compiled_because_using_reserved_keyword`: {
files: []string{
filepath.Join("preview-test", "composable-schema-invalid-root.zed"),
},
expectErr: "line 4, column 12: Expected identifier, found token TokenTypeKeyword",
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
require := require.New(t)

tempOutFile := filepath.Join(t.TempDir(), "out.zed")
cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t,
zedtesting.StringFlag{FlagName: "out", FlagValue: tempOutFile})

err := schemaCompileCmdFunc(cmd, tc.files)
if tc.expectErr == "" {
require.NoError(err)
tempOutString, err := os.ReadFile(tempOutFile)
require.NoError(err)
require.Equal(tc.expectStr, string(tempOutString))
} else {
require.Error(err)
require.Contains(err.Error(), tc.expectErr)
}
})
}
}
Loading