Skip to content

Commit

Permalink
Add projectName to ember-cli-config.json
Browse files Browse the repository at this point in the history
Updates our blueprint for ember-cli/ember-cli-update#1249. Also adds additional test coverage for the generated config file.
  • Loading branch information
simonihmig committed Sep 29, 2023
1 parent 572d094 commit e2a28c3
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 26 deletions.
1 change: 1 addition & 0 deletions files/config/ember-cli-update.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"schemaVersion": "1.0.0",
"projectName": "<%= addonName %>",
"packages": [
{
"name": "@embroider/addon-blueprint",
Expand Down
107 changes: 97 additions & 10 deletions tests/helpers/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface AssertGeneratedOptions {
testAppLocation?: string;
testAppName?: string;
expectedStaticFiles?: string[];
packageManager?: 'npm' | 'yarn' | 'pnpm';
typeScript?: boolean;
existingMonorepo?: boolean;
}

/**
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -99,6 +186,6 @@ export async function matchesFixture(
*/
expect(sourceContents.trim()).to.equal(
fixtureContents.trim(),
`${testFilePath} matches ${fixtureFile}`
`${testFilePath} matches ${fixtureFile}`,
);
}
2 changes: 1 addition & 1 deletion tests/smoke-tests/--addon-location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke-tests/--test-app-location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
22 changes: 12 additions & 10 deletions tests/smoke-tests/--typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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',
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke-tests/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -75,6 +75,8 @@ describe('custom locations', () => {
addonLocation,
testAppLocation,
expectedStaticFiles: ['README.md', 'CONTRIBUTING.md'],
packageManager: 'pnpm',
existingMonorepo: true,
});
});

Expand Down
4 changes: 3 additions & 1 deletion tests/smoke-tests/within-existing-monorepo/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ 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);
});

it('was generated correctly', async () => {
await assertGeneratedCorrectly({
projectRoot: cwd,
expectedStaticFiles: ['README.md', 'CONTRIBUTING.md'],
packageManager,
existingMonorepo: true,
});
});

Expand Down

0 comments on commit e2a28c3

Please sign in to comment.