From e2a28c301c529804043d663499f10f5819923496 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Fri, 29 Sep 2023 18:26:08 +0200 Subject: [PATCH] Add projectName to ember-cli-config.json Updates our blueprint for https://github.com/ember-cli/ember-cli-update/pull/1249. Also adds additional test coverage for the generated config file. --- files/config/ember-cli-update.json | 1 + tests/helpers/assertions.ts | 107 ++++++++++++++++-- tests/smoke-tests/--addon-location.test.ts | 2 +- tests/smoke-tests/--test-app-location.test.ts | 2 +- tests/smoke-tests/--typescript.test.ts | 22 ++-- tests/smoke-tests/defaults.test.ts | 4 +- .../custom-locations.test.ts | 4 +- .../within-existing-monorepo/defaults.test.ts | 4 +- 8 files changed, 120 insertions(+), 26 deletions(-) diff --git a/files/config/ember-cli-update.json b/files/config/ember-cli-update.json index 90dc9d63..e5f81dfe 100644 --- a/files/config/ember-cli-update.json +++ b/files/config/ember-cli-update.json @@ -1,5 +1,6 @@ { "schemaVersion": "1.0.0", + "projectName": "<%= addonName %>", "packages": [ { "name": "@embroider/addon-blueprint", diff --git a/tests/helpers/assertions.ts b/tests/helpers/assertions.ts index 39d25ddd..05e64d1e 100644 --- a/tests/helpers/assertions.ts +++ b/tests/helpers/assertions.ts @@ -14,6 +14,9 @@ interface AssertGeneratedOptions { testAppLocation?: string; testAppName?: string; expectedStaticFiles?: string[]; + packageManager?: 'npm' | 'yarn' | 'pnpm'; + typeScript?: boolean; + existingMonorepo?: boolean; } /** @@ -22,14 +25,17 @@ interface AssertGeneratedOptions { */ export async function assertGeneratedCorrectly({ projectRoot, - addonLocation = 'my-addon', + addonLocation, addonName = 'my-addon', - testAppLocation = 'test-app', - testAppName = 'test-app', + testAppLocation, + testAppName, expectedStaticFiles = ['README.md', 'LICENSE.md'], + packageManager = 'npm', + typeScript = false, + existingMonorepo = false, }: AssertGeneratedOptions) { - let addonPath = path.join(projectRoot, addonLocation); - let testAppPath = path.join(projectRoot, testAppLocation); + let addonPath = path.join(projectRoot, addonLocation ?? 'my-addon'); + let testAppPath = path.join(projectRoot, testAppLocation ?? 'test-app'); expect(await fse.pathExists(addonPath), `${addonPath} exists`).toBe(true); expect(await fse.pathExists(testAppPath), `${testAppPath} exists`).toBe(true); @@ -38,14 +44,16 @@ export async function assertGeneratedCorrectly({ let testAppPackageJson = await packageJsonAt(testAppPath); expect(addonPackageJson.name, `addon has correct name: ${addonName}`).toEqual(addonName); - expect(testAppPackageJson.name, `testApp has correct name: ${testAppName}`).toEqual(testAppName); + expect(testAppPackageJson.name, `testApp has correct name: ${testAppName ?? 'test-app'}`).toEqual( + testAppName ?? 'test-app', + ); expect( [ ...Object.keys(testAppPackageJson.dependencies || {}), ...Object.keys(testAppPackageJson.devDependencies || {}), ], - `The test app has a (dev)dependency on the addon` + `The test app has a (dev)dependency on the addon`, ).to.include(addonName); for (let expectedFile of expectedStaticFiles) { @@ -54,7 +62,86 @@ export async function assertGeneratedCorrectly({ expect(await fse.pathExists(pathToFile), `${pathToFile} exists`).toBe(true); } - await matchesFixture('.prettierrc.cjs', { cwd: projectRoot }); + if (!existingMonorepo) { + await matchesFixture('.prettierrc.cjs', { cwd: projectRoot }); + + await assertCorrectECUJson({ + projectRoot, + addonName, + addonLocation, + testAppLocation, + testAppName, + packageManager, + typeScript, + }); + } +} + +interface AssertECUOptions { + projectRoot: string; + addonName: string; + addonLocation?: string; + testAppLocation?: string; + testAppName?: string; + packageManager: 'npm' | 'yarn' | 'pnpm'; + typeScript: boolean; +} + +export async function assertCorrectECUJson({ + projectRoot, + addonName, + addonLocation, + testAppLocation, + testAppName, + packageManager, + typeScript, +}: AssertECUOptions) { + let configPath = path.join(projectRoot, 'config/ember-cli-update.json'); + + let json = await fse.readJSON(configPath); + + expect(json.projectName).toEqual(addonName); + expect(json.packages).toHaveLength(1); + + expect(json.packages[0].name).toEqual('@embroider/addon-blueprint'); + expect(json.packages[0].version).toEqual('2.6.1'); // @todo + + expect(json.packages[0].blueprints).toHaveLength(1); + expect(json.packages[0].blueprints[0].name).toEqual('@embroider/addon-blueprint'); + + if (packageManager !== 'npm') { + expect(json.packages[0].blueprints[0].options).toContain(`--${packageManager}`); + } + + if (typeScript) { + expect(json.packages[0].blueprints[0].options).toContain(`--typescript`); + } else { + expect(json.packages[0].blueprints[0].options).not.toContain(`--typescript`); + } + + if (addonLocation) { + expect(json.packages[0].blueprints[0].options).toContain(`--addon-location=${addonLocation}`); + } else { + expect(json.packages[0].blueprints[0].options).not.toContain( + `--addon-location=${addonLocation}`, + ); + } + + if (testAppLocation) { + expect(json.packages[0].blueprints[0].options).toContain( + `--test-app-location=${testAppLocation}`, + ); + } else { + expect(json.packages[0].blueprints[0].options).not.toContain( + `--test-app-location=${testAppLocation}`, + ); + } + + if (testAppName) { + expect(json.packages[0].blueprints[0].options).toContain(`--test-app-name=${testAppName}`); + } else { + expect(json.packages[0].blueprints[0].options).not.toContain(`--test-app-name=${testAppName}`); + } } export async function matchesFixture( @@ -80,7 +167,7 @@ export async function matchesFixture( * 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; @@ -99,6 +186,6 @@ export async function matchesFixture( */ expect(sourceContents.trim()).to.equal( fixtureContents.trim(), - `${testFilePath} matches ${fixtureFile}` + `${testFilePath} matches ${fixtureFile}`, ); } diff --git a/tests/smoke-tests/--addon-location.test.ts b/tests/smoke-tests/--addon-location.test.ts index f8fa7633..149a43fe 100644 --- a/tests/smoke-tests/--addon-location.test.ts +++ b/tests/smoke-tests/--addon-location.test.ts @@ -34,7 +34,7 @@ describe('--addon-location', () => { }); it('was generated correctly', async () => { - assertGeneratedCorrectly({ projectRoot: cwd, addonLocation }); + assertGeneratedCorrectly({ projectRoot: cwd, addonLocation, packageManager: 'pnpm' }); }); it('runs tests', async () => { diff --git a/tests/smoke-tests/--test-app-location.test.ts b/tests/smoke-tests/--test-app-location.test.ts index 05f4a83d..6551a3c0 100644 --- a/tests/smoke-tests/--test-app-location.test.ts +++ b/tests/smoke-tests/--test-app-location.test.ts @@ -34,7 +34,7 @@ describe('--test-app-location', () => { }); it('was generated correctly', async () => { - assertGeneratedCorrectly({ projectRoot: cwd, testAppLocation }); + assertGeneratedCorrectly({ projectRoot: cwd, testAppLocation, packageManager: 'pnpm' }); }); it('runs tests', async () => { diff --git a/tests/smoke-tests/--typescript.test.ts b/tests/smoke-tests/--typescript.test.ts index 1909eb42..0c280d19 100644 --- a/tests/smoke-tests/--typescript.test.ts +++ b/tests/smoke-tests/--typescript.test.ts @@ -36,7 +36,11 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { it('was generated correctly', async () => { await helper.build(); - assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + assertGeneratedCorrectly({ + projectRoot: helper.projectRoot, + packageManager, + typeScript: true, + }); }); // Tests are additive, so when running them in order, we want to check linting @@ -55,7 +59,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { // (controlled by ember-cli) // // Ensure that we have no lint errors. - // It's important to keep this along with the tests, + // It's important to keep this along with the tests, // so that we can have confidence that the lints aren't destructively changing // the files in a way that would break consumers await helper.run('lint:fix'); @@ -75,8 +79,6 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { let distContents = splitHashedFiles(await dirContents(distDir)); let declarationsContents = await dirContents(declarationsDir); - - expect(distContents.unhashed).to.deep.equal([ '_app_', 'components', @@ -89,14 +91,14 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { expect(distContents.hashed.length).toBe(4); expect( distContents.hashed - .filter(file => file.includes('_rollup')) - .map(file => file.split('.js')[1]) - ).to.deep.equal(["", '.map'], 'the rollup helpers are emitted with a source map'); + .filter((file) => file.includes('_rollup')) + .map((file) => file.split('.js')[1]), + ).to.deep.equal(['', '.map'], 'the rollup helpers are emitted with a source map'); expect( distContents.hashed - .filter(file => file.includes('template-only')) - .map(file => file.split('.js')[1]) - ).to.deep.equal(["", '.map'], 'the template-only component is emitted with a source map'); + .filter((file) => file.includes('template-only')) + .map((file) => file.split('.js')[1]), + ).to.deep.equal(['', '.map'], 'the template-only component is emitted with a source map'); expect(declarationsContents).to.deep.equal([ 'components', diff --git a/tests/smoke-tests/defaults.test.ts b/tests/smoke-tests/defaults.test.ts index 4f19c33d..9c14eab3 100644 --- a/tests/smoke-tests/defaults.test.ts +++ b/tests/smoke-tests/defaults.test.ts @@ -33,7 +33,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { let pnpm = path.join(helper.projectRoot, 'pnpm-lock.yaml'); let testManifest = await fse.readJson( - path.join(helper.projectRoot, 'test-app', 'package.json') + path.join(helper.projectRoot, 'test-app', 'package.json'), ); switch (packageManager) { @@ -105,7 +105,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { }); it('was generated correctly', async () => { - await assertGeneratedCorrectly({ projectRoot: helper.projectRoot }); + await assertGeneratedCorrectly({ projectRoot: helper.projectRoot, packageManager }); }); // Tests are additive, so when running them in order, we want to check linting diff --git a/tests/smoke-tests/within-existing-monorepo/custom-locations.test.ts b/tests/smoke-tests/within-existing-monorepo/custom-locations.test.ts index f3dace48..9fd51052 100644 --- a/tests/smoke-tests/within-existing-monorepo/custom-locations.test.ts +++ b/tests/smoke-tests/within-existing-monorepo/custom-locations.test.ts @@ -65,7 +65,7 @@ describe('custom locations', () => { fixturify.readSync(cwd, { ignore: ['addons', 'tests', 'node_modules', 'pnpm-lock.yaml', '.npmrc'], }), - 'root files have not been touched' + 'root files have not been touched', ).toEqual(rootFiles); }); @@ -75,6 +75,8 @@ describe('custom locations', () => { addonLocation, testAppLocation, expectedStaticFiles: ['README.md', 'CONTRIBUTING.md'], + packageManager: 'pnpm', + existingMonorepo: true, }); }); diff --git a/tests/smoke-tests/within-existing-monorepo/defaults.test.ts b/tests/smoke-tests/within-existing-monorepo/defaults.test.ts index 415a2c95..85e447bc 100644 --- a/tests/smoke-tests/within-existing-monorepo/defaults.test.ts +++ b/tests/smoke-tests/within-existing-monorepo/defaults.test.ts @@ -91,7 +91,7 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { fixturify.readSync(cwd, { ignore: ['my-addon', 'test-app', 'node_modules', lockFile], }), - 'root files have not been touched' + 'root files have not been touched', ).toEqual(rootFiles); }); @@ -99,6 +99,8 @@ for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { await assertGeneratedCorrectly({ projectRoot: cwd, expectedStaticFiles: ['README.md', 'CONTRIBUTING.md'], + packageManager, + existingMonorepo: true, }); });