From c3cb1810df1f6dba6a4f83e2cc235d8dd2d75f9a Mon Sep 17 00:00:00 2001 From: Safeer Jiwan Date: Mon, 20 May 2024 14:21:42 -0700 Subject: [PATCH] fix: ftltest config: use ConfigPaths with envar fallback --- go-runtime/ftl/ftltest/ftltest.go | 8 +-- .../testdata/go/wrapped/ftl-project.toml | 12 +++++ .../testdata/go/wrapped/wrapped_test.go | 51 +++++++++++++++++-- 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 integration/testdata/go/wrapped/ftl-project.toml diff --git a/go-runtime/ftl/ftltest/ftltest.go b/go-runtime/ftl/ftltest/ftltest.go index 8aad52e783..6ff8cdd661 100644 --- a/go-runtime/ftl/ftltest/ftltest.go +++ b/go-runtime/ftl/ftltest/ftltest.go @@ -16,6 +16,7 @@ import ( "github.com/TBD54566975/ftl/backend/schema" cf "github.com/TBD54566975/ftl/common/configuration" + pc "github.com/TBD54566975/ftl/common/projectconfig" "github.com/TBD54566975/ftl/go-runtime/ftl" "github.com/TBD54566975/ftl/go-runtime/ftl/reflection" "github.com/TBD54566975/ftl/internal/log" @@ -73,10 +74,11 @@ func WithProjectFiles(paths ...string) Option { var preprocessingErr error if len(paths) == 0 { envValue, ok := os.LookupEnv("FTL_CONFIG") - if !ok { - preprocessingErr = fmt.Errorf("loading project files: no path provided and FTL_CONFIG environment variable not set") + if ok { + paths = strings.Split(envValue, ",") + } else { + paths = pc.ConfigPaths(paths) } - paths = strings.Split(envValue, ",") } paths = slices.Map(paths, func(p string) string { path, err := filepath.Abs(p) diff --git a/integration/testdata/go/wrapped/ftl-project.toml b/integration/testdata/go/wrapped/ftl-project.toml new file mode 100644 index 0000000000..bb0a782f5f --- /dev/null +++ b/integration/testdata/go/wrapped/ftl-project.toml @@ -0,0 +1,12 @@ +ftl-min-version = "" + +[global] + [global.configuration] + config = "inline://ImJhemJheiI" + [global.secrets] + secret = "inline://ImJhemJheiI" + +[executables] + ftl = "" + +[commands] diff --git a/integration/testdata/go/wrapped/wrapped_test.go b/integration/testdata/go/wrapped/wrapped_test.go index 7874ea5f1a..f96608f743 100644 --- a/integration/testdata/go/wrapped/wrapped_test.go +++ b/integration/testdata/go/wrapped/wrapped_test.go @@ -13,13 +13,54 @@ import ( "github.com/alecthomas/assert/v2" ) -func TestWrapped(t *testing.T) { +func TestWrappedWithConfigEnvar(t *testing.T) { absProjectPath1, err := filepath.Abs("ftl-project-test-1.toml") assert.NoError(t, err) absProjectPath2, err := filepath.Abs("ftl-project-test-2.toml") assert.NoError(t, err) t.Setenv("FTL_CONFIG", fmt.Sprintf("%s,%s", absProjectPath1, absProjectPath2)) + for _, tt := range []struct { + name string + options []ftltest.Option + configValue string + secretValue string + expectedError ftl.Option[string] + }{ + { + name: "WithProjectTomlFromEnvar", + options: []ftltest.Option{ + ftltest.WithProjectFiles(), + ftltest.WithCallsAllowedWithinModule(), + ftltest.WhenVerb(time.Time, func(ctx context.Context, req time.TimeRequest) (time.TimeResponse, error) { + return time.TimeResponse{Time: stdtime.Date(2024, 1, 1, 0, 0, 0, 0, stdtime.UTC)}, nil + }), + }, + configValue: "foobar", + secretValue: "foobar", + }, + } { + t.Run(tt.name, func(t *testing.T) { + ctx := ftltest.Context( + tt.options..., + ) + myConfig.Get(ctx) + resp, err := Outer(ctx) + + if expected, ok := tt.expectedError.Get(); ok { + assert.EqualError(t, err, expected) + return + } else { + assert.NoError(t, err) + } + assert.Equal(t, "2024-01-01 00:00:00 +0000 UTC", resp.Output) + assert.Equal(t, tt.configValue, resp.Config) + assert.Equal(t, tt.secretValue, resp.Secret) + }) + } +} + +func TestWrapped(t *testing.T) { for _, tt := range []struct { name string options []ftltest.Option @@ -62,7 +103,7 @@ func TestWrapped(t *testing.T) { secretValue: "shhhhh", }, { - name: "WithProjectToml", + name: "WithProjectTomlSpecified", options: []ftltest.Option{ ftltest.WithProjectFiles("ftl-project-test-1.toml"), ftltest.WithCallsAllowedWithinModule(), @@ -73,7 +114,7 @@ func TestWrapped(t *testing.T) { configValue: "bar", secretValue: "bar", }, { - name: "WithProjectTomlFromEnvar", + name: "WithProjectTomlFromRoot", options: []ftltest.Option{ ftltest.WithProjectFiles(), ftltest.WithCallsAllowedWithinModule(), @@ -81,8 +122,8 @@ func TestWrapped(t *testing.T) { return time.TimeResponse{Time: stdtime.Date(2024, 1, 1, 0, 0, 0, 0, stdtime.UTC)}, nil }), }, - configValue: "foobar", - secretValue: "foobar", + configValue: "bazbaz", + secretValue: "bazbaz", }, } { t.Run(tt.name, func(t *testing.T) {