Skip to content

Commit 1070314

Browse files
committed
Config stores env.Env, mostly for DumpEnv
1 parent b0067a5 commit 1070314

5 files changed

Lines changed: 45 additions & 36 deletions

File tree

internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type Config struct {
3838
Branch string
3939
// JobRetryCount is the count of the number of times the job has been retried.
4040
JobRetryCount int
41+
// Env provides access to environment variables.
42+
// It's public because many tests in other packages reference it (perhaps they should not).
43+
Env env.Env
4144
// errs is a map of environment variables name and the validation errors associated with them.
4245
errs InvalidConfigError
4346
}
@@ -46,6 +49,7 @@ type Config struct {
4649
// It returns Config struct and an InvalidConfigError if there is an invalid configuration.
4750
func New(env env.Env) (Config, error) {
4851
c := Config{
52+
Env: env,
4953
errs: InvalidConfigError{},
5054
}
5155

internal/config/config_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func getExampleEnv() env.Env {
2727
}
2828

2929
func TestNewConfig(t *testing.T) {
30-
c, err := New(getExampleEnv())
30+
env := getExampleEnv()
31+
32+
c, err := New(env)
3133
if err != nil {
3234
t.Errorf("config.New() error = %v", err)
3335
}
@@ -44,6 +46,7 @@ func TestNewConfig(t *testing.T) {
4446
SuiteSlug: "my_suite",
4547
TestRunner: "rspec",
4648
JobRetryCount: 0,
49+
Env: env,
4750
errs: InvalidConfigError{},
4851
}
4952

@@ -82,6 +85,7 @@ func TestNewConfig_MissingConfigWithDefault(t *testing.T) {
8285
TestRunner: "rspec",
8386
ResultPath: "tmp/rspec.json",
8487
JobRetryCount: 0,
88+
Env: env,
8589
}
8690

8791
if diff := cmp.Diff(c, want, cmpopts.IgnoreUnexported(Config{})); diff != "" {

internal/config/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func getIntEnvWithDefault(env env.Env, key string, defaultValue int) (int, error
3535
return valueInt, nil
3636
}
3737

38-
func (c Config) DumpEnv(env env.Env) map[string]string {
38+
func (c Config) DumpEnv() map[string]string {
3939
keys := []string{
4040
"BUILDKITE_BUILD_ID",
4141
"BUILDKITE_JOB_ID",
@@ -58,7 +58,7 @@ func (c Config) DumpEnv(env env.Env) map[string]string {
5858

5959
envs := make(map[string]string)
6060
for _, key := range keys {
61-
envs[key] = env.Get(key)
61+
envs[key] = c.Env.Get(key)
6262
}
6363

6464
envs["BUILDKITE_TEST_ENGINE_IDENTIFIER"] = c.Identifier

main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func main() {
8686
OrganizationSlug: cfg.OrganizationSlug,
8787
})
8888

89-
testPlan, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, env, files, testRunner)
89+
testPlan, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, files, testRunner)
9090
if err != nil {
9191
logErrorAndExit(16, "Couldn't fetch or create test plan: %v", err)
9292
}
@@ -120,7 +120,7 @@ func main() {
120120
// At this point, the runner is expected to have completed
121121

122122
if !testPlan.Fallback {
123-
sendMetadata(ctx, apiClient, cfg, env, timeline, runResult.Statistics())
123+
sendMetadata(ctx, apiClient, cfg, timeline, runResult.Statistics())
124124
}
125125

126126
printReport(runResult, testPlan.SkippedTests, testRunner.Name())
@@ -204,10 +204,10 @@ func createTimestamp() string {
204204
return time.Now().Format(time.RFC3339Nano)
205205
}
206206

207-
func sendMetadata(ctx context.Context, apiClient *api.Client, cfg config.Config, env env.Env, timeline []api.Timeline, statistics runner.RunStatistics) {
207+
func sendMetadata(ctx context.Context, apiClient *api.Client, cfg config.Config, timeline []api.Timeline, statistics runner.RunStatistics) {
208208
err := apiClient.PostTestPlanMetadata(ctx, cfg.SuiteSlug, cfg.Identifier, api.TestPlanMetadataParams{
209209
Timeline: timeline,
210-
Env: cfg.DumpEnv(env),
210+
Env: cfg.DumpEnv(),
211211
Version: version.Version,
212212
Statistics: statistics,
213213
})
@@ -297,7 +297,7 @@ func logErrorAndExit(exitCode int, format string, v ...any) {
297297

298298
// fetchOrCreateTestPlan fetches a test plan from the server, or creates a
299299
// fallback plan if the server is unavailable or returns an error plan.
300-
func fetchOrCreateTestPlan(ctx context.Context, apiClient *api.Client, cfg config.Config, env env.Env, files []string, testRunner TestRunner) (plan.TestPlan, error) {
300+
func fetchOrCreateTestPlan(ctx context.Context, apiClient *api.Client, cfg config.Config, files []string, testRunner TestRunner) (plan.TestPlan, error) {
301301
debug.Println("Fetching test plan")
302302

303303
// Fetch the plan from the server's cache.
@@ -339,7 +339,7 @@ func fetchOrCreateTestPlan(ctx context.Context, apiClient *api.Client, cfg confi
339339

340340
debug.Println("No test plan found, creating a new plan")
341341
// If the cache is empty, create a new plan.
342-
params, err := createRequestParam(ctx, cfg, env, files, *apiClient, testRunner)
342+
params, err := createRequestParam(ctx, cfg, files, *apiClient, testRunner)
343343
if err != nil {
344344
return handleError(err)
345345
}
@@ -366,7 +366,7 @@ func fetchOrCreateTestPlan(ctx context.Context, apiClient *api.Client, cfg confi
366366
// createRequestParam generates the parameters needed for a test plan request.
367367
// For runners other than "rspec", it constructs the test plan parameters with all test files.
368368
// For the "rspec" runner, it filters the test files through the Test Engine API and splits the filtered files into examples.
369-
func createRequestParam(ctx context.Context, cfg config.Config, env env.Env, files []string, client api.Client, runner TestRunner) (api.TestPlanParams, error) {
369+
func createRequestParam(ctx context.Context, cfg config.Config, files []string, client api.Client, runner TestRunner) (api.TestPlanParams, error) {
370370
testFiles := []plan.TestCase{}
371371
for _, file := range files {
372372
testFiles = append(testFiles, plan.TestCase{
@@ -394,7 +394,7 @@ func createRequestParam(ctx context.Context, cfg config.Config, env env.Env, fil
394394
// The SplitByExample flag indicates whether to filter slow files for splitting by example.
395395
// Regardless of the flag's state, the API will still filter other files that need to be split by example, such as those containing skipped tests.
396396
// Therefore, we must filter and split files even when SplitByExample is disabled.
397-
testParams, err := filterAndSplitFiles(ctx, cfg, env, client, testFiles, runner)
397+
testParams, err := filterAndSplitFiles(ctx, cfg, client, testFiles, runner)
398398
if err != nil {
399399
return api.TestPlanParams{}, err
400400
}
@@ -411,12 +411,12 @@ func createRequestParam(ctx context.Context, cfg config.Config, env env.Env, fil
411411
// filterAndSplitFiles filters the test files through the Test Engine API and splits the filtered files into examples.
412412
// It returns the test plan parameters with the examples from the filtered files and the remaining files.
413413
// An error is returned if there is a failure in any of the process.
414-
func filterAndSplitFiles(ctx context.Context, cfg config.Config, env env.Env, client api.Client, files []plan.TestCase, runner TestRunner) (api.TestPlanParamsTest, error) {
414+
func filterAndSplitFiles(ctx context.Context, cfg config.Config, client api.Client, files []plan.TestCase, runner TestRunner) (api.TestPlanParamsTest, error) {
415415
// Filter files that need to be split.
416416
debug.Printf("Filtering %d files", len(files))
417417
filteredFiles, err := client.FilterTests(ctx, cfg.SuiteSlug, api.FilterTestsParams{
418418
Files: files,
419-
Env: cfg.DumpEnv(env),
419+
Env: cfg.DumpEnv(),
420420
})
421421

422422
if err != nil {

main_test.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ func TestFetchOrCreateTestPlan(t *testing.T) {
342342
defer svr.Close()
343343

344344
ctx := context.Background()
345-
env := env.Map{}
346345
cfg := config.Config{
347346
NodeIndex: 0,
348347
Parallelism: 10,
349348
Identifier: "identifier",
350349
ServerBaseUrl: svr.URL,
350+
Env: env.Map{},
351351
}
352352
apiClient := api.NewClient(api.ClientConfig{
353353
ServerBaseUrl: cfg.ServerBaseUrl,
@@ -363,7 +363,7 @@ func TestFetchOrCreateTestPlan(t *testing.T) {
363363
},
364364
}
365365

366-
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, env, files, testRunner)
366+
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, files, testRunner)
367367
if err != nil {
368368
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) error = %v", cfg, files, err)
369369
}
@@ -418,8 +418,8 @@ func TestFetchOrCreateTestPlan_CachedPlan(t *testing.T) {
418418
OrganizationSlug: "org",
419419
SuiteSlug: "suite",
420420
Branch: "tat-123/my-cool-feature",
421+
Env: env.Map{},
421422
}
422-
env := env.Map{}
423423
apiClient := api.NewClient(api.ClientConfig{
424424
ServerBaseUrl: cfg.ServerBaseUrl,
425425
OrganizationSlug: cfg.OrganizationSlug,
@@ -437,7 +437,7 @@ func TestFetchOrCreateTestPlan_CachedPlan(t *testing.T) {
437437
},
438438
}
439439

440-
got, err := fetchOrCreateTestPlan(context.Background(), apiClient, cfg, env, tests, testRunner)
440+
got, err := fetchOrCreateTestPlan(context.Background(), apiClient, cfg, tests, testRunner)
441441
if err != nil {
442442
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) error = %v", cfg, tests, err)
443443
}
@@ -466,16 +466,16 @@ func TestFetchOrCreateTestPlan_PlanError(t *testing.T) {
466466
Identifier: "identifier",
467467
Branch: "tat-123/my-cool-feature",
468468
ServerBaseUrl: svr.URL,
469+
Env: env.Map{},
469470
}
470-
env := env.Map{}
471471
apiClient := api.NewClient(api.ClientConfig{
472472
ServerBaseUrl: cfg.ServerBaseUrl,
473473
})
474474

475475
// we want the function to return a fallback plan
476476
want := plan.CreateFallbackPlan(files, cfg.Parallelism)
477477

478-
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, env, files, TestRunner)
478+
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, files, TestRunner)
479479
if err != nil {
480480
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) error = %v", cfg, files, err)
481481
}
@@ -505,16 +505,16 @@ func TestFetchOrCreateTestPlan_InternalServerError(t *testing.T) {
505505
Identifier: "identifier",
506506
Branch: "tat-123/my-cool-feature",
507507
ServerBaseUrl: svr.URL,
508+
Env: env.Map{},
508509
}
509-
env := env.Map{}
510510
apiClient := api.NewClient(api.ClientConfig{
511511
ServerBaseUrl: cfg.ServerBaseUrl,
512512
})
513513

514514
// we want the function to return a fallback plan
515515
want := plan.CreateFallbackPlan(files, cfg.Parallelism)
516516

517-
got, err := fetchOrCreateTestPlan(fetchCtx, apiClient, cfg, env, files, testRunner)
517+
got, err := fetchOrCreateTestPlan(fetchCtx, apiClient, cfg, files, testRunner)
518518
if err != nil {
519519
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) error = %v", cfg, files, err)
520520
}
@@ -541,16 +541,16 @@ func TestFetchOrCreateTestPlan_BadRequest(t *testing.T) {
541541
Identifier: "identifier",
542542
Branch: "",
543543
ServerBaseUrl: svr.URL,
544+
Env: env.Map{},
544545
}
545-
env := env.Map{}
546546
apiClient := api.NewClient(api.ClientConfig{
547547
ServerBaseUrl: cfg.ServerBaseUrl,
548548
})
549549

550550
// we want the function to return an empty test plan and an error
551551
want := plan.TestPlan{}
552552

553-
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, env, files, testRunner)
553+
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, files, testRunner)
554554
if err == nil {
555555
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) want error, got %v", cfg, files, err)
556556
}
@@ -577,16 +577,16 @@ func TestFetchOrCreateTestPlan_BillingError(t *testing.T) {
577577
Identifier: "identifier",
578578
Branch: "",
579579
ServerBaseUrl: svr.URL,
580+
Env: env.Map{},
580581
}
581-
env := env.Map{}
582582
apiClient := api.NewClient(api.ClientConfig{
583583
ServerBaseUrl: cfg.ServerBaseUrl,
584584
})
585585

586586
// we want the function to return a fallback plan
587587
want := plan.CreateFallbackPlan(files, cfg.Parallelism)
588588

589-
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, env, files, testRunner)
589+
got, err := fetchOrCreateTestPlan(ctx, apiClient, cfg, files, testRunner)
590590
if err != nil {
591591
t.Errorf("fetchOrCreateTestPlan(ctx, %v, %v) error = %v", cfg, files, err)
592592
}
@@ -614,8 +614,8 @@ func TestCreateRequestParams(t *testing.T) {
614614
Parallelism: 7,
615615
Branch: "",
616616
TestRunner: "rspec",
617+
Env: env.Map{},
617618
}
618-
env := env.Map{}
619619

620620
client := api.NewClient(api.ClientConfig{
621621
ServerBaseUrl: svr.URL,
@@ -630,7 +630,7 @@ func TestCreateRequestParams(t *testing.T) {
630630
"testdata/rspec/spec/fruits/grape_spec.rb",
631631
}
632632

633-
got, err := createRequestParam(context.Background(), cfg, env, files, *client, runner.Rspec{
633+
got, err := createRequestParam(context.Background(), cfg, files, *client, runner.Rspec{
634634
RunnerConfig: runner.RunnerConfig{
635635
TestCommand: "rspec",
636636
},
@@ -708,8 +708,8 @@ func TestCreateRequestParams_NonRSpec(t *testing.T) {
708708
Parallelism: 7,
709709
Branch: "",
710710
TestRunner: r.Name(),
711+
Env: env.Map{},
711712
}
712-
env := env.Map{}
713713

714714
client := api.NewClient(api.ClientConfig{
715715
ServerBaseUrl: svr.URL,
@@ -720,7 +720,7 @@ func TestCreateRequestParams_NonRSpec(t *testing.T) {
720720
"testdata/fruits/cherry.spec.js",
721721
}
722722

723-
got, err := createRequestParam(context.Background(), cfg, env, files, *client, r)
723+
got, err := createRequestParam(context.Background(), cfg, files, *client, r)
724724

725725
if err != nil {
726726
t.Errorf("createRequestParam() error = %v", err)
@@ -761,8 +761,8 @@ func TestCreateRequestParams_FilterTestsError(t *testing.T) {
761761
Parallelism: 7,
762762
Branch: "",
763763
SplitByExample: true,
764+
Env: env.Map{},
764765
}
765-
env := env.Map{}
766766

767767
client := api.NewClient(api.ClientConfig{
768768
ServerBaseUrl: svr.URL,
@@ -777,7 +777,7 @@ func TestCreateRequestParams_FilterTestsError(t *testing.T) {
777777
"grape_spec.rb",
778778
}
779779

780-
_, err := createRequestParam(context.Background(), cfg, env, files, *client, runner.Rspec{})
780+
_, err := createRequestParam(context.Background(), cfg, files, *client, runner.Rspec{})
781781

782782
if err.Error() != "filter tests: forbidden" {
783783
t.Errorf("createRequestParam() error = %v, want forbidden error", err)
@@ -800,8 +800,8 @@ func TestCreateRequestParams_NoFilteredFiles(t *testing.T) {
800800
Parallelism: 7,
801801
Branch: "",
802802
SplitByExample: true,
803+
Env: env.Map{},
803804
}
804-
env := env.Map{}
805805

806806
client := api.NewClient(api.ClientConfig{
807807
ServerBaseUrl: svr.URL,
@@ -816,7 +816,7 @@ func TestCreateRequestParams_NoFilteredFiles(t *testing.T) {
816816
"testdata/rspec/spec/fruits/grape_spec.rb",
817817
}
818818

819-
got, err := createRequestParam(context.Background(), cfg, env, files, *client, runner.Rspec{
819+
got, err := createRequestParam(context.Background(), cfg, files, *client, runner.Rspec{
820820
RunnerConfig: runner.RunnerConfig{
821821
TestCommand: "rspec",
822822
},
@@ -934,6 +934,7 @@ func TestSendMetadata(t *testing.T) {
934934
SuiteSlug: "rspec",
935935
Identifier: "fruitsabc",
936936
ServerBaseUrl: svr.URL,
937+
Env: env,
937938
}
938939
client := api.NewClient(api.ClientConfig{
939940
ServerBaseUrl: cfg.ServerBaseUrl,
@@ -943,7 +944,7 @@ func TestSendMetadata(t *testing.T) {
943944
Total: 3,
944945
}
945946

946-
sendMetadata(context.Background(), client, cfg, env, timeline, statistics)
947+
sendMetadata(context.Background(), client, cfg, timeline, statistics)
947948
}
948949

949950
func TestSendMetadata_Unauthorized(t *testing.T) {
@@ -957,8 +958,8 @@ func TestSendMetadata_Unauthorized(t *testing.T) {
957958
SuiteSlug: "my-suite",
958959
Identifier: "identifier",
959960
ServerBaseUrl: svr.URL,
961+
Env: env.Map{},
960962
}
961-
env := env.Map{}
962963
client := api.NewClient(api.ClientConfig{
963964
ServerBaseUrl: cfg.ServerBaseUrl,
964965
})
@@ -969,5 +970,5 @@ func TestSendMetadata_Unauthorized(t *testing.T) {
969970
Total: 3,
970971
}
971972

972-
sendMetadata(context.Background(), client, cfg, env, timeline, statistics)
973+
sendMetadata(context.Background(), client, cfg, timeline, statistics)
973974
}

0 commit comments

Comments
 (0)