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

fix(coverage): prevent reportsDirectory from removing user's project #5376

Merged
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
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,10 @@ Clean coverage report on watch rerun
- **Available for providers:** `'v8' | 'istanbul'`
- **CLI:** `--coverage.reportsDirectory=<path>`

::: warning
Vitest will delete this directory before running tests if `coverage.clean` is enabled (default value).
:::

Directory to write coverage report to.

To preview the coverage report in the output of [HTML reporter](/guide/reporters.html#html-reporter), this option must be set as a sub-directory of the html report directory (for example `./html/coverage`).
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,21 @@ export function resolveConfig(
}
}

// TODO: V2.0.0 remove
// @ts-expect-error -- check for removed API option
if (resolved.coverage.provider === 'c8')
throw new Error('"coverage.provider: c8" is not supported anymore. Use "coverage.provider: v8" instead')

if (resolved.coverage.provider === 'v8' && resolved.coverage.enabled && isBrowserEnabled(resolved))
throw new Error('@vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')

if (resolved.coverage.enabled && resolved.coverage.reportsDirectory) {
const reportsDirectory = resolve(resolved.root, resolved.coverage.reportsDirectory)

if (reportsDirectory === resolved.root || reportsDirectory === process.cwd())
Comment on lines +147 to +149
Copy link
Contributor

@hi-ogawa hi-ogawa Mar 14, 2024

Choose a reason for hiding this comment

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

Maybe check also parent directory like ../?
Using root.startsWith(reportsDirectory) can cover root === reportsDirectory as a special case.

(Ah, I guess startsWith might not be it since "/some/directory".startsWith("/some/dir") is a false positive.)

Copy link
Member

Choose a reason for hiding this comment

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

We can check startsWith(reportsDirectory + '/')

Copy link
Member Author

@AriPerkkio AriPerkkio Mar 14, 2024

Choose a reason for hiding this comment

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

Sounds good. We can also add this improvement later on if you want to include this PR as is in the release @sheremet-va.

throw new Error(`You cannot set "coverage.reportsDirectory" as ${reportsDirectory}. Vitest needs to be able to remove this directory before test run`)
}

resolved.deps ??= {}
resolved.deps.moduleDirectories ??= []
resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map((dir) => {
Expand Down
38 changes: 38 additions & 0 deletions test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from 'vitest'
import type { UserConfig } from 'vitest/config'
import { version } from 'vitest/package.json'

import { normalize, resolve } from 'pathe'
import * as testUtils from '../../test-utils'

function runVitest(config: NonNullable<UserConfig['test']> & { shard?: any }) {
Expand Down Expand Up @@ -67,6 +68,43 @@ test('v8 coverage provider cannot be used with browser in workspace', async () =
expect(stderr).toMatch('Error: @vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')
})

test('coverage reportsDirectory cannot be current working directory', async () => {
const { stderr } = await runVitest({
coverage: {
enabled: true,
reportsDirectory: './',

// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})

const directory = normalize(resolve('./'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})

test('coverage reportsDirectory cannot be root', async () => {
const { stderr } = await runVitest({
root: './fixtures',
coverage: {
enabled: true,
reportsDirectory: './',

// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})

const directory = normalize(resolve('./fixtures'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})

test('version number is printed when coverage provider fails to load', async () => {
const { stderr, stdout } = await runVitest({
coverage: {
Expand Down
Loading