Skip to content

Commit 8edde7f

Browse files
authored
Bugfix afterscenario attachments (#646)
* Minor doc and comment corrections * Fixed bug where it was impossible to make attachments from 'after scenario' hook, also removed some dud comments. * typo
1 parent 1e7c45e commit 8edde7f

File tree

7 files changed

+139
-48
lines changed

7 files changed

+139
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
88

99
## Unreleased
1010

11-
- Ambiguous step definitions will now be detected when strit mode is activated - ([636](https://github.com/cucumber/godog/pull/636) - [johnlon](https://github.com/johnlon))
11+
- Ambiguous step definitions will now be detected when strict mode is activated - ([636](https://github.com/cucumber/godog/pull/636) - [johnlon](https://github.com/johnlon))
1212
- Provide support for attachments / embeddings including a new example in the examples dir - ([623](https://github.com/cucumber/godog/pull/623) - [johnlon](https://github.com/johnlon))
1313

1414
## [v0.14.1]

_examples/attachments/attachments_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package attachments_test
22

3-
// This example shows how to attach data to the cucumber reports
3+
// This "demo" doesn't actually get run as a test by the build.
4+
5+
// This "example" shows how to attach data to the cucumber reports
46
// Run the sample with : go test -v attachments_test.go
57
// Then review the "embeddings" within the JSON emitted on the console.
68

@@ -39,34 +41,47 @@ func TestFeatures(t *testing.T) {
3941

4042
func InitializeScenario(ctx *godog.ScenarioContext) {
4143

44+
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
45+
ctx = godog.Attach(ctx,
46+
godog.Attachment{Body: []byte("BeforeScenarioAttachment"), FileName: "Step Attachment 1", MediaType: "text/plain"},
47+
)
48+
return ctx, nil
49+
})
50+
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
51+
ctx = godog.Attach(ctx,
52+
godog.Attachment{Body: []byte("AfterScenarioAttachment"), FileName: "Step Attachment 2", MediaType: "text/plain"},
53+
)
54+
return ctx, nil
55+
})
56+
4257
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
4358
ctx = godog.Attach(ctx,
44-
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
59+
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Step Attachment 3", MediaType: "text/plain"},
4560
)
4661
return ctx, nil
4762
})
4863
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
4964
ctx = godog.Attach(ctx,
50-
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
65+
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Step Attachment 4", MediaType: "text/plain"},
5166
)
5267
return ctx, nil
5368
})
5469

5570
ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) {
5671
// the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report
5772
ctx = godog.Attach(ctx,
58-
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment", MediaType: "text/plain"},
73+
godog.Attachment{Body: []byte("TheData1"), FileName: "Step Attachment 5", MediaType: "text/plain"},
5974
)
6075
ctx = godog.Attach(ctx,
61-
godog.Attachment{Body: []byte("{ \"a\" : 1 }"), FileName: "Json Attachment", MediaType: "application/json"},
76+
godog.Attachment{Body: []byte("{ \"a\" : 1 }"), FileName: "Step Attachment 6", MediaType: "application/json"},
6277
)
6378

6479
return ctx, nil
6580
})
6681
ctx.Step(`^I have attached two documents at once$`, func(ctx context.Context) (context.Context, error) {
6782
ctx = godog.Attach(ctx,
68-
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment 1", MediaType: "text/plain"},
69-
godog.Attachment{Body: []byte("TheData2"), FileName: "Data Attachment 2", MediaType: "text/plain"},
83+
godog.Attachment{Body: []byte("TheData1"), FileName: "Step Attachment 7", MediaType: "text/plain"},
84+
godog.Attachment{Body: []byte("TheData2"), FileName: "Step Attachment 8", MediaType: "text/plain"},
7085
)
7186

7287
return ctx, nil

internal/formatters/fmt_output_test.go

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,79 @@ func listFmtOutputTestsFeatureFiles() (featureFiles []string, err error) {
6363

6464
func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
6565
fmtOutputScenarioInitializer := func(ctx *godog.ScenarioContext) {
66+
stepIndex := 0
67+
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
68+
if strings.Contains(sc.Name, "attachment") {
69+
att := godog.Attachments(ctx)
70+
attCount := len(att)
71+
if attCount != 0 {
72+
assert.FailNowf(tT, "Unexpected attachments: "+sc.Name, "should have been empty, found %d", attCount)
73+
}
6674

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))
75+
ctx = godog.Attach(ctx,
76+
godog.Attachment{Body: []byte("BeforeScenarioAttachment"), FileName: "Before Scenario Attachment 1", MediaType: "text/plain"},
77+
)
78+
}
79+
return ctx, nil
80+
})
81+
82+
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
83+
84+
if strings.Contains(sc.Name, "attachment") {
85+
att := godog.Attachments(ctx)
86+
attCount := len(att)
87+
if attCount != 4 {
88+
assert.FailNow(tT, "Unexpected attachements: "+sc.Name, "expected 4, found %d", attCount)
89+
}
90+
ctx = godog.Attach(ctx,
91+
godog.Attachment{Body: []byte("AfterScenarioAttachment"), FileName: "After Scenario Attachment 2", MediaType: "text/plain"},
92+
)
7293
}
94+
return ctx, nil
95+
})
96+
97+
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
98+
stepIndex++
99+
100+
if strings.Contains(st.Text, "attachment") {
101+
att := godog.Attachments(ctx)
102+
attCount := len(att)
103+
104+
// 1 for before scenario ONLY if this is the 1st step
105+
expectedAttCount := 0
106+
if stepIndex == 1 {
107+
expectedAttCount = 1
108+
}
73109

74-
if st.Text == "a step with multiple attachment calls" {
110+
if attCount != expectedAttCount {
111+
assert.FailNow(tT, "Unexpected attachments: "+st.Text, "expected 1, found %d\n%+v", attCount, att)
112+
}
75113
ctx = godog.Attach(ctx,
76-
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
114+
godog.Attachment{Body: []byte("BeforeStepAttachment"), FileName: "Before Step Attachment 3", MediaType: "text/plain"},
77115
)
78116
}
79117
return ctx, nil
80118
})
81119
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
82120

83-
if st.Text == "a step with multiple attachment calls" {
121+
if strings.Contains(st.Text, "attachment") {
84122
att := godog.Attachments(ctx)
85123
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))
124+
125+
// 1 for before scenario ONLY if this is the 1st step
126+
// 1 for before before step
127+
// 2 from from step
128+
expectedAttCount := 3
129+
if stepIndex == 1 {
130+
expectedAttCount = 4
131+
}
132+
133+
if attCount != expectedAttCount {
134+
// 1 from before scenario, 1 from before step, 1 from step
135+
assert.FailNow(tT, "Unexpected attachments: "+st.Text, "expected 4, found %d", attCount)
88136
}
89137
ctx = godog.Attach(ctx,
90-
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "Data Attachment", MediaType: "text/plain"},
138+
godog.Attachment{Body: []byte("AfterStepAttachment"), FileName: "After Step Attachment 4", MediaType: "text/plain"},
91139
)
92140
}
93141
return ctx, nil
@@ -105,7 +153,8 @@ func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
105153
expectOutputPath := strings.Replace(featureFilePath, "features", fmtName, 1)
106154
expectOutputPath = strings.TrimSuffix(expectOutputPath, path.Ext(expectOutputPath))
107155
if _, err := os.Stat(expectOutputPath); err != nil {
108-
t.Skipf("Couldn't find expected output file %q", expectOutputPath)
156+
// the test author needs to write an "expected output" file for any formats they want the test feature to be verified against
157+
t.Skipf("Skipping test for feature '%v' for format '%v', because no 'expected output' file %q", featureFilePath, fmtName, expectOutputPath)
109158
}
110159

111160
expectedOutput, err := os.ReadFile(expectOutputPath)
@@ -130,6 +179,9 @@ func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
130179
expected := normalise(string(expectedOutput))
131180
actual := normalise(buf.String())
132181
assert.Equalf(t, expected, actual, "path: %s", expectOutputPath)
182+
if expected != actual {
183+
println("diff")
184+
}
133185
}
134186
}
135187

@@ -162,8 +214,10 @@ func pendingStepDef() error { return godog.ErrPending }
162214
func failingStepDef() error { return fmt.Errorf("step failed") }
163215

164216
func stepWithSingleAttachmentCall(ctx context.Context) (context.Context, error) {
165-
if len(godog.Attachments(ctx)) > 0 {
166-
assert.FailNow(tT, "Unexpected Attachments found - should have been empty")
217+
aCount := len(godog.Attachments(ctx))
218+
if aCount != 2 {
219+
// 1 from before scenario, 1 from before step
220+
assert.FailNowf(tT, "Unexpected Attachments found", "should have been 2, but found %v", aCount)
167221
}
168222

169223
ctx = godog.Attach(ctx,
@@ -174,15 +228,16 @@ func stepWithSingleAttachmentCall(ctx context.Context) (context.Context, error)
174228
return ctx, nil
175229
}
176230
func stepWithMultipleAttachmentCalls(ctx context.Context) (context.Context, error) {
177-
if len(godog.Attachments(ctx)) != 1 {
178-
assert.FailNow(tT, "Expected 1 Attachment that should have been inserted by before step")
231+
aCount := len(godog.Attachments(ctx))
232+
if aCount != 1 {
233+
assert.FailNowf(tT, "Unexpected Attachments found", "Expected 1 Attachment, but found %v", aCount)
179234
}
180235

181236
ctx = godog.Attach(ctx,
182-
godog.Attachment{Body: []byte("TheData1"), FileName: "TheFilename1", MediaType: "text/plain"},
237+
godog.Attachment{Body: []byte("TheData1"), FileName: "TheFilename3", MediaType: "text/plain"},
183238
)
184239
ctx = godog.Attach(ctx,
185-
godog.Attachment{Body: []byte("TheData2"), FileName: "TheFilename2", MediaType: "text/plain"},
240+
godog.Attachment{Body: []byte("TheData2"), FileName: "TheFilename4", MediaType: "text/plain"},
186241
)
187242

188243
return ctx, nil

internal/formatters/formatter-tests/cucumber/scenario_with_attachment

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[
22
{
33
"uri": "formatter-tests/features/scenario_with_attachment.feature",
4-
"id": "scenario-with-attachment",
4+
"id": "feature-with-attachment",
55
"keyword": "Feature",
6-
"name": "scenario with attachment",
6+
"name": "feature with attachment",
77
"description": " describes\n an attachment\n feature",
88
"line": 1,
99
"elements": [
1010
{
11-
"id": "scenario-with-attachment;step-with-attachment",
11+
"id": "feature-with-attachment;scenario-with-attachment",
1212
"keyword": "Scenario",
13-
"name": "step with attachment",
13+
"name": "scenario with attachment",
1414
"description": "",
1515
"line": 6,
1616
"type": "scenario",
@@ -27,6 +27,16 @@
2727
"duration": 0
2828
},
2929
"embeddings": [
30+
{
31+
"name": "Before Scenario Attachment 1",
32+
"mime_type": "text/plain",
33+
"data": "QmVmb3JlU2NlbmFyaW9BdHRhY2htZW50"
34+
},
35+
{
36+
"name": "Before Step Attachment 3",
37+
"mime_type": "text/plain",
38+
"data": "QmVmb3JlU3RlcEF0dGFjaG1lbnQ="
39+
},
3040
{
3141
"name": "TheFilename1",
3242
"mime_type": "text/plain",
@@ -36,6 +46,11 @@
3646
"name": "TheFilename2",
3747
"mime_type": "text/plain",
3848
"data": "VGhlRGF0YTI="
49+
},
50+
{
51+
"name": "After Step Attachment 4",
52+
"mime_type": "text/plain",
53+
"data": "QWZ0ZXJTdGVwQXR0YWNobWVudA=="
3954
}
4055
]
4156
},
@@ -52,24 +67,29 @@
5267
},
5368
"embeddings": [
5469
{
55-
"name": "Data Attachment",
70+
"name": "Before Step Attachment 3",
5671
"mime_type": "text/plain",
5772
"data": "QmVmb3JlU3RlcEF0dGFjaG1lbnQ="
5873
},
5974
{
60-
"name": "TheFilename1",
75+
"name": "TheFilename3",
6176
"mime_type": "text/plain",
6277
"data": "VGhlRGF0YTE="
6378
},
6479
{
65-
"name": "TheFilename2",
80+
"name": "TheFilename4",
6681
"mime_type": "text/plain",
6782
"data": "VGhlRGF0YTI="
6883
},
6984
{
70-
"name": "Data Attachment",
85+
"name": "After Step Attachment 4",
7186
"mime_type": "text/plain",
7287
"data": "QWZ0ZXJTdGVwQXR0YWNobWVudA=="
88+
},
89+
{
90+
"name": "After Scenario Attachment 2",
91+
"mime_type": "text/plain",
92+
"data": "QWZ0ZXJTY2VuYXJpb0F0dGFjaG1lbnQ="
7393
}
7494
]
7595
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{"event":"TestRunStarted","version":"0.1.0","timestamp":-6795364578871,"suite":"events"}
2-
{"event":"TestSource","location":"formatter-tests/features/scenario_with_attachment.feature:1","source":"Feature: scenario with attachment\n describes\n an attachment\n feature\n\n Scenario: step with attachment\n Given a step with a single attachment call for multiple attachments\n And a step with multiple attachment calls\n"}
2+
{"event":"TestSource","location":"formatter-tests/features/scenario_with_attachment.feature:1","source":"Feature: feature with attachment\n describes\n an attachment\n feature\n\n Scenario: scenario with attachment\n Given a step with a single attachment call for multiple attachments\n And a step with multiple attachment calls\n"}
33
{"event":"TestCaseStarted","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871}
44
{"event":"StepDefinitionFound","location":"formatter-tests/features/scenario_with_attachment.feature:7","definition_id":"fmt_output_test.go:XXX -\u003e github.com/cucumber/godog/internal/formatters_test.stepWithSingleAttachmentCall","arguments":[]}
55
{"event":"TestStepStarted","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871}
6+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Before Scenario Attachment 1","mimeType":"text/plain","body":"BeforeScenarioAttachment"}
7+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Before Step Attachment 3","mimeType":"text/plain","body":"BeforeStepAttachment"}
68
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
79
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"}
10+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"After Step Attachment 4","mimeType":"text/plain","body":"AfterStepAttachment"}
811
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"status":"passed"}
912
{"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":[]}
1013
{"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"}
12-
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
13-
{"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"}
14+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"Before Step Attachment 3","mimeType":"text/plain","body":"BeforeStepAttachment"}
15+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename3","mimeType":"text/plain","body":"TheData1"}
16+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename4","mimeType":"text/plain","body":"TheData2"}
17+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"After Step Attachment 4","mimeType":"text/plain","body":"AfterStepAttachment"}
18+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"After Scenario Attachment 2","mimeType":"text/plain","body":"AfterScenarioAttachment"}
1519
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"status":"passed"}
1620
{"event":"TestCaseFinished","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871,"status":"passed"}
1721
{"event":"TestRunFinished","status":"passed","timestamp":-6795364578871,"snippets":"","memory":""}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Feature: scenario with attachment
1+
Feature: feature with attachment
22
describes
33
an attachment
44
feature
55

6-
Scenario: step with attachment
6+
Scenario: scenario with attachment
77
Given a step with a single attachment call for multiple attachments
88
And a step with multiple attachment calls

0 commit comments

Comments
 (0)