Skip to content

Commit 9558224

Browse files
authored
Additional code review observations on Attach() functionality from #623 (#628)
* support multiple calls to the Attach() function from a single step * run_progress_test.go changed so it's not sensitive to the name of the clone target directory * applied code review comments also added _example/attachments * applied code review comments also added _example/attachments * applied code review comments also added _example/attachments
1 parent f85def3 commit 9558224

File tree

13 files changed

+177
-23
lines changed

13 files changed

+177
-23
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-
- Provide support for attachments / embeddings - ([623](https://github.com/cucumber/godog/pull/623) - [johnlon](https://github.com/johnlon))
11+
- 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))
1212

1313
## [v0.14.1]
1414

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ When steps are orthogonal and small, you can combine them just like you do with
292292

293293
`TestFeatures` acts as a regular Go test, so you can leverage your IDE facilities to run and debug it.
294294

295+
### Attachments
296+
297+
An example showing how to make attachments (aka embeddings) to the results is shown in [_examples/attachments](/_examples/attachments/)
298+
295299
## Code of Conduct
296300

297301
Everyone interacting in this codebase and issue tracker is expected to follow the Cucumber [code of conduct](https://github.com/cucumber/cucumber/blob/master/CODE_OF_CONDUCT.md).

_examples/attachments/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# An example of Making attachments to the reports
2+
3+
The JSON (and in future NDJSON) report formats allow the inclusion of data attachments.
4+
5+
These attachments could be console logs or file data or images for instance.
6+
7+
The example in this directory shows how the godog API is used to add attachments to the JSON report.
8+
9+
10+
## Run the example
11+
12+
You must use the '-v' flag or you will not see the cucumber JSON output.
13+
14+
go test -v atttachment_test.go
15+
16+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package attachments_test
2+
3+
// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
4+
// Sample commands:
5+
// * run all scenarios from default directory (features): go test -test.run "^TestFeatures/"
6+
// * run all scenarios and list subtest names: go test -test.v -test.run "^TestFeatures/"
7+
// * run all scenarios from one feature file: go test -test.v -godog.paths features/nodogs.feature -test.run "^TestFeatures/"
8+
// * run all scenarios from multiple feature files: go test -test.v -godog.paths features/nodogs.feature,features/godogs.feature -test.run "^TestFeatures/"
9+
// * run single scenario as a subtest: go test -test.v -test.run "^TestFeatures/Eat_5_out_of_12$"
10+
// * show usage help: go test -godog.help
11+
// * show usage help if there were other test files in directory: go test -godog.help godogs_test.go
12+
// * run scenarios with multiple formatters: go test -test.v -godog.format cucumber:cuc.json,pretty -test.run "^TestFeatures/"
13+
14+
import (
15+
"context"
16+
"os"
17+
"testing"
18+
19+
"github.com/cucumber/godog"
20+
"github.com/cucumber/godog/colors"
21+
)
22+
23+
var opts = godog.Options{
24+
Output: colors.Colored(os.Stdout),
25+
Format: "cucumber", // cucumber json format
26+
}
27+
28+
func TestFeatures(t *testing.T) {
29+
o := opts
30+
o.TestingT = t
31+
32+
status := godog.TestSuite{
33+
Name: "attachments",
34+
Options: &o,
35+
ScenarioInitializer: InitializeScenario,
36+
}.Run()
37+
38+
if status == 2 {
39+
t.SkipNow()
40+
}
41+
42+
if status != 0 {
43+
t.Fatalf("zero status code expected, %d received", status)
44+
}
45+
}
46+
47+
func InitializeScenario(ctx *godog.ScenarioContext) {
48+
49+
ctx.Step(`^I have attached two documents in sequence$`, func(ctx context.Context) (context.Context, error) {
50+
// the attached bytes will be base64 encoded by the framework and placed in the embeddings section of the cuke report
51+
ctx = godog.Attach(ctx,
52+
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment", MediaType: "text/plain"},
53+
)
54+
ctx = godog.Attach(ctx,
55+
godog.Attachment{Body: []byte("{ \"a\" : 1 }"), FileName: "Json Attachment", MediaType: "application/json"},
56+
)
57+
58+
return ctx, nil
59+
})
60+
ctx.Step(`^I have attached two documents at once$`, func(ctx context.Context) (context.Context, error) {
61+
ctx = godog.Attach(ctx,
62+
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment 1", MediaType: "text/plain"},
63+
godog.Attachment{Body: []byte("TheData2"), FileName: "Data Attachment 2", MediaType: "text/plain"},
64+
)
65+
66+
return ctx, nil
67+
})
68+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Feature: Attaching content to the cucumber report
2+
The cucumber JSON and NDJSON support the inclusion of attachments.
3+
These can be text or images or any data really.
4+
5+
Scenario: Attaching files to the report
6+
Given I have attached two documents in sequence
7+
And I have attached two documents at once

internal/formatters/fmt_cucumber.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type cukeStep struct {
154154
Match cukeMatch `json:"match"`
155155
Result cukeResult `json:"result"`
156156
DataTable []*cukeDataTableRow `json:"rows,omitempty"`
157-
Embeddings []*cukeEmbedding `json:"embeddings,omitempty"`
157+
Embeddings []cukeEmbedding `json:"embeddings,omitempty"`
158158
}
159159

160160
type cukeDataTableRow struct {
@@ -303,10 +303,10 @@ func (f *Cuke) buildCukeStep(pickle *messages.Pickle, stepResult models.PickleSt
303303
}
304304

305305
if stepResult.Attachments != nil {
306-
attachments := []*cukeEmbedding{}
306+
attachments := []cukeEmbedding{}
307307

308308
for _, a := range stepResult.Attachments {
309-
attachments = append(attachments, &cukeEmbedding{
309+
attachments = append(attachments, cukeEmbedding{
310310
Name: a.Name,
311311
Data: base64.RawStdEncoding.EncodeToString(a.Data),
312312
MimeType: a.MimeType,

internal/formatters/fmt_output_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919

2020
const fmtOutputTestsFeatureDir = "formatter-tests/features"
2121

22+
var tT *testing.T
23+
2224
func Test_FmtOutput(t *testing.T) {
25+
tT = t
2326
pkg := os.Getenv("GODOG_TESTED_PACKAGE")
2427
os.Setenv("GODOG_TESTED_PACKAGE", "github.com/cucumber/godog")
2528

@@ -64,7 +67,8 @@ func fmtOutputTest(fmtName, testName, featureFilePath string) func(*testing.T) {
6467
ctx.Step(`^(?:a )?pending step$`, pendingStepDef)
6568
ctx.Step(`^(?:a )?passing step$`, passingStepDef)
6669
ctx.Step(`^odd (\d+) and even (\d+) number$`, oddEvenStepDef)
67-
ctx.Step(`^(?:a )?a step with attachment$`, stepWithAttachment)
70+
ctx.Step(`^(?:a )?a step with a single attachment call for multiple attachments$`, stepWithSingleAttachmentCall)
71+
ctx.Step(`^(?:a )?a step with multiple attachment calls$`, stepWithMultipleAttachmentCalls)
6872
}
6973

7074
return func(t *testing.T) {
@@ -127,11 +131,29 @@ func pendingStepDef() error { return godog.ErrPending }
127131

128132
func failingStepDef() error { return fmt.Errorf("step failed") }
129133

130-
func stepWithAttachment(ctx context.Context) (context.Context, error) {
131-
ctxOut := godog.Attach(ctx,
134+
func stepWithSingleAttachmentCall(ctx context.Context) (context.Context, error) {
135+
if len(godog.Attachments(ctx)) > 0 {
136+
assert.FailNow(tT, "Unexpected Attachments found - should have been empty")
137+
}
138+
139+
ctx = godog.Attach(ctx,
132140
godog.Attachment{Body: []byte("TheData1"), FileName: "TheFilename1", MediaType: "text/plain"},
133141
godog.Attachment{Body: []byte("TheData2"), FileName: "TheFilename2", MediaType: "text/plain"},
134142
)
135143

136-
return ctxOut, nil
144+
return ctx, nil
145+
}
146+
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")
149+
}
150+
151+
ctx = godog.Attach(ctx,
152+
godog.Attachment{Body: []byte("TheData1"), FileName: "TheFilename1", MediaType: "text/plain"},
153+
)
154+
ctx = godog.Attach(ctx,
155+
godog.Attachment{Body: []byte("TheData2"), FileName: "TheFilename2", MediaType: "text/plain"},
156+
)
157+
158+
return ctx, nil
137159
}

internal/formatters/formatter-tests/cucumber/scenario_with_attachment

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"steps": [
1818
{
1919
"keyword": "Given ",
20-
"name": "a step with attachment",
20+
"name": "a step with a single attachment call for multiple attachments",
2121
"line": 7,
2222
"match": {
2323
"location": "fmt_output_test.go:119"
@@ -38,6 +38,30 @@
3838
"data": "VGhlRGF0YTI"
3939
}
4040
]
41+
},
42+
{
43+
"keyword": "And ",
44+
"name": "a step with multiple attachment calls",
45+
"line": 8,
46+
"match": {
47+
"location": "fmt_output_test.go:119"
48+
},
49+
"result": {
50+
"status": "passed",
51+
"duration": 0
52+
},
53+
"embeddings": [
54+
{
55+
"name": "TheFilename1",
56+
"mime_type": "text/plain",
57+
"data": "VGhlRGF0YTE"
58+
},
59+
{
60+
"name": "TheFilename2",
61+
"mime_type": "text/plain",
62+
"data": "VGhlRGF0YTI"
63+
}
64+
]
4165
}
4266
]
4367
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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 attachment\n"}
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"}
33
{"event":"TestCaseStarted","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871}
4-
{"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.stepWithAttachment","arguments":[]}
4+
{"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}
66
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
77
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"}
88
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:7","timestamp":-6795364578871,"status":"passed"}
9+
{"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":[]}
10+
{"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":"TheFilename1","mimeType":"text/plain","body":"TheData1"}
12+
{"event":"Attachment","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"contentEncoding":"BASE64","fileName":"TheFilename2","mimeType":"text/plain","body":"TheData2"}
13+
{"event":"TestStepFinished","location":"formatter-tests/features/scenario_with_attachment.feature:8","timestamp":-6795364578871,"status":"passed"}
914
{"event":"TestCaseFinished","location":"formatter-tests/features/scenario_with_attachment.feature:6","timestamp":-6795364578871,"status":"passed"}
1015
{"event":"TestRunFinished","status":"passed","timestamp":-6795364578871,"snippets":"","memory":""}

internal/formatters/formatter-tests/features/scenario_with_attachment.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Feature: scenario with attachment
44
feature
55

66
Scenario: step with attachment
7-
Given a step with attachment
7+
Given a step with a single attachment call for multiple attachments
8+
And a step with multiple attachment calls

0 commit comments

Comments
 (0)