Skip to content

Commit

Permalink
Add some fixture utils and assert that the .prettierrc.js is generate…
Browse files Browse the repository at this point in the history
…d correctly for all tests, ensuring that the fixture utils work
  • Loading branch information
NullVoxPopuli committed Jul 14, 2023
1 parent a263708 commit 6a3683e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/
1 change: 1 addition & 0 deletions tests/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/
44 changes: 43 additions & 1 deletion tests/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fse from 'fs-extra';
import fs from 'node:fs/promises';
import path from 'node:path';
import { expect } from 'vitest';

import { packageJsonAt } from './utils.js';
import { fixture, packageJsonAt } from './utils.js';

interface AssertGeneratedOptions {
projectRoot: string;
Expand Down Expand Up @@ -50,4 +51,45 @@ export async function assertGeneratedCorrectly({

expect(await fse.pathExists(pathToFile), `${pathToFile} exists`).toBe(true);
}

await matchesFixture('.prettierrc.js', { cwd: projectRoot });
}

export async function matchesFixture(
/**
* Project-relative file to test against
*/
testFilePath: string,
options?: {
/**
* Which fixture set to use
*/
scenario?: string;
/**
* By default, the file used will be the same as the testFilePath, but
* in the fixtures directory under the (maybe) specified scenario.
* this can be overridden, if needed.
* (like if you're testFilePath is deep with in an existing monorepo, and wouldn't
* inherently match our default-project structure used in the fixtures)
*/
file?: string;

/**
* The working directory to use for the relative paths. Defaults to process.cwd() (node default)
*/
cwd?: string;
}
) {
let scenario = options?.scenario ?? 'default';
let fixtureFile = options?.file ?? testFilePath;

if (options?.cwd) {
testFilePath = path.join(options.cwd, testFilePath);
fixtureFile = path.join(options.cwd, fixtureFile);
}

let sourceContents = await fs.readFile(testFilePath);
let fixtureContents = await fixture(fixtureFile, { scenario });

expect(sourceContents).to.equal(fixtureContents, `${testFilePath} matches ${fixtureFile}`);
}
5 changes: 5 additions & 0 deletions tests/fixtures/default/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
singleQuote: true,
};
20 changes: 20 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,29 @@ import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const blueprintPath = path.join(__dirname, '..');
const fixturesPath = path.join(__dirname, 'fixtures');

export const SUPPORTED_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm'] as const;

export async function fixture(
/**
* Which file within in the fixture-set / scenario to read
*/
file: string,
options?: {
/**
* Which fixture set to use
*/
scenario?: string;
}
) {
let scenario = options?.scenario ?? 'default';
let fixtureFilePath = path.join(fixturesPath, scenario, file);
let contents = await fs.readFile(fixtureFilePath);

return contents.toString();
}

export async function createTmp() {
let prefix = 'v2-addon-blueprint--';
let prefixPath = path.join(os.tmpdir(), prefix);
Expand Down

0 comments on commit 6a3683e

Please sign in to comment.