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 all 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 now loaded via the regular require paths relative to the current directory, unless it begins with a dot (e.g. `--format=./relpath/to/formatter`). Previously this was always loaded as a file relative to the current directory.

### 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.1",
"duration": "^0.2.2",
"durations": "^3.4.2",
"figures": "^3.2.0",
Expand Down
5 changes: 3 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,8 @@ 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
const CustomFormatter = createRequire(cwd)(customFormatterPath)

if (typeof CustomFormatter === 'function') {
return CustomFormatter
} else if (
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.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

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