Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests and refactored code (Part 1) #3

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _Codemod to convert Ember addons to v2 addon format_
- Switches Ember's "magic" import paths to relative paths
- Preserves your code whenever possible
- Supports [`ember-cli-typescript`](https://docs.ember-cli-typescript.com/) and [`glint`](https://typed-ember.gitbook.io/glint/)
- Focuses on maintainbility and extensibility
- Focuses on maintainability and extensibility


## Usage
Expand Down
10 changes: 8 additions & 2 deletions src/migration/ember-addon/steps/augment-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ function analyzePackageManager(options) {

function deriveAddonLocation(addonPackage) {
if (!addonPackage.name) {
throw new RangeError(
`ERROR: In package.json, the package name \`${addonPackage.name}\` is not valid.`
throw new SyntaxError(
'ERROR: In package.json, the package name is missing.'
);
}

if (!addonPackage.version) {
throw new SyntaxError(
'ERROR: In package.json, the package version is missing.'
Comment on lines +71 to +78
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to npm, we can expect that a valid package.json will always have the name and version fields.

https://docs.npmjs.com/creating-a-package-json-file#required-name-and-version-fields

);
}

Expand Down
28 changes: 13 additions & 15 deletions src/migration/ember-addon/steps/update-addon-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function updateDependencies(packageJson) {
'ember-cli-htmlbars',
];

const packagesToInstall = ['@embroider/addon-shim'];

packagesToDelete.forEach((packageName) => {
dependencies.delete(packageName);
});

const packagesToInstall = ['@embroider/addon-shim'];

packagesToInstall.forEach((packageName) => {
const version = decideVersion(packageName, dependencies);

Expand All @@ -37,12 +37,17 @@ function updateDevDependencies(packageJson, options) {

const devDependencies = convertToMap(packageJson['devDependencies']);

const packagesToDelete = [
'@embroider/macros',
'ember-auto-import',
'ember-cli-babel',
'ember-cli-htmlbars',
];
/*
For the time being, we'll take the approach of starting over and
adding back the development dependencies that are required. For
a more conservative approach, we could delete only the following:

- @embroider/macros
- ember-auto-import
- ember-cli-babel
- ember-cli-htmlbars
*/
devDependencies.clear();

const packagesToInstall = packages.addon.hasTypeScript
? [
Expand All @@ -65,13 +70,6 @@ function updateDevDependencies(packageJson, options) {
'rollup-plugin-copy',
];

// May be easier to start over and add missing dependencies
devDependencies.clear();

packagesToDelete.forEach((packageName) => {
devDependencies.delete(packageName);
});
Comment on lines -71 to -73
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused code. To be precise, a no-op because devDependencies becomes an empty map after line 69.


packagesToInstall.forEach((packageName) => {
const version = decideVersion(packageName, devDependencies);

Expand Down
11 changes: 7 additions & 4 deletions src/migration/ember-addon/steps/update-test-app-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function moveDependenciesToDevDependencies(packageJson, options) {
function updateDependencies(packageJson) {
const dependencies = convertToMap(packageJson['dependencies']);

// May be easier to start over and add missing dependencies
/*
For the time being, we'll take the approach of starting over and
adding back the dependencies that are required.
*/
dependencies.clear();

packageJson['dependencies'] = convertToObject(dependencies);
Expand All @@ -58,9 +61,9 @@ function updateOtherFields(packageJson, options) {

delete packageJson['ember-addon'];

packageJson['keywords'] = packageJson['keywords'].filter((keyword) => {
return keyword !== 'ember-addon';
});
packageJson['keywords'] = (packageJson['keywords'] ?? []).filter(
(keyword) => keyword !== 'ember-addon'
);
Comment on lines +64 to +66
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package.json in an Ember addon should have the keywords field, which is an array that includes 'ember-addon'. Just in case, I used the nullish coalescing operator to prevent a runtime error.


packageJson['name'] = packages.testApp.name;

Expand Down
79 changes: 79 additions & 0 deletions tests/migration/ember-addon/steps/analyze-addon/base-case.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { analyzeAddon } from '../../../../../src/migration/ember-addon/steps/analyze-addon.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | analyze-addon > base case', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/ember-container-query-typescript',
testAppLocation: undefined,
testAppName: undefined,
};

const inputProject = {
addon: {
components: {
'container-query.hbs': '',
'container-query.ts': '',
},
helpers: {
'aspect-ratio.ts': '',
'height.ts': '',
'width.ts': '',
},
modifiers: {
'container-query.ts': '',
},
styles: {
'container-query.css': '',
},
'.gitkeep': '',
'index.ts': '',
'template-registry.ts': '',
},
app: {
components: {
'container-query.js': '',
},
helpers: {
'aspect-ratio.js': '',
'height.js': '',
'width.js': '',
},
modifiers: {
'container-query.js': '',
},
'.gitkeep': '',
},
};

const expectedValue = {
appReexports: [
'components/container-query.js',
'helpers/aspect-ratio.js',
'helpers/height.js',
'helpers/width.js',
'modifiers/container-query.js',
],
publicEntrypoints: [
'components/container-query.js',
'helpers/aspect-ratio.js',
'helpers/height.js',
'helpers/width.js',
'index.js',
'modifiers/container-query.js',
'template-registry.js',
],
};

loadFixture(inputProject, options);

// Run the step
const augmentedOptions = {
projectRoot: 'tmp/ember-container-query-typescript',
};

assert.deepEqual(analyzeAddon(augmentedOptions), expectedValue);

// Check idempotence
assert.deepEqual(analyzeAddon(augmentedOptions), expectedValue);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { analyzeAddon } from '../../../../../src/migration/ember-addon/steps/analyze-addon.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | analyze-addon > edge case (folders are missing)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
testAppLocation: undefined,
testAppName: undefined,
};

const inputProject = {};

const expectedValue = {
appReexports: [],
publicEntrypoints: [],
};

loadFixture(inputProject, options);

// Run the step
const augmentedOptions = {
projectRoot: 'tmp/new-v1-addon-javascript',
};

assert.deepEqual(analyzeAddon(augmentedOptions), expectedValue);

// Check idempotence
assert.deepEqual(analyzeAddon(augmentedOptions), expectedValue);
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('migration | ember-addon | steps | augment-options > custom locations', fun
null,
2
),
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { augmentOptions } from '../../../../../src/migration/ember-addon/steps/augment-options.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | augment-options > error handling (empty file)', function () {
test('migration | ember-addon | steps | augment-options > error handling (package.json is an empty file)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
Expand All @@ -11,7 +11,7 @@ test('migration | ember-addon | steps | augment-options > error handling (empty

const inputProject = {
'package.json': '',
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { augmentOptions } from '../../../../../src/migration/ember-addon/steps/augment-options.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | augment-options > error handling (corrupt package.json)', function () {
test('migration | ember-addon | steps | augment-options > error handling (package.json is not a valid JSON)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
Expand All @@ -10,8 +10,8 @@ test('migration | ember-addon | steps | augment-options > error handling (corrup
};

const inputProject = {
'package.json': '{\n name:}',
'yarn.lock': 'some code for yarn.lock',
'package.json': '{\n "name": }',
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to test what would happen when the value is missing.

Instead, I had incorrectly tested the case of a JSON key having an incorrect format (because the double quotations are missing).

'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand All @@ -23,7 +23,7 @@ test('migration | ember-addon | steps | augment-options > error handling (corrup
(error) => {
assert.strictEqual(
error.message,
'ERROR: package.json is missing or is not a valid JSON. (Unexpected token n in JSON at position 4)\n'
'ERROR: package.json is missing or is not a valid JSON. (Unexpected token } in JSON at position 12)\n'
);

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { augmentOptions } from '../../../../../src/migration/ember-addon/steps/augment-options.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | augment-options > error handling (empty object)', function () {
test('migration | ember-addon | steps | augment-options > error handling (package name is missing)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
Expand All @@ -11,7 +11,7 @@ test('migration | ember-addon | steps | augment-options > error handling (empty

const inputProject = {
'package.json': '{}',
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand All @@ -23,7 +23,7 @@ test('migration | ember-addon | steps | augment-options > error handling (empty
(error) => {
assert.strictEqual(
error.message,
'ERROR: In package.json, the package name `undefined` is not valid.'
'ERROR: In package.json, the package name is missing.'
);

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { augmentOptions } from '../../../../../src/migration/ember-addon/steps/augment-options.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | augment-options > error handling (incorrect scoped package name)', function () {
test('migration | ember-addon | steps | augment-options > error handling (package name is not valid)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
Expand All @@ -14,19 +14,11 @@ test('migration | ember-addon | steps | augment-options > error handling (incorr
{
name: '@ijlee2/',
version: '0.0.0',
dependencies: {
'ember-cli-babel': '^7.26.11',
'ember-cli-htmlbars': '^6.1.1',
},
devDependencies: {},
'ember-addon': {
configPath: 'tests/dummy/config',
},
},
null,
2
),
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { augmentOptions } from '../../../../../src/migration/ember-addon/steps/augment-options.js';
import { assert, loadFixture, test } from '../../../../test-helpers.js';

test('migration | ember-addon | steps | augment-options > error handling (package version is missing)', function () {
const options = {
addonLocation: undefined,
projectRoot: 'tmp/new-v1-addon-javascript',
testAppLocation: undefined,
testAppName: undefined,
};

const inputProject = {
'package.json': JSON.stringify(
{
name: 'new-v1-addon',
},
null,
2
),
'yarn.lock': '',
};

loadFixture(inputProject, options);

assert.throws(
() => {
augmentOptions(options);
},
(error) => {
assert.strictEqual(
error.message,
'ERROR: In package.json, the package version is missing.'
);

return true;
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('migration | ember-addon | steps | augment-options > Glint', function () {
null,
2
),
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('migration | ember-addon | steps | augment-options > JavaScript', function
null,
2
),
'yarn.lock': 'some code for yarn.lock',
'yarn.lock': '',
};

loadFixture(inputProject, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('migration | ember-addon | steps | augment-options > npm', function () {
null,
2
),
'package-lock.json': 'some code for package-lock.json',
'package-lock.json': '',
};

loadFixture(inputProject, options);
Expand Down
Loading