Skip to content

Commit

Permalink
applied code review comments also added _example/attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnlon committed May 30, 2024
1 parent 276cca3 commit 137cbf3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ When steps are orthogonal and small, you can combine them just like you do with

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

### Attachments

An example showing how to make attachments (aka embeddings) to the results is shown in [_examples/attachments](/_examples/attachments/)

## Code of Conduct

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).
Expand Down
16 changes: 16 additions & 0 deletions _examples/attachments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# An example of Making attachments to the reports

The JSON (and in future NDJSON) report formats allow the inclusion of data attachments.

These attachments could be console logs or file data or images for instance.

The example in this directory shows how the godog API is used to add attachments to the JSON report.


## Run the example

You must use the '-v' flag or you will not see the cucumber JSON output.

go test -v atttachment_test_go


68 changes: 68 additions & 0 deletions _examples/attachments/attachments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package attachments_test

// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
// Sample commands:
// * run all scenarios from default directory (features): go test -test.run "^TestFeatures/"
// * run all scenarios and list subtest names: go test -test.v -test.run "^TestFeatures/"
// * run all scenarios from one feature file: go test -test.v -godog.paths features/nodogs.feature -test.run "^TestFeatures/"
// * run all scenarios from multiple feature files: go test -test.v -godog.paths features/nodogs.feature,features/godogs.feature -test.run "^TestFeatures/"
// * run single scenario as a subtest: go test -test.v -test.run "^TestFeatures/Eat_5_out_of_12$"
// * show usage help: go test -godog.help
// * show usage help if there were other test files in directory: go test -godog.help godogs_test.go
// * run scenarios with multiple formatters: go test -test.v -godog.format cucumber:cuc.json,pretty -test.run "^TestFeatures/"

import (
"context"
"os"
"testing"

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
)

var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "cucumber", // cucumber json format
}

func TestFeatures(t *testing.T) {
o := opts
o.TestingT = t

status := godog.TestSuite{
Name: "attachments",
Options: &o,
ScenarioInitializer: InitializeScenario,
}.Run()

if status == 2 {
t.SkipNow()
}

if status != 0 {
t.Fatalf("zero status code expected, %d received", status)
}
}

func InitializeScenario(ctx *godog.ScenarioContext) {

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
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment", MediaType: "text/plain"},
)
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("{ \"a\" : 1 }"), FileName: "Json Attachment", MediaType: "application/json"},
)

return ctx, nil
})
ctx.Step(`^I have attached two documents at once$`, func(ctx context.Context) (context.Context, error) {
ctx = godog.Attach(ctx,
godog.Attachment{Body: []byte("TheData1"), FileName: "Data Attachment 1", MediaType: "text/plain"},
godog.Attachment{Body: []byte("TheData2"), FileName: "Data Attachment 2", MediaType: "text/plain"},
)

return ctx, nil
})
}
7 changes: 7 additions & 0 deletions _examples/attachments/features/attachments.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Attaching content to the cucumber report
The cucumber JSON and NDJSON support the inclusion of attachments.
These can be text or images or any data really.

Scenario: Attaching files to the report
Given I have attached two documents in sequence
And I have attached two documents at once
6 changes: 3 additions & 3 deletions internal/formatters/fmt_cucumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ type cukeStep struct {
Match cukeMatch `json:"match"`
Result cukeResult `json:"result"`
DataTable []*cukeDataTableRow `json:"rows,omitempty"`
Embeddings []*cukeEmbedding `json:"embeddings,omitempty"`
Embeddings []cukeEmbedding `json:"embeddings,omitempty"`
}

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

if stepResult.Attachments != nil {
attachments := []*cukeEmbedding{}
attachments := []cukeEmbedding{}

for _, a := range stepResult.Attachments {
attachments = append(attachments, &cukeEmbedding{
attachments = append(attachments, cukeEmbedding{
Name: a.Name,
Data: base64.RawStdEncoding.EncodeToString(a.Data),
MimeType: a.MimeType,
Expand Down
4 changes: 2 additions & 2 deletions internal/models/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ type PickleStepResult struct {

Def *StepDefinition

Attachments []*PickleAttachment
Attachments []PickleAttachment
}

// NewStepResult ...
func NewStepResult(
status StepResultStatus,
pickleID, pickleStepID string,
match *StepDefinition,
attachments []*PickleAttachment,
attachments []PickleAttachment,
err error,
) PickleStepResult {
return PickleStepResult{
Expand Down
6 changes: 3 additions & 3 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func clearAttach(ctx context.Context) context.Context {
return context.WithValue(ctx, attachmentKey{}, nil)
}

func pickleAttachments(ctx context.Context) []*models.PickleAttachment {
func pickleAttachments(ctx context.Context) []models.PickleAttachment {

pickledAttachments := []*models.PickleAttachment{}
pickledAttachments := []models.PickleAttachment{}
attachments := Attachments(ctx)

for _, a := range attachments {
pickledAttachments = append(pickledAttachments, &models.PickleAttachment{
pickledAttachments = append(pickledAttachments, models.PickleAttachment{

Check warning on line 104 in suite.go

View check run for this annotation

Codecov / codecov/patch

suite.go#L104

Added line #L104 was not covered by tests
Name: a.FileName,
Data: a.Body,
MimeType: a.MediaType,
Expand Down

0 comments on commit 137cbf3

Please sign in to comment.