Skip to content

Commit

Permalink
refactor: Standardized function (getModuleName)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijlee2 committed Jul 28, 2023
1 parent e7082c7 commit 482468c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/steps/rename-tests/rename-acceptance-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { join } from 'node:path';
import { findFiles, parseFilePath } from '@codemod-utils/files';

import type { Options } from '../../types/index.js';
import { renameModule } from '../../utils/rename-tests/index.js';
import { parseEntity, renameModule } from '../../utils/rename-tests/index.js';

const folderToEntityType = new Map<string, string>();

function getModuleName(filePath: string): string {
let { dir, name } = parseFilePath(filePath);

dir = dir.replace(/^tests\/acceptance(\/)?/, '');
name = name.replace(/-test$/, '');

const entityName = join(dir, name);
const { entityType, remainingPath } = parseEntity(dir, folderToEntityType);
const entityName = join(remainingPath, name);

// a.k.a. friendlyTestDescription
return ['Acceptance', entityName].join(' | ');
return ['Acceptance', entityType, entityName].filter(Boolean).join(' | ');
}

export function renameAcceptanceTests(options: Options): void {
Expand Down
2 changes: 1 addition & 1 deletion src/steps/rename-tests/rename-integration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getModuleName(filePath: string): string {
const entityName = join(remainingPath, name);

// a.k.a. friendlyTestDescription
return ['Integration', entityType, entityName].join(' | ');
return ['Integration', entityType, entityName].filter(Boolean).join(' | ');
}

export function renameIntegrationTests(options: Options): void {
Expand Down
2 changes: 1 addition & 1 deletion src/steps/rename-tests/rename-unit-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getModuleName(filePath: string): string {
const entityName = join(remainingPath, name);

// a.k.a. friendlyTestDescription
return ['Unit', entityType, entityName].join(' | ');
return ['Unit', entityType, entityName].filter(Boolean).join(' | ');
}

export function renameUnitTests(options: Options): void {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/rename-tests/parse-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export function parseEntity(
const [folder, ...remainingPaths] = dir.split('/');
const entityType = folderToEntityType.get(folder!);

if (entityType === undefined) {
return {
entityType,
remainingPath: dir,
};
}

return {
entityType,
remainingPath: remainingPaths.join('/'),
Expand Down

0 comments on commit 482468c

Please sign in to comment.