Repository navigation
chore: pull in lint rules from spicedb #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,9 +113,7 @@ func registerBackupCmd(rootCmd *cobra.Command) { | |
| Use: "redact <filename>", | ||
| Short: "Redact a backup file to remove sensitive information", | ||
| Args: commands.ValidationWrapper(cobra.ExactArgs(1)), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return backupRedactCmdFunc(cmd, args) | ||
| }, | ||
| RunE: backupRedactCmdFunc, | ||
| } | ||
|
|
||
| rootCmd.AddCommand(backupCmd) | ||
|
|
@@ -364,11 +362,12 @@ func backupCreateCmdFunc(cmd *cobra.Command, args []string) (err error) { | |
| Uint64("processed", relsProcessed). | ||
| Uint64("throughput", perSec(relsProcessed, time.Since(relationshipReadStart))). | ||
| Stringer("elapsed", time.Since(relationshipReadStart).Round(time.Second)) | ||
| if isCanceled(err) { | ||
| switch { | ||
| case isCanceled(err): | ||
| evt.Msg("backup canceled - resume by restarting the backup command") | ||
| } else if err != nil { | ||
| case err != nil: | ||
| evt.Msg("backup failed") | ||
| } else { | ||
| default: | ||
| evt.Msg("finished backup") | ||
| } | ||
| }() | ||
|
|
@@ -490,7 +489,7 @@ func encoderForNewBackup(cmd *cobra.Command, c client.Client, backupFile *os.Fil | |
| return nil, nil, fmt.Errorf("error reading schema: %w", err) | ||
| } | ||
| if schemaResp.ReadAt == nil { | ||
| return nil, nil, fmt.Errorf("`backup` is not supported on this version of SpiceDB") | ||
| return nil, nil, errors.New("`backup` is not supported on this version of SpiceDB") | ||
| } | ||
| schema := schemaResp.SchemaText | ||
|
|
||
|
|
@@ -550,17 +549,20 @@ func openProgressFile(backupFileName string, backupAlreadyExisted bool) (*os.Fil | |
| // if a backup existed | ||
| var fileMode int | ||
| readCursor, err := os.ReadFile(progressFileName) | ||
| if backupAlreadyExisted && (os.IsNotExist(err) || len(readCursor) == 0) { | ||
| return nil, nil, fmt.Errorf("backup file %s already exists", backupFileName) | ||
| } else if backupAlreadyExisted && err == nil { | ||
| cursor = &v1.Cursor{ | ||
| Token: string(readCursor), | ||
| if backupAlreadyExisted { | ||
| if os.IsNotExist(err) || len(readCursor) == 0 { | ||
| return nil, nil, fmt.Errorf("backup file %s already exists", backupFileName) | ||
|
Comment on lines
+552
to
+554
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bit of renesting in favor of implementing the same logic with a |
||
| } | ||
| if err == nil { | ||
| cursor = &v1.Cursor{ | ||
| Token: string(readCursor), | ||
| } | ||
|
|
||
| // if backup existed and there is a progress marker, the latter should not be truncated to make sure the | ||
| // cursor stays around in case of a failure before we even start ingesting from bulk export | ||
| fileMode = os.O_WRONLY | os.O_CREATE | ||
| log.Info().Str("filename", backupFileName).Msg("backup file already exists, will resume") | ||
| // if backup existed and there is a progress marker, the latter should not be truncated to make sure the | ||
| // cursor stays around in case of a failure before we even start ingesting from bulk export | ||
| fileMode = os.O_WRONLY | os.O_CREATE | ||
| log.Info().Str("filename", backupFileName).Msg("backup file already exists, will resume") | ||
| } | ||
| } else { | ||
| // if a backup did not exist, make sure to truncate the progress file | ||
| fileMode = os.O_WRONLY | os.O_CREATE | os.O_TRUNC | ||
|
|
@@ -733,7 +735,7 @@ func backupParseRevisionCmdFunc(_ *cobra.Command, out io.Writer, args []string) | |
|
|
||
| loadedToken := decoder.ZedToken() | ||
| if loadedToken == nil { | ||
| return fmt.Errorf("failed to parse decoded revision") | ||
| return errors.New("failed to parse decoded revision") | ||
| } | ||
|
|
||
| _, err = fmt.Fprintln(out, loadedToken.Token) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" | ||
|
|
@@ -29,7 +30,7 @@ func TestImportCmdHappyPath(t *testing.T) { | |
| ctx := t.Context() | ||
| srv := zedtesting.NewTestServer(ctx, t) | ||
| go func() { | ||
| require.NoError(srv.Run(ctx)) | ||
| assert.NoError(t, srv.Run(ctx)) | ||
|
Comment on lines
-32
to
+33
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is working around the "no require in goroutines" issue |
||
| }() | ||
| conn, err := srv.GRPCDialContext(ctx) | ||
| require.NoError(err) | ||
|
|
@@ -57,5 +58,5 @@ func TestImportCmdHappyPath(t *testing.T) { | |
| Resource: &v1.ObjectReference{ObjectType: "resource", ObjectId: "1"}, | ||
| }) | ||
| require.NoError(err) | ||
| require.Equal(resp.Permissionship, v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION) | ||
| require.Equal(v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION, resp.Permissionship) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,65 +128,69 @@ caveat test/some_caveat(someCondition int) { | |
|
|
||
| func TestSchemaCompile(t *testing.T) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored these to be three separate tests because 1. you can't really use |
||
| t.Parallel() | ||
| require := require.New(t) | ||
|
|
||
| testCases := map[string]struct { | ||
| files []string | ||
| out string | ||
| expectErr error | ||
| expectStr string | ||
| }{ | ||
| `file_not_found`: { | ||
| files: []string{ | ||
| filepath.Join("preview-test", "nonexistent.zed"), | ||
| }, | ||
| expectErr: fs.ErrNotExist, | ||
| }, | ||
| `happy_path`: { | ||
| files: []string{ | ||
| filepath.Join("preview-test", "composable-schema-root.zed"), | ||
| }, | ||
| expectStr: `definition user {} | ||
| files := []string{filepath.Join("preview-test", "composable-schema-root.zed")} | ||
| expected := `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: compiler.BaseCompilerError{}, | ||
| }, | ||
| } | ||
| ` | ||
|
|
||
| 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}) | ||
|
|
||
| tempOutFile := filepath.Join(t.TempDir(), "out.zed") | ||
| cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t, | ||
| zedtesting.StringFlag{FlagName: "out", FlagValue: tempOutFile}) | ||
|
|
||
| mockTermCheckerr := &mockTermChecker{returnVal: false} | ||
| err := schemaCompileCmdFunc(cmd, tc.files, mockTermCheckerr) | ||
| if tc.expectErr == nil { | ||
| require.NoError(err) | ||
| tempOutString, err := os.ReadFile(tempOutFile) | ||
| require.NoError(err) | ||
| require.Equal(tc.expectStr, string(tempOutString)) | ||
| // TODO re-enable after adding a test that uses stdout | ||
| // require.Equal(int(os.Stdout.Fd()), mockTermCheckerr.capturedFd, "expected stdout to be checked for terminal") | ||
| } else { | ||
| require.Error(err) | ||
| require.ErrorAs(err, &tc.expectErr) | ||
| } | ||
| }) | ||
| } | ||
| mockTermCheckerr := &mockTermChecker{returnVal: false} | ||
| err := schemaCompileCmdFunc(cmd, files, mockTermCheckerr) | ||
|
|
||
| require.NoError(err) | ||
| tempOutString, err := os.ReadFile(tempOutFile) | ||
| require.NoError(err) | ||
| require.Equal(expected, string(tempOutString)) | ||
| // TODO re-enable after adding a test that uses stdout | ||
| // require.Equal(int(os.Stdout.Fd()), mockTermCheckerr.capturedFd, "expected stdout to be checked for terminal") | ||
| } | ||
|
|
||
| func TestSchemaCompileFileNotFound(t *testing.T) { | ||
| t.Parallel() | ||
| require := require.New(t) | ||
|
|
||
| files := []string{filepath.Join("preview-test", "nonexistent.zed")} | ||
|
|
||
| tempOutFile := filepath.Join(t.TempDir(), "out.zed") | ||
| cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t, | ||
| zedtesting.StringFlag{FlagName: "out", FlagValue: tempOutFile}) | ||
|
|
||
| mockTermCheckerr := &mockTermChecker{returnVal: false} | ||
| err := schemaCompileCmdFunc(cmd, files, mockTermCheckerr) | ||
| require.Error(err) | ||
| require.ErrorIs(err, fs.ErrNotExist) | ||
| } | ||
|
|
||
| func TestSchemaCompileFailureFromReservedKeyword(t *testing.T) { | ||
| t.Parallel() | ||
| require := require.New(t) | ||
|
|
||
| files := []string{filepath.Join("preview-test", "composable-schema-invalid-root.zed")} | ||
| var expectedErr compiler.BaseCompilerError | ||
|
|
||
| tempOutFile := filepath.Join(t.TempDir(), "out.zed") | ||
| cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t, | ||
| zedtesting.StringFlag{FlagName: "out", FlagValue: tempOutFile}) | ||
|
|
||
| mockTermCheckerr := &mockTermChecker{returnVal: false} | ||
| err := schemaCompileCmdFunc(cmd, files, mockTermCheckerr) | ||
| require.Error(err) | ||
| require.ErrorAs(err, &expectedErr) | ||
| } | ||
|
|
||
| // TODO: refactor the impl function to provide a pipe or buffer directly and delegate the input selection to | ||
| // another function | ||
| // | ||
| //nolint:tparallel // these tests can't be parallel because they muck around with the definition of os.Stdin. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but you kept the |
||
| func TestSchemaWrite(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are all ported over relatively directly from SpiceDb.