ember-codemod-rename-test-modules 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Isaac J. Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ [![This project uses GitHub Actions for continuous integration.](https://github.com/ijlee2/ember-codemod-rename-test-modules/actions/workflows/ci.yml/badge.svg)](https://github.com/ijlee2/ember-codemod-rename-test-modules/actions/workflows/ci.yml)
2
+
3
+ # ember-codemod-rename-test-modules
4
+
5
+ _Codemod to rename test modules_
6
+
7
+
8
+ ## Usage
9
+
10
+ ### Arguments
11
+
12
+ <details>
13
+ <summary>Optional: Specify the project root</summary>
14
+
15
+ Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).
16
+
17
+ ```sh
18
+ npx ember-codemod-rename-test-modules --root <path/to/your/project>
19
+ ```
20
+
21
+ </details>
22
+
23
+
24
+ ### Limitations
25
+
26
+ The codemod is designed to cover typical cases. It is not designed to cover one-off cases.
27
+
28
+ To better meet your needs, consider cloning the repo and running the codemod locally.
29
+
30
+ ```sh
31
+ cd <path/to/cloned/repo>
32
+
33
+ # Compile TypeScript
34
+ pnpm build
35
+
36
+ # Run codemod
37
+ ./dist/bin/ember-codemod-rename-test-modules.js --root <path/to/your/project>
38
+ ```
39
+
40
+
41
+ ## Compatibility
42
+
43
+ - Node.js v16 or above
44
+
45
+
46
+ ## Contributing
47
+
48
+ See the [Contributing](CONTRIBUTING.md) guide for details.
49
+
50
+
51
+ ## License
52
+
53
+ This project is licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ // eslint-disable-next-line n/shebang
3
+ 'use strict';
4
+ import yargs from 'yargs';
5
+ import { hideBin } from 'yargs/helpers';
6
+ import { runCodemod } from '../src/index.js';
7
+ // Provide a title to the process in `ps`
8
+ process.title = 'ember-codemod-rename-test-modules';
9
+ // Set codemod options
10
+ const argv = yargs(hideBin(process.argv))
11
+ .option('root', {
12
+ describe: 'Where to run the codemod',
13
+ type: 'string',
14
+ })
15
+ .parseSync();
16
+ const codemodOptions = {
17
+ projectRoot: argv['root'] ?? process.cwd(),
18
+ };
19
+ runCodemod(codemodOptions);
@@ -0,0 +1,5 @@
1
+ import { createOptions, renameTests } from './steps/index.js';
2
+ export function runCodemod(codemodOptions) {
3
+ const options = createOptions(codemodOptions);
4
+ renameTests(options);
5
+ }
@@ -0,0 +1,6 @@
1
+ export function createOptions(codemodOptions) {
2
+ const { projectRoot } = codemodOptions;
3
+ return {
4
+ projectRoot,
5
+ };
6
+ }
@@ -0,0 +1,2 @@
1
+ export * from './create-options.js';
2
+ export * from './rename-tests.js';
@@ -0,0 +1,3 @@
1
+ export * from './rename-acceptance-tests.js';
2
+ export * from './rename-integration-tests.js';
3
+ export * from './rename-unit-tests.js';
@@ -0,0 +1,30 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { findFiles, parseFilePath } from '@codemod-utils/files';
4
+ import { parseEntity, renameModule } from '../../utils/rename-tests/index.js';
5
+ const folderToEntityType = new Map();
6
+ function getModuleName(filePath) {
7
+ let { dir, name } = parseFilePath(filePath);
8
+ dir = dir.replace(/^tests\/acceptance(\/)?/, '');
9
+ name = name.replace(/-test$/, '');
10
+ const { entityType, remainingPath } = parseEntity(dir, folderToEntityType);
11
+ const entityName = join(remainingPath, name);
12
+ // a.k.a. friendlyTestDescription
13
+ return ['Acceptance', entityType, entityName].filter(Boolean).join(' | ');
14
+ }
15
+ export function renameAcceptanceTests(options) {
16
+ const { projectRoot } = options;
17
+ const filePaths = findFiles('tests/acceptance/**/*-test.{js,ts}', {
18
+ projectRoot,
19
+ });
20
+ filePaths.forEach((filePath) => {
21
+ const oldPath = join(projectRoot, filePath);
22
+ const oldFile = readFileSync(oldPath, 'utf8');
23
+ const data = {
24
+ isTypeScript: filePath.endsWith('.ts'),
25
+ moduleName: getModuleName(filePath),
26
+ };
27
+ const newFile = renameModule(oldFile, data);
28
+ writeFileSync(oldPath, newFile, 'utf8');
29
+ });
30
+ }
@@ -0,0 +1,34 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { findFiles, parseFilePath } from '@codemod-utils/files';
4
+ import { parseEntity, renameModule } from '../../utils/rename-tests/index.js';
5
+ const folderToEntityType = new Map([
6
+ ['components', 'Component'],
7
+ ['helpers', 'Helper'],
8
+ ['modifiers', 'Modifier'],
9
+ ]);
10
+ function getModuleName(filePath) {
11
+ let { dir, name } = parseFilePath(filePath);
12
+ dir = dir.replace(/^tests\/integration(\/)?/, '');
13
+ name = name.replace(/-test$/, '');
14
+ const { entityType, remainingPath } = parseEntity(dir, folderToEntityType);
15
+ const entityName = join(remainingPath, name);
16
+ // a.k.a. friendlyTestDescription
17
+ return ['Integration', entityType, entityName].filter(Boolean).join(' | ');
18
+ }
19
+ export function renameIntegrationTests(options) {
20
+ const { projectRoot } = options;
21
+ const filePaths = findFiles('tests/integration/**/*-test.{js,ts}', {
22
+ projectRoot,
23
+ });
24
+ filePaths.forEach((filePath) => {
25
+ const oldPath = join(projectRoot, filePath);
26
+ const oldFile = readFileSync(oldPath, 'utf8');
27
+ const data = {
28
+ isTypeScript: filePath.endsWith('.ts'),
29
+ moduleName: getModuleName(filePath),
30
+ };
31
+ const newFile = renameModule(oldFile, data);
32
+ writeFileSync(oldPath, newFile, 'utf8');
33
+ });
34
+ }
@@ -0,0 +1,41 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { findFiles, parseFilePath } from '@codemod-utils/files';
4
+ import { parseEntity, renameModule } from '../../utils/rename-tests/index.js';
5
+ const folderToEntityType = new Map([
6
+ ['adapters', 'Adapter'],
7
+ ['controllers', 'Controller'],
8
+ ['initializers', 'Initializer'],
9
+ ['instance-initializers', 'Instance Initializer'],
10
+ ['mixins', 'Mixin'],
11
+ ['models', 'Model'],
12
+ ['routes', 'Route'],
13
+ ['serializers', 'Serializer'],
14
+ ['services', 'Service'],
15
+ ['utils', 'Utility'],
16
+ ]);
17
+ function getModuleName(filePath) {
18
+ let { dir, name } = parseFilePath(filePath);
19
+ dir = dir.replace(/^tests\/unit(\/)?/, '');
20
+ name = name.replace(/-test$/, '');
21
+ const { entityType, remainingPath } = parseEntity(dir, folderToEntityType);
22
+ const entityName = join(remainingPath, name);
23
+ // a.k.a. friendlyTestDescription
24
+ return ['Unit', entityType, entityName].filter(Boolean).join(' | ');
25
+ }
26
+ export function renameUnitTests(options) {
27
+ const { projectRoot } = options;
28
+ const filePaths = findFiles('tests/unit/**/*-test.{js,ts}', {
29
+ projectRoot,
30
+ });
31
+ filePaths.forEach((filePath) => {
32
+ const oldPath = join(projectRoot, filePath);
33
+ const oldFile = readFileSync(oldPath, 'utf8');
34
+ const data = {
35
+ isTypeScript: filePath.endsWith('.ts'),
36
+ moduleName: getModuleName(filePath),
37
+ };
38
+ const newFile = renameModule(oldFile, data);
39
+ writeFileSync(oldPath, newFile, 'utf8');
40
+ });
41
+ }
@@ -0,0 +1,6 @@
1
+ import { renameAcceptanceTests, renameIntegrationTests, renameUnitTests, } from './rename-tests/index.js';
2
+ export function renameTests(options) {
3
+ renameAcceptanceTests(options);
4
+ renameIntegrationTests(options);
5
+ renameUnitTests(options);
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from './parse-entity.js';
2
+ export * from './rename-module.js';
@@ -0,0 +1,14 @@
1
+ export function parseEntity(dir, folderToEntityType) {
2
+ const [folder, ...remainingPaths] = dir.split('/');
3
+ const entityType = folderToEntityType.get(folder);
4
+ if (entityType === undefined) {
5
+ return {
6
+ entityType,
7
+ remainingPath: dir,
8
+ };
9
+ }
10
+ return {
11
+ entityType,
12
+ remainingPath: remainingPaths.join('/'),
13
+ };
14
+ }
@@ -0,0 +1,28 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
+ import { AST } from '@codemod-utils/ast-javascript';
3
+ export function renameModule(file, data) {
4
+ const traverse = AST.traverse(data.isTypeScript);
5
+ const ast = traverse(file, {
6
+ visitCallExpression(node) {
7
+ if (node.value.callee.type !== 'Identifier' ||
8
+ node.value.callee.name !== 'module') {
9
+ return false;
10
+ }
11
+ if (node.value.arguments.length !== 2) {
12
+ return false;
13
+ }
14
+ switch (node.value.arguments[0].type) {
15
+ case 'Literal': {
16
+ node.value.arguments[0] = AST.builders.literal(data.moduleName);
17
+ break;
18
+ }
19
+ case 'StringLiteral': {
20
+ node.value.arguments[0] = AST.builders.stringLiteral(data.moduleName);
21
+ break;
22
+ }
23
+ }
24
+ return false;
25
+ },
26
+ });
27
+ return AST.print(ast);
28
+ }
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "ember-codemod-rename-test-modules",
3
+ "version": "0.1.0",
4
+ "description": "Codemod to rename test modules",
5
+ "keywords": [
6
+ "codemod",
7
+ "ember-codemod",
8
+ "emberjs"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/ijlee2/ember-codemod-rename-test-modules.git"
13
+ },
14
+ "license": "MIT",
15
+ "author": "Isaac J. Lee",
16
+ "type": "module",
17
+ "main": "dist/src/index.js",
18
+ "bin": "dist/bin/ember-codemod-rename-test-modules.js",
19
+ "directories": {
20
+ "test": "tests"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@codemod-utils/ast-javascript": "^0.3.2",
27
+ "@codemod-utils/files": "^0.5.2",
28
+ "yargs": "^17.7.2"
29
+ },
30
+ "devDependencies": {
31
+ "@babel/core": "^7.22.9",
32
+ "@codemod-utils/tests": "^0.2.5",
33
+ "@sondr3/minitest": "0.1.1",
34
+ "@tsconfig/esm": "^1.0.4",
35
+ "@tsconfig/node16": "^16.1.0",
36
+ "@tsconfig/strictest": "^2.0.1",
37
+ "@types/node": "^16.11.7",
38
+ "@types/yargs": "^17.0.24",
39
+ "@typescript-eslint/eslint-plugin": "^6.2.0",
40
+ "@typescript-eslint/parser": "^6.2.0",
41
+ "concurrently": "^8.2.0",
42
+ "eslint": "^8.45.0",
43
+ "eslint-config-prettier": "^8.9.0",
44
+ "eslint-import-resolver-typescript": "^3.5.5",
45
+ "eslint-plugin-import": "^2.28.0",
46
+ "eslint-plugin-n": "^16.0.1",
47
+ "eslint-plugin-prettier": "^5.0.0",
48
+ "eslint-plugin-simple-import-sort": "^10.0.0",
49
+ "eslint-plugin-typescript-sort-keys": "^2.3.0",
50
+ "lerna-changelog": "^2.2.0",
51
+ "prettier": "^3.0.0",
52
+ "typescript": "^5.1.6"
53
+ },
54
+ "engines": {
55
+ "node": "16.* || >= 18"
56
+ },
57
+ "changelog": {
58
+ "labels": {
59
+ "breaking": "Breaking Change",
60
+ "bug": "Bug Fix",
61
+ "enhance: code": "Enhancement",
62
+ "enhance: dependency": "Internal",
63
+ "enhance: documentation": "Documentation"
64
+ }
65
+ },
66
+ "scripts": {
67
+ "build": "./build.sh --production",
68
+ "changelog": "lerna-changelog",
69
+ "lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
70
+ "lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
71
+ "lint:js": "eslint . --cache",
72
+ "lint:js:fix": "eslint . --fix",
73
+ "lint:types": "tsc --noEmit",
74
+ "test": "./build.sh --test && mt dist-for-testing --quiet"
75
+ }
76
+ }