Skip to content

Commit

Permalink
fix(fixture 🐛): make @betterer/fixture throw on duplicate name (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
phenomnomnominal authored Oct 12, 2020
1 parent a7fe7cd commit 9fb0357
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/fixture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"dependencies": {
"@betterer/betterer": "^3.0.3",
"@betterer/logger": "^3.0.0",
"@betterer/errors": "^3.1.0",
"ansi-regex": "^5.0.0",
"fs-extra": "^9.0.1",
"graceful-fs": "^4.2.3"
Expand Down
20 changes: 9 additions & 11 deletions packages/fixture/src/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { BettererRunNames, BettererRuns, BettererSummary } from '@betterer/betterer';
import { warnΔ } from '@betterer/logger';
import { registerError } from '@betterer/errors';
import { promises as fs } from 'graceful-fs';
import * as path from 'path';

import { createFixtureFS } from './fs';
import { createFixtureLogs } from './logging';
import { Fixture, FixtureFactory, FixtureFileSystemFiles } from './types';

const DUPLICATE_FIXTURE_NAME = registerError(
(fixtureName) => `There is already a fixture in use called "${fixtureName.toString()}"`
);

export async function createFixtureDirectoryΔ(fixturesPath: string): Promise<FixtureFactory> {
try {
await fs.mkdir(fixturesPath);
Expand All @@ -15,23 +19,17 @@ export async function createFixtureDirectoryΔ(fixturesPath: string): Promise<Fi
}

return async function createFixtureΔ(fixtureName: string, files: FixtureFileSystemFiles): Promise<Fixture> {
try {
const fixtureNames = await fs.readdir(fixturesPath);
if (fixtureNames.includes(fixtureName)) {
warnΔ(`There is already a fixture in use called "${fixtureName}"`);
}
} catch {
// Move on...
const fixtureNames = await fs.readdir(fixturesPath);
if (fixtureNames.includes(fixtureName)) {
throw DUPLICATE_FIXTURE_NAME(fixtureName);
}

const fixturePath = path.resolve(fixturesPath, fixtureName);
const fixtureFS = await createFixtureFS(fixturePath, files);
const fixtureLogs = createFixtureLogs();

// Wait long enough that the watch mode debounce doesn't get in the way:
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await new Promise((resolve) => setTimeout(resolve, 500));

return {
...fixtureFS,
Expand Down

0 comments on commit 9fb0357

Please sign in to comment.