diff --git a/README.md b/README.md index a6fff18..9326405 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ The SDK provides the following APIs: - `SchemaApply`: runs the "atlas schema apply" command. - `SchemaInspect`: runs the "atlas schema inspect" command. - `SchemaDiff`: runs the "atlas schema diff" command. +- `SchemaTest`: runs the "atlas schema test" command. Example with `MigrateApply` API: diff --git a/atlasexec/atlas.go b/atlasexec/atlas.go index cf8c455..93cee0e 100644 --- a/atlasexec/atlas.go +++ b/atlasexec/atlas.go @@ -150,6 +150,15 @@ type ( URL string Vars Vars } + // SchemaTestParams are the parameters for the `schema test` command. + SchemaTestParams struct { + Env string + ConfigURL string + URL string + DevURL string + Run string + Vars Vars + } Vars map[string]string ) @@ -461,6 +470,28 @@ func (c *Client) SchemaInspect(ctx context.Context, params *SchemaInspectParams) return stringVal(c.runCommand(ctx, args)) } +// SchemaTest runs the 'schema test' command. +func (c *Client) SchemaTest(ctx context.Context, params *SchemaTestParams) (string, error) { + args := []string{"schema", "test"} + if params.Env != "" { + args = append(args, "--env", params.Env) + } + if params.ConfigURL != "" { + args = append(args, "--config", params.ConfigURL) + } + if params.URL != "" { + args = append(args, "--url", params.URL) + } + if params.DevURL != "" { + args = append(args, "--dev-url", params.DevURL) + } + if params.Run != "" { + args = append(args, "--run", params.Run) + } + args = append(args, params.Vars.AsArgs()...) + return stringVal(c.runCommand(ctx, args)) +} + func lintArgs(params *MigrateLintParams) ([]string, error) { args := []string{"migrate", "lint"} if params.Web { diff --git a/atlasexec/atlas_test.go b/atlasexec/atlas_test.go index ae78c9a..252c92d 100644 --- a/atlasexec/atlas_test.go +++ b/atlasexec/atlas_test.go @@ -1000,3 +1000,61 @@ func TestMigrateTest(t *testing.T) { }) } } + +func TestSchemaTest(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + // Mock the client with a script that just prints the arguments to stderr and + // exit with an error code. + c, err := atlasexec.NewClient(t.TempDir(), filepath.Join(wd, "./mock-args.sh")) + require.NoError(t, err) + + for _, tt := range []struct { + name string + params *atlasexec.SchemaTestParams + expect string + }{ + { + name: "no params", + params: &atlasexec.SchemaTestParams{}, + expect: "schema test", + }, + { + name: "with env", + params: &atlasexec.SchemaTestParams{ + Env: "test", + }, + expect: "schema test --env test", + }, + { + name: "with config", + params: &atlasexec.SchemaTestParams{ + ConfigURL: "file://config.hcl", + }, + expect: "schema test --config file://config.hcl", + }, + { + name: "with dev-url", + params: &atlasexec.SchemaTestParams{ + DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory", + }, + expect: "schema test --dev-url sqlite://file?_fk=1&cache=shared&mode=memory", + }, + { + name: "with run", + params: &atlasexec.SchemaTestParams{ + Run: "example", + }, + expect: "schema test --run example", + }, + } { + t.Run(tt.name, func(t *testing.T) { + _, err := c.SchemaTest(context.Background(), tt.params) + require.Error(t, err) + // The script mock-args.sh exit with an error code. + // So, our atlasexec.SchemaTest should return a Error. + // Which contains all output from the script (both stdout and stderr). + require.Equal(t, tt.expect, err.Error()) + }) + } +}