Skip to content

Commit

Permalink
test: add test cases for isSubDirOrEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Nov 7, 2023
1 parent 95a4494 commit 9507efe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
11 changes: 1 addition & 10 deletions packages/solutions/app-tools/src/analyze/getBundleEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Entrypoint } from '@modern-js/types';
import type { AppNormalizedConfig, IAppContext } from '../types';
import { getFileSystemEntry } from './getFileSystemEntry';
import { JS_EXTENSIONS } from './constants';
import { isSubDirOrEqual } from './utils';

const ensureExtensions = (file: string) => {
if (!path.extname(file)) {
Expand All @@ -24,16 +25,6 @@ const ensureExtensions = (file: string) => {
*/
const isDirectory = (file: string) => !path.extname(file);

const isSubDirOrEqual = (parent: string, child: string): boolean => {
if (parent === child) {
return true;
}
const relative = path.relative(parent, child);
const isSubdir =
relative && !relative.startsWith('..') && !path.isAbsolute(relative);
return Boolean(isSubdir);
};

const ifAlreadyExists = (
entrypoints: Entrypoint[],
checked: Entrypoint,
Expand Down
10 changes: 10 additions & 0 deletions packages/solutions/app-tools/src/analyze/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ export const checkIsBuildCommands = () => {

return buildCommands.includes(command);
};

export const isSubDirOrEqual = (parent: string, child: string): boolean => {
if (parent === child) {
return true;
}
const relative = path.relative(parent, child);
const isSubdir =
relative && !relative.startsWith('..') && !path.isAbsolute(relative);
return Boolean(isSubdir);
};
28 changes: 28 additions & 0 deletions packages/solutions/app-tools/tests/analyze/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path';
import { isSubDirOrEqual } from '../../src/analyze/utils';

describe('isSubDirOrEqual', () => {
it('should return true for the same directories', () => {
const parent = path.resolve('/Users/test');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(true);
});

it('should return true for a child directory', () => {
const parent = path.resolve('/Users');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(true);
});

it('should return false for a non-child directory', () => {
const parent = path.resolve('/Users/test');
const child = path.resolve('/Users/anotherTest');
expect(isSubDirOrEqual(parent, child)).toBe(false);
});

it('should return false for a directory at a higher level', () => {
const parent = path.resolve('/Users/test/deeper');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(false);
});
});

0 comments on commit 9507efe

Please sign in to comment.