Skip to content

Commit

Permalink
fix: ftltest config: use ConfigPaths with envar fallback (#1543)
Browse files Browse the repository at this point in the history
Fixes #1502.

Adds tests to ensure we're correctly falling back to the config in
`FTL_CONFIG` and to `ftl-project.toml` in the gitroot.
  • Loading branch information
safeer authored May 21, 2024
1 parent 9601ba0 commit 0ed16e6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
11 changes: 7 additions & 4 deletions go-runtime/ftl/ftltest/ftltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -59,7 +60,8 @@ func Context(options ...Option) context.Context {
// WithProjectFiles loads config and secrets from a project file
//
// Takes a list of paths to project files. If multiple paths are provided, they are loaded in order, with later files taking precedence.
// If no paths are provided, the list is inferred from the FTL_CONFIG environment variable. If that is not found, an error is returned.
// If no paths are provided, the list is inferred from the FTL_CONFIG environment variable. If that is not found, the ftl-project.toml
// file in the git root is used. If ftl-project.toml is not found, no project files are loaded.
//
// To be used when setting up a context for a test:
// ctx := ftltest.Context(
Expand All @@ -73,10 +75,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)
Expand Down
12 changes: 12 additions & 0 deletions integration/testdata/go/wrapped/ftl-project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ftl-min-version = ""

[global]
[global.configuration]
config = "inline://ImJhemJheiI"
[global.secrets]
secret = "inline://ImJhemJheiI"

[executables]
ftl = ""

[commands]
51 changes: 46 additions & 5 deletions integration/testdata/go/wrapped/wrapped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -73,16 +114,16 @@ func TestWrapped(t *testing.T) {
configValue: "bar",
secretValue: "bar",
}, {
name: "WithProjectTomlFromEnvar",
name: "WithProjectTomlFromRoot",
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",
configValue: "bazbaz",
secretValue: "bazbaz",
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 0ed16e6

Please sign in to comment.