func RunBuildWithPassingEnvsMultistep()

in common/buildtest/variables.go [57:126]


func RunBuildWithPassingEnvsMultistep(t *testing.T, config *common.RunnerConfig, setup BuildSetupFn) {
	formatter := shellFormatter(config.Shell)

	steps := []string{formatter.PipeVar("hello=world") + formatter.EnvName("GITLAB_ENV")}
	if config.Shell == "bash" {
		steps = append(steps, `echo 'executed=$(echo "yes")' >> $GITLAB_ENV`)
	}

	resp, err := common.GetRemoteBuildResponse(steps...)
	require.NoError(t, err)

	build := &common.Build{
		JobResponse: resp,
		Runner:      config,
	}

	if runtime.GOOS == "linux" && config.Shell == shells.SNPwsh {
		build.Image.Name = common.TestPwshImage
	}

	dir := t.TempDir()
	build.Runner.RunnerSettings.BuildsDir = filepath.Join(dir, "build")
	build.Runner.RunnerSettings.CacheDir = filepath.Join(dir, "cache")
	build.Variables = append(build.Variables, common.JobVariable{
		Key:   "existing",
		Value: "existingvalue",
	})

	build.Steps = append(
		build.Steps,
		common.Step{
			Name: "custom-step",
			Script: []string{
				`echo ` + formatter.EnvName("GITLAB_ENV"),
				`echo hellovalue=` + formatter.EnvName("hello"),
				`echo executed=` + formatter.EnvName("executed"),
				formatter.PipeVar("foo=bar") + formatter.EnvName("GITLAB_ENV"),
			},
			When: common.StepWhenOnSuccess,
		},
		common.Step{
			Name: common.StepNameAfterScript,
			Script: []string{
				`echo foovalue=` + formatter.EnvName("foo"),
				`echo existing=` + formatter.EnvName("existing"),
			},
			When: common.StepWhenAlways,
		},
	)
	build.Cache = append(build.Cache, common.Cache{
		Key:    "cache",
		Paths:  common.ArtifactPaths{"unknown/path/${foo}"},
		Policy: common.CachePolicyPullPush,
	})

	if setup != nil {
		setup(t, build)
	}

	buf := new(bytes.Buffer)
	trace := &common.Trace{Writer: buf}
	assert.NoError(t, RunBuildWithTrace(t, build, trace))

	contents := buf.String()
	assert.Contains(t, contents, "existing=existingvalue")
	assert.Contains(t, contents, "hellovalue=world")
	assert.Contains(t, contents, "foovalue=bar")
	assert.Contains(t, contents, "unknown/path/bar: no matching files")
	assert.NotContains(t, contents, "executed=yes")
}