Skip to content

Commit

Permalink
Store t.testdir() fixtures in .tap/fixtures
Browse files Browse the repository at this point in the history
Fix: #883

Much better location for this stuff.
  • Loading branch information
isaacs committed Sep 10, 2023
1 parent 6914d23 commit a5dc854
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 32 deletions.
10 changes: 7 additions & 3 deletions src/create-plugin/src/index.ts
Expand Up @@ -2,7 +2,7 @@
import { readFileSync } from 'node:fs'
import { resolveImport } from 'resolve-import'
import { randomInt } from 'node:crypto'
import { basename } from 'node:path'
import { basename, dirname } from 'node:path'
import { Init } from 'npm-init-template'

const corePkg = JSON.parse(
Expand Down Expand Up @@ -41,9 +41,13 @@ const pkgName = (s: string) => {
'/' +
slugify(sp.slice(1).join('/'))
)
} else {
return slugify(s)
}
const base = basename(s)
const dir = basename(dirname(s))
if (dir !== '.') {
return `@${slugify(dir)}/${slugify(base)}`
}
return slugify(base)
}

const defName =
Expand Down
2 changes: 1 addition & 1 deletion src/create-plugin/test/index.ts
Expand Up @@ -195,7 +195,7 @@ t.test('actually build and verify', async t => {
Buffer.concat(err).toString(),
],
[
`Plugin package name: (test-tap-testdir-index-actually-build-and-verify) Plugin description: Run npm install? (Yes) Run git init? (Yes) Folder to create in? (test/tap-testdir-index-actually-build-and-verify)
`Plugin package name: (@fixtures/test-index.ts-actually-build-and-verify) Plugin description: Run npm install? (Yes) Run git init? (Yes) Folder to create in? (.tap/fixtures/test-index.ts-actually-build-and-verify)
Done!
Stubbed source at ${dir}/src/index.ts
Expand Down
17 changes: 17 additions & 0 deletions src/fixture/README.md
Expand Up @@ -4,6 +4,23 @@ A default tap plugin providing `t.testdir()` and `t.fixture()`
methods, for creating temporary directories with stuff for tests
to operate on.

Fixtures created with this plugin live folders under
`./.tap/fixtures`, in the root of the project. The folder name is
based on the name of the test file, and the name of the test.

For example, if a file at `./src/foo.test.mjs` had this:

```js
import t from 'tap'
t.test('child test', async t => {
t.testdir({ file: 'contents' })
})
```

Then a file would be created at
`.tap/fixtures/src-foo.test.mjs-child-test/file` containing
`'contents'`.

## USAGE

This plugin is installed with tap by default. If you had
Expand Down
25 changes: 13 additions & 12 deletions src/fixture/src/index.ts
@@ -1,12 +1,13 @@
// module code goes here
import {
argv,
cwd,
env,
mainScript,
TapPlugin,
TestBase,
} from '@tapjs/core'
import { basename, dirname, resolve, sep } from 'node:path'
import { basename, dirname, relative, resolve } from 'node:path'
import { rimraf, rimrafSync } from 'rimraf'
import {
Fixture,
Expand All @@ -19,7 +20,7 @@ export * from './fixture.js'
export interface TestFixturesOptions {
/**
* Directory to store test fixtures.
* Defaults to `./tap-testdir-${test name}`
* Defaults to `./.tap/fixtures/${test name}`
*/
testdir?: string

Expand Down Expand Up @@ -111,8 +112,7 @@ export class TestFixtures {
* {@link @tapjs/fixture!index.TestFixtures#testdir}.
*
* By default, it uses a folder name based on the name of the test file
* and subtest, prepended with `tap-testdir-...`, which is automatically
* omitted from consideration in coverage and other analysis.
* and subtest, within \`.tap/fixtures\` in the root of the project.
*
* @group Spies, Mocks, and Fixtures
*/
Expand All @@ -132,18 +132,18 @@ export class TestFixtures {
const p = t.parent && TestFixtures.#refs.get(t.parent)
if (!p) {
const main = mainScript()
// put in a prefix in the dirname so do not inadvertently run it
// on a subsequent tap invocation, if it was saved.
const dir = dirname(main)
const base = [
'tap-testdir',
basename(main).replace(/\.[^.]+$/, ''),
/* c8 ignore start */
const root = env.TAP_CWD || cwd
/* c8 ignore stop */
const name = [
dirname(relative(root, main)),
basename(main),
...argv.slice(2),
]
.join(' ')
.trim()
.replace(re, '-')
return dir + sep + base
return resolve(root, '.tap/fixtures', name)
}

/* c8 ignore start */
Expand Down Expand Up @@ -175,6 +175,7 @@ export const config = {
'save-fixture': {
type: 'boolean',
short: 'F',
description: 'Do not clean up fixtures created with `t.testdir()`',
description:
'Do not clean up fixtures created with `t.testdir()`',
},
}
4 changes: 2 additions & 2 deletions src/npm-init-template/src/index.ts
Expand Up @@ -260,8 +260,8 @@ export class Init {
? resolve(target, f)
: resolve(target, p.dir, p.name)
if (
exc.some(p => minimatch(tf, p)) ||
!inc.some(p => minimatch(tf, p))
exc.some(p => minimatch(relative(target, tf), p)) ||
!inc.some(p => minimatch(relative(target, tf), p))
) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion src/run/src/list.ts
Expand Up @@ -17,7 +17,7 @@ const alwaysExcludeNames = [

const alwaysExcludePattern = `**/@(${alwaysExcludeNames.join(
'|'
)}|tap-testdir-*)/**`
)})/**`

const defaultInclude =
'**/{' +
Expand Down
2 changes: 1 addition & 1 deletion src/run/src/run.ts
Expand Up @@ -39,7 +39,7 @@ export const run = async (args: string[], config: LoadedConfig) => {
// always think that all tests are new after generating!
// TODO: these come from plugins, they should be able to export
// this, like with testFileExtensions.
/(^(node|tapmock):|[\\\/](tap-testdir-[^\\\/]+|tap-snapshots)[\\\/])/
/(^(node|tapmock):|[\\\/](\.tap|tap-snapshots)[\\\/])/
)

const argv = testArgv(config)
Expand Down
4 changes: 2 additions & 2 deletions src/run/tap-snapshots/test/plugin.ts.test.cjs
Expand Up @@ -11,7 +11,7 @@ Array [
"successfully added plugin(s):",
],
Array [
"{CWD}/test/tap-testdir-plugin-adding-plugins-add-a-plugin-that-is-a-file-on-disk/my-plugin",
"{CWD}/.tap/fixtures/test-plugin.ts-adding-plugins-add-a-plugin-that-is-a-file-on-disk/my-plugin",
],
]
`
Expand All @@ -21,7 +21,7 @@ Array [
Array [
"adding plugins:",
Array [
"{CWD}/test/tap-testdir-plugin-adding-plugins-add-a-plugin-that-is-a-file-on-disk/my-plugin",
"{CWD}/.tap/fixtures/test-plugin.ts-adding-plugins-add-a-plugin-that-is-a-file-on-disk/my-plugin",
],
],
]
Expand Down
9 changes: 4 additions & 5 deletions src/run/test/list.ts
Expand Up @@ -35,9 +35,6 @@ t.test('list some test files', async t => {
sub: {
'sub.mts': '',
},
'tap-testdir-foo.mts': {
'test.js': '',
},
// fixtures excluded by default
fixture: {
'test.js': '',
Expand Down Expand Up @@ -223,9 +220,11 @@ t.test('list some test files', async t => {
t.test('filter changed files', async t => {
// testdirs are excluded from file tracking by default, so
// use a different directory name.
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')

const fixture = fileURLToPath(new URL('fixtures/project', import.meta.url))
const fixture = fileURLToPath(
new URL('fixtures/project', import.meta.url)
)
const cwd = process.cwd()

const files = (dir: string): Record<string, Buffer> =>
Expand Down
2 changes: 1 addition & 1 deletion src/run/test/repl/index.ts
Expand Up @@ -329,7 +329,7 @@ ok 2 - other assertion

t.test('run the suite', async t => {
// testdirs are excluded file file tracking, so use a different name
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')

const dir = t.testdir({
'.tap': {
Expand Down
8 changes: 4 additions & 4 deletions src/run/test/run.ts
Expand Up @@ -127,7 +127,7 @@ t.test('fail to find all named test files', async t => {

t.test('set test envs', async t => {
// testdirs are excluded by default, so use a different name
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')
const cwd = t.testdir({
'env.test.js': `
import t from 'tap'
Expand Down Expand Up @@ -165,7 +165,7 @@ coverage-map: map.js
})

t.test('save test failures', async t => {
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')
const cwd = t.testdir({
'failer.test.js': `
import t from 'tap'
Expand Down Expand Up @@ -256,7 +256,7 @@ ok 1 - this is standard input

t.test('no files found to run', async t => {
// testdirs are excluded by default, so use a different name
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')

const cwd = t.testdir({
'.taprc': `
Expand Down Expand Up @@ -354,7 +354,7 @@ coverage-map: map.mjs

t.test('run stdin with a file', async t => {
// testdirs are excluded by default, so use a different name
t.testdirName = t.testdirName.replace(/tap-testdir/, 'XXX-testdir')
t.testdirName = t.testdirName.replace(/\.tap[\\\/]fixtures/, 'XXX')

const cwd = t.testdir({
'env.test.js': `
Expand Down

0 comments on commit a5dc854

Please sign in to comment.