Skip to content

Commit 901da7f

Browse files
authored
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
1 parent bcf6bce commit 901da7f

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

_examples/attachments/attachments_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ func TestFeatures(t *testing.T) {
4646

4747
func InitializeScenario(ctx *godog.ScenarioContext) {
4848

49+
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
50+
ctx = godog.Attach(ctx,
51+
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
52+
)
53+
return ctx, nil
54+
})
55+
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
56+
ctx = godog.Attach(ctx,
57+
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
58+
)
59+
return ctx, nil
60+
})
61+
4962
ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) {
5063
// the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report
5164
ctx = godog.Attach(ctx,

internal/formatters/fmt_output_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ func listFmtOutputTestsFeatureFiles() (featureFiles []string, err error) {
6363

6464
func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
6565
fmtOutputScenarioInitializer := func(ctx *godog.ScenarioContext) {
66+
67+
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
68+
att := godog.Attachments(ctx)
69+
attCount := len(att)
70+
if attCount > 0 {
71+
assert.FailNow(tT, fmt.Sprintf("Unexpected Attachments found - should have been empty, found %d\n%+v", attCount, att))
72+
}
73+
74+
if st.Text == "a step with multiple attachment calls" {
75+
ctx = godog.Attach(ctx,
76+
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
77+
)
78+
}
79+
return ctx, nil
80+
})
81+
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
82+
83+
if st.Text == "a step with multiple attachment calls" {
84+
att := godog.Attachments(ctx)
85+
attCount := len(att)
86+
if attCount != 3 {
87+
assert.FailNow(tT, fmt.Sprintf("Expected 3 Attachments - 1 from the before step and 2 from the step, found %d\n%+v", attCount, att))
88+
}
89+
ctx = godog.Attach(ctx,
90+
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
91+
)
92+
}
93+
return ctx, nil
94+
})
95+
6696
ctx.Step(`^(?:a )?failing step`, failingStepDef)
6797
ctx.Step(`^(?:a )?pending step$`, pendingStepDef)
6898
ctx.Step(`^(?:a )?passing step$`, passingStepDef)
@@ -144,8 +174,8 @@ func stepWithSingleAttachmentCall(ctx context.Context) (context.Context, error)
144174
return ctx, nil
145175
}
146176
func stepWithMultipleAttachmentCalls(ctx context.Context) (context.Context, error) {
147-
if len(godog.Attachments(ctx)) > 0 {
148-
assert.FailNow(tT, "Unexpected Attachments found - should have been empty")
177+
if len(godog.Attachments(ctx)) != 1 {
178+
assert.FailNow(tT, "Expected 1 Attachment that should have been inserted by before step")
149179
}
150180

151181
ctx = godog.Attach(ctx,

internal/formatters/formatter-tests/cucumber/scenario_with_attachment

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
"duration": 0
5252
},
5353
"embeddings": [
54+
{
55+
"name": "Data Attachment",
56+
"mime_type": "text/plain",
57+
"data": "QmVmb3JlU3RlcEF0dGFjaG1lbnQ="
58+
},
5459
{
5560
"name": "TheFilename1",
5661
"mime_type": "text/plain",
@@ -60,6 +65,11 @@
6065
"name": "TheFilename2",
6166
"mime_type": "text/plain",
6267
"data": "VGhlRGF0YTI="
68+
},
69+
{
70+
"name": "Data Attachment",
71+
"mime_type": "text/plain",
72+
"data": "QWZ0ZXJTdGVwQXR0YWNobWVudA=="
6373
}
6474
]
6575
}

internal/formatters/formatter-tests/events/scenario_with_attachment

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"status":"passed"}
99
{"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":[]}
1010
{"event":"TestStepStarted","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871}
11+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Data Attachment","mimeType":"text/plain","body":"BeforeStepAttachment"}
1112
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
1213
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"}
14+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Data Attachment","mimeType":"text/plain","body":"AfterStepAttachment"}
1315
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"status":"passed"}
1416
{"event":"TestCaseFinished","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871,"status":"passed"}
1517
{"event":"TestRunFinished","status":"passed","timestamp":-6795364578871,"snippets":"","memory":""}

suite.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,15 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, scena
179179
status = StepPassed
180180
}
181181

182-
pickledAttachments := pickleAttachments(ctx)
183-
ctx = clearAttach(ctx)
184-
185182
// Run after step handlers.
186183
rctx, err = s.runAfterStepHooks(ctx, step, status, err)
187184

188-
shouldFail := s.shouldFail(err)
185+
// extract any accumulated attachments and clear them
186+
pickledAttachments := pickleAttachments(rctx)
187+
rctx = clearAttach(rctx)
189188

190189
// Trigger after scenario on failing or last step to attach possible hook error to step.
191-
if !s.shouldFail(scenarioErr) && (isLast || shouldFail) {
190+
if !s.shouldFail(scenarioErr) && (isLast || s.shouldFail(err)) {
192191
rctx, err = s.runAfterScenarioHooks(rctx, pickle, err)
193192
}
194193

0 commit comments

Comments
 (0)