From 6a3683ee45fec9fc6a0ed440fd8cbfde5907958d Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 14 Jul 2023 13:31:05 -0400 Subject: [PATCH] Add some fixture utils and assert that the .prettierrc.js is generated correctly for all tests, ensuring that the fixture utils work --- tests/.eslintignore | 1 + tests/.prettierignore | 1 + tests/assertions.ts | 44 ++++++++++++++++++++++++++- tests/fixtures/default/.prettierrc.js | 5 +++ tests/utils.ts | 20 ++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/.eslintignore create mode 100644 tests/.prettierignore create mode 100644 tests/fixtures/default/.prettierrc.js diff --git a/tests/.eslintignore b/tests/.eslintignore new file mode 100644 index 00000000..67fc140b --- /dev/null +++ b/tests/.eslintignore @@ -0,0 +1 @@ +fixtures/ diff --git a/tests/.prettierignore b/tests/.prettierignore new file mode 100644 index 00000000..67fc140b --- /dev/null +++ b/tests/.prettierignore @@ -0,0 +1 @@ +fixtures/ diff --git a/tests/assertions.ts b/tests/assertions.ts index 1cdbee83..f953355d 100644 --- a/tests/assertions.ts +++ b/tests/assertions.ts @@ -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; @@ -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}`); } diff --git a/tests/fixtures/default/.prettierrc.js b/tests/fixtures/default/.prettierrc.js new file mode 100644 index 00000000..534e6d35 --- /dev/null +++ b/tests/fixtures/default/.prettierrc.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + singleQuote: true, +}; diff --git a/tests/utils.ts b/tests/utils.ts index 488a76b0..a73e81fe 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -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);