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

Cucumber now creates whatever directories are necessary to make a report #2266

Merged
merged 8 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber.

## [Unreleased]

- Formatters create sub-directory automatically instead of failing ([#2266](https://github.com/cucumber/cucumber-js/pull/2266))
## [9.0.1] - 2023-03-15
### Fixed
- Ensure feature paths are properly deduplicated ([#2258](https://github.com/cucumber/cucumber-js/pull/2258))
Expand Down
4 changes: 2 additions & 2 deletions docs/formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For each value you provide, `TYPE` should be one of:
* A relative path to a local formatter implementation e.g. `./my-customer-formatter.js`
* An absolute path to a local formatter implementation in the form of a `file://` URL

If `PATH` is supplied, the formatter prints to the given file, otherwise it prints to `stdout`.
If `PATH` is supplied, the formatter prints to the given file, otherwise it prints to `stdout`. If the path includes directories that do not yet exist they will be created.

For example, this configuration would give you a progress bar as you run, plus JSON and HTML report files:

Expand All @@ -30,7 +30,7 @@ Some notes on specifying Formatters:

## Options

Many formatters, including the built-in ones, support some configurability via options. You can provide this data as an object literal via the `formatOptions` configuration option, like this:
Many formatters, including the built-in ones, support some configuration via options. You can provide this data as an object literal via the `formatOptions` configuration option, like this:

- In a configuration file `{ formatOptions: { someOption: true } }`
- On the CLI `$ cucumber-js --format-options '{"someOption":true}'`
Expand Down
12 changes: 7 additions & 5 deletions features/formatter_paths.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ Feature: Formatter Paths
<duration-stat>
"""

Scenario: Invalid path
When I run cucumber-js with `-f summary:invalid/summary.txt`
Then it fails
And the error output contains the text:
Scenario: Created relative path
When I run cucumber-js with `-f summary:some/long/path/for/reports/summary.txt`
Then the file "some/long/path/for/reports/summary.txt" has the text:
"""
ENOENT
1 scenario (1 passed)
1 step (1 passed)
<duration-stat>
"""

20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
"lodash.merge": "^4.6.2",
"lodash.mergewith": "^4.6.2",
"luxon": "3.2.1",
"mkdirp": "^2.1.5",
"mz": "^2.7.0",
"progress": "^2.0.3",
"resolve-pkg": "^2.0.0",
Expand Down
29 changes: 23 additions & 6 deletions src/api/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'mz/fs'
import path from 'path'
import { IRunOptionsFormats } from './types'
import { ILogger } from '../logger'
import { mkdirp } from 'mkdirp'

export async function initializeFormatters({
env,
Expand Down Expand Up @@ -70,12 +71,28 @@ export async function initializeFormatters({
await initializeFormatter(stdout, 'stdout', configuration.stdout)
)

for (const [target, type] of Object.entries(configuration.files)) {
const stream: IFormatterStream = fs.createWriteStream(null, {
fd: await fs.open(path.resolve(cwd, target), 'w'),
})
formatters.push(await initializeFormatter(stream, target, type))
}
const streamPromises: Promise<void>[] = []

Object.entries(configuration.files).forEach(([target, type]) => {
streamPromises.push(
(async (target, type) => {
const absoluteTarget = path.resolve(cwd, target)

try {
await mkdirp(path.dirname(absoluteTarget))
} catch (error) {
logger.warn('Failed to ensure directory for formatter target exists')
}

const stream: IFormatterStream = fs.createWriteStream(null, {
fd: await fs.open(absoluteTarget, 'w'),
})
formatters.push(await initializeFormatter(stream, target, type))
})(target, type)
)
})

await Promise.all(streamPromises)

return async function () {
await Promise.all(formatters.map(async (f) => await f.finished()))
Expand Down
8 changes: 4 additions & 4 deletions src/formatter/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ const FormatterBuilder = {
descriptor: string,
cwd: string
) {
let normalised: URL | string = descriptor
let normalized: URL | string = descriptor
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
if (descriptor.startsWith('.')) {
normalised = pathToFileURL(path.resolve(cwd, descriptor))
normalized = pathToFileURL(path.resolve(cwd, descriptor))
} else if (descriptor.startsWith('file://')) {
normalised = new URL(descriptor)
normalized = new URL(descriptor)
}
let CustomClass = await FormatterBuilder.loadFile(normalised)
let CustomClass = await FormatterBuilder.loadFile(normalized)
CustomClass = FormatterBuilder.resolveConstructor(CustomClass)
if (doesHaveValue(CustomClass)) {
return CustomClass
Expand Down