Search code examples
dockercypress-cucumber-preprocessor

container to pipe to an executable and out again


I need a Docker image which contains a Linux executable where I can pipe an input file into when running it like this (in cmd on Windows or bash on Linux):

docker run --rm -i myContainer < myInputFile > myOutputFile

In my case it is the executable cucumber-json-formatter-linux-386 which converts NDJSON Cucumber messages into a JSON file (to be used e.g. for Xray).

Why Docker?

  • There is also a Windows executable available, but our security software says it contains a virus.
  • So I can’t download it, but to be honest, I also don’t trust it.
  • I need something OS indepedent, also running in CI, etc.

Why convert?

Cucumber can output JSON directly. But I am using @badeball/cypress-cucumber-preprocessor which only can output NDJSON and needs me to use that binary to convert it into JSON. @badeball wrote “I've refactored my implementation to output messages, because that was significantly easier implementation wise”. So it’s not Cucumber writing those NDJSON messages here.


Solution

  • Assuming you already have installed Docker, create a folder containing the following file called “Dockerfile”:

    FROM scratch
    COPY cucumber-json-formatter-linux-386 /formatter
    ENTRYPOINT ["/formatter"]
    

    Also copy the Linux executable (cucumber-json-formatter-linux-386) into the same folder.

    Then cd into that folder and build the container like this:

    docker build -t cucumber-json-formatter .
    

    Now you can run the container like this:

    docker run --rm -i cucumber-json-formatter < input.ndjson > output.json
    
    • The argument --rm removes the container after usage. Do this if you don't want a pile of dead containers to build up over time.
    • The argument -i is needed to keep the input stream open, while the file is piped in.

    I tried this on a Windows host (cmd, not Powershell), but should work the same on Linux.

    If you want to use it automatically in @badeball/cypress-cucumber-preprocessor add a file .cypress-cucumber-preprocessorrc.json on your project root, containing:

    {
        "json": {
            "enabled": true,
            "formatter": "docker",
            "args": ["run", "--rm", "-i", "cucumber-json-formatter"]
        }
    }
    

    On test execution this will create a file cucumber-messages.ndjson and then cucumber-report.json, both on the project root.