fixed a bug where the attachments are extracted from the context too early, this prevented AfterStep from making attachments (#637)

* fixed a bug where the attachments are extracted from the context too early, this prevented AfterStep from making attachments
Этот коммит содержится в:
John Lonergan 2024-07-23 17:22:51 +01:00 коммит произвёл GitHub
родитель bcf6bce793
коммит 901da7fa3d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 61 добавлений и 7 удалений

Просмотреть файл

@ -46,6 +46,19 @@ func TestFeatures(t *testing.T) {
func InitializeScenario(ctx *godog.ScenarioContext) { func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
)
return ctx, nil
})
ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) { ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) {
// the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report // the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report
ctx = godog.Attach(ctx, ctx = godog.Attach(ctx,

Просмотреть файл

@ -63,6 +63,36 @@ func listFmtOutputTestsFeatureFiles() (featureFiles []string, err error) {
func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) { func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
fmtOutputScenarioInitializer := func(ctx *godog.ScenarioContext) { fmtOutputScenarioInitializer := func(ctx *godog.ScenarioContext) {
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
att := godog.Attachments(ctx)
attCount := len(att)
if attCount > 0 {
assert.FailNow(tT, fmt.Sprintf("Unexpected Attachments found - should have been empty, found %d\n%+v", attCount, att))
}
if st.Text == "a step with multiple attachment calls" {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
)
}
return ctx, nil
})
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
if st.Text == "a step with multiple attachment calls" {
att := godog.Attachments(ctx)
attCount := len(att)
if attCount != 3 {
assert.FailNow(tT, fmt.Sprintf("Expected 3 Attachments - 1 from the before step and 2 from the step, found %d\n%+v", attCount, att))
}
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
)
}
return ctx, nil
})
ctx.Step(`^(?:a )?failing step`, failingStepDef) ctx.Step(`^(?:a )?failing step`, failingStepDef)
ctx.Step(`^(?:a )?pending step$`, pendingStepDef) ctx.Step(`^(?:a )?pending step$`, pendingStepDef)
ctx.Step(`^(?:a )?passing step$`, passingStepDef) ctx.Step(`^(?:a )?passing step$`, passingStepDef)
@ -144,8 +174,8 @@ func stepWithSingleAttachmentCall(ctx context.Context) (context.Context, error)
return ctx, nil return ctx, nil
} }
func stepWithMultipleAttachmentCalls(ctx context.Context) (context.Context, error) { func stepWithMultipleAttachmentCalls(ctx context.Context) (context.Context, error) {
if len(godog.Attachments(ctx)) > 0 { if len(godog.Attachments(ctx)) != 1 {
assert.FailNow(tT, "Unexpected Attachments found - should have been empty") assert.FailNow(tT, "Expected 1 Attachment that should have been inserted by before step")
} }
ctx = godog.Attach(ctx, ctx = godog.Attach(ctx,

Просмотреть файл

@ -51,6 +51,11 @@
"duration": 0 "duration": 0
}, },
"embeddings": [ "embeddings": [
{
"name": "Data Attachment",
"mime_type": "text/plain",
"data": "QmVmb3JlU3RlcEF0dGFjaG1lbnQ="
},
{ {
"name": "TheFilename1", "name": "TheFilename1",
"mime_type": "text/plain", "mime_type": "text/plain",
@ -60,6 +65,11 @@
"name": "TheFilename2", "name": "TheFilename2",
"mime_type": "text/plain", "mime_type": "text/plain",
"data": "VGhlRGF0YTI=" "data": "VGhlRGF0YTI="
},
{
"name": "Data Attachment",
"mime_type": "text/plain",
"data": "QWZ0ZXJTdGVwQXR0YWNobWVudA=="
} }
] ]
} }

Просмотреть файл

@ -8,8 +8,10 @@
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"status":"passed"} {"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"status":"passed"}
{"event":"StepDefinitionFound","location":"formatter-tests/features/scenario_with_attachment.feature:8","definition_id":"fmt_output_test.go:XXX -\u003e github.com/cucumber/godog/internal/formatters_test.stepWithMultipleAttachmentCalls","arguments":[]} {"event":"StepDefinitionFound","location":"formatter-tests/features/scenario_with_attachment.feature:8","definition_id":"fmt_output_test.go:XXX -\u003e github.com/cucumber/godog/internal/formatters_test.stepWithMultipleAttachmentCalls","arguments":[]}
{"event":"TestStepStarted","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871} {"event":"TestStepStarted","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871}
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Data Attachment","mimeType":"text/plain","body":"BeforeStepAttachment"}
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"} {"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"} {"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"}
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Data Attachment","mimeType":"text/plain","body":"AfterStepAttachment"}
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"status":"passed"} {"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"status":"passed"}
{"event":"TestCaseFinished","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871,"status":"passed"} {"event":"TestCaseFinished","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871,"status":"passed"}
{"event":"TestRunFinished","status":"passed","timestamp":-6795364578871,"snippets":"","memory":""} {"event":"TestRunFinished","status":"passed","timestamp":-6795364578871,"snippets":"","memory":""}

Просмотреть файл

@ -179,16 +179,15 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, scena
status = StepPassed status = StepPassed
} }
pickledAttachments := pickleAttachments(ctx)
ctx = clearAttach(ctx)
// Run after step handlers. // Run after step handlers.
rctx, err = s.runAfterStepHooks(ctx, step, status, err) rctx, err = s.runAfterStepHooks(ctx, step, status, err)
shouldFail := s.shouldFail(err) // extract any accumulated attachments and clear them
pickledAttachments := pickleAttachments(rctx)
rctx = clearAttach(rctx)
// Trigger after scenario on failing or last step to attach possible hook error to step. // Trigger after scenario on failing or last step to attach possible hook error to step.
if !s.shouldFail(scenarioErr) && (isLast || shouldFail) { if !s.shouldFail(scenarioErr) && (isLast || s.shouldFail(err)) {
rctx, err = s.runAfterScenarioHooks(rctx, pickle, err) rctx, err = s.runAfterScenarioHooks(rctx, pickle, err)
} }