Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle output in json-formatter #894

Merged

Conversation

vincent-psarga
Copy link
Contributor

Summary

JSON formatter currently does not handle output field in the generated json files. This field is populated in the steps by calling puts(in Ruby, there must be equivalents elsewhere).

The idea here is to use a specific attachment type (text/output) which won't end up in the attachments.
Of course we should not ask the users to handle the output type themselves, but we should add this in the various cucumber flavours.

For example, in cucumber-ruby, we could have this the messages formatter:

      def puts(message)
        embed(message, 'text/output')
      end

I'm not 100% sure of the way to use attachments for output, so any better idea is more than welcome :)

Details

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • The change has been ported to Java.
  • The change has been ported to Ruby.
  • The change has been ported to JavaScript.
  • The change has been ported to Go.
  • The change has been ported to .NET.
  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have updated the CHANGELOG accordingly.

@vincent-psarga vincent-psarga added language: go json-formatter The `cucumber-json-formatter` executable labels Feb 10, 2020
@@ -8,6 +8,10 @@ Given('a passed {word} with text attachment {string}', function(word: string, at
this.attach(attachmentText, 'text/plain')
})

Given('a step outputs:', function(txt: string) {
this.attach(txt, 'text/output')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text/output is not a valid media type. It should be text/plain. Alternatively, it should be an extension - I suggest text/x.cucumber.output+plain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds fine, I was definitely not convinced by text/output anyway, sounded too hacky :D

@vincent-psarga
Copy link
Contributor Author

vincent-psarga commented Feb 11, 2020

Note, I'm also wondering about a good way to implement this in various languages. From what I've seen, in Ruby, we define puts on the JSON formatter.
In Javascript, I haven't found a way to get output field set in the JSON formatter.
In Java, I found createWriteHookAction which seems to do the same (although limited to Hooks from what I understood when glancing at the code).

What I think should be pretty useful, would be to have, for each languages, two ways to add output:

  • a generic one (so the API between all cucumber implementation would be the same), which we could call output (or anything better works for me)
  • a way that uses name more coupled to the language.

For example, this could be:

# cucumber-ruby message formatter
class MessageFormatter
  def output(text)
    # It's called embed in cucumber-ruby but let's forget it for now
    attach(text, 'text/x.cucumber.output+plain')
  end

  def puts(text)
    output(text)
  end
end
// cucumber-js message formatter
class MessageFormatter implements Formatter {
  public output(text: string): void {
    this.attach(text, 'text/x.cucumber.output+plain')
  }

  // .log as in console.log
  public log(text: string): void {
    this.output(text)
  }
} 
// cucumber-jvm message formatter
class MessageFormatter {
  public void output(String text) {
    this.attach(text, "text/x.cucumber.output+plain")
  }

  // .println as in System.out.println
  public void println(String text) {
    this.output(text);
  }
}

@aslakhellesoy
Copy link
Contributor

The user isn't invoking the attach / embed / puts methods on the formatter instance - they don't have access to that. In Ruby/Java, the method is on the World object (self/this), and in Cucumber-JVM it is on the scenario object, which is passed to Before hooks that declare a Scenario parameter.

Let's discuss this on a call.

@vincent-psarga
Copy link
Contributor Author

See #897

@vincent-psarga vincent-psarga merged commit 1c42755 into master Feb 21, 2020
@vincent-psarga vincent-psarga deleted the json-formatter-handle-text-attachments-as-output branch February 21, 2020 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json-formatter The `cucumber-json-formatter` executable language: go
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants