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

Load custom formatter by relative path only if it begins with a dot #1413

Merged
merged 6 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ If anything is missing from the migration guide, please submit an issue.
* Add `transpose` method to [data table interface](docs/support_files/data_table_interface.md)
* Add `log` function to world, providing a shorthand to log plain text as [attachment(s)](docs/support_files/attachments.md)
* Now includes [TypeScript](https://www.typescriptlang.org/) type definitions, deprecating the need for `@types/cucumber` in TypeScript projects
* Yarn PnP can now be used with this project with custom formatters [#1413](https://github.com/cucumber/cucumber-js/pull/1413)

### Breaking changes

Expand All @@ -56,6 +57,7 @@ If anything is missing from the migration guide, please submit an issue.
* Custom formatters will need to migrate
* `json` formatter is deprecated and will be removed in next major release. Custom formatters should migrate to use the `message` formatter, or the [standalone JSON formatter](https://github.com/cucumber/cucumber/tree/master/json-formatter) as a stopgap.
* Remove long-deprecated `typeName` from options object for `defineParameterType` in favour of `name`
* Custom formatters are no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`).
Copy link

Choose a reason for hiding this comment

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

NIT: They are always loaded relative to the cwd now

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, the note being that it uses the regular require search path relative to cwd now, instead of always trying to load a file by path from the cwd

Copy link
Member Author

Choose a reason for hiding this comment

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

Hopefully that's a bit clearer now.


### Bug fixes

Expand Down
19 changes: 6 additions & 13 deletions features/support/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ export class World {
envOverride: NodeJS.ProcessEnv = null
): Promise<void> {
const messageFilename = 'message.ndjson'
const args = ['node', executablePath]
.concat(inputArgs, [
'--backtrace',
'--predictable-ids',
'--format',
`message:${messageFilename}`,
])
.map((arg) => {
if (_.includes(arg, '/')) {
return path.normalize(arg)
}
return arg
})
const args = ['node', executablePath].concat(inputArgs, [
'--backtrace',
'--predictable-ids',
'--format',
`message:${messageFilename}`,
])
const env = _.merge({}, process.env, this.sharedEnv, envOverride)
const cwd = this.tmpDir

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
"cli-table3": "^0.6.0",
"colors": "^1.4.0",
"commander": "^5.0.0",
"create-require": "^1.1.0",
"duration": "^0.2.2",
"durations": "^3.4.2",
"figures": "^3.2.0",
Expand Down
7 changes: 5 additions & 2 deletions src/formatter/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Writable as WritableStream } from 'stream'
import { IParsedArgvFormatOptions } from '../cli/argv_parser'
import { SnippetInterface } from './step_definition_snippet_builder/snippet_syntax'
import HtmlFormatter from './html_formatter'
import createRequire from 'create-require'

interface IGetStepDefinitionSnippetBuilderOptions {
cwd: string
Expand Down Expand Up @@ -107,8 +108,10 @@ const FormatterBuilder = {
},

loadCustomFormatter(customFormatterPath: string, cwd: string) {
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
const CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
let CustomFormatter = null

CustomFormatter = createRequire(cwd)(customFormatterPath)
Copy link

Choose a reason for hiding this comment

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

Suggested change
let CustomFormatter = null
CustomFormatter = createRequire(cwd)(customFormatterPath)
const CustomFormatter = createRequire(cwd)(customFormatterPath)

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed


if (typeof CustomFormatter === 'function') {
return CustomFormatter
} else if (
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es2017"],
"lib": ["es2017", "dom"],
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
"module": "commonjs",
"noImplicitAny": true,
"noImplicitReturns": true,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

create-require@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.0.tgz#8fe85b319928953deef6f0e5c44bfea13e3c81ef"
integrity sha512-yEFVS7dQjDXp5iOEtWisN4uFmL+pUTyIaEizKda9Eb77XX58p6pgFOLAPaBCP+IR6ZPZ1jgJLAuf+ABk0zXYBQ==

cross-spawn@^7.0.0, cross-spawn@^7.0.2:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down