-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { relative, isAbsolute } from 'path'; | ||
|
||
function isInsideAnotherPath(parent: string, directory: string): boolean { | ||
const relativePart = relative(parent, directory); | ||
// Tested folder is above parent. | ||
if (relativePart.startsWith('..')) { | ||
return false; | ||
} | ||
// Tested folder is the same as parent. | ||
if (relativePart.length === 0) { | ||
return false; | ||
} | ||
// Tested directory has nothing in common with parent. | ||
if (isAbsolute(relativePart)) { | ||
return false; | ||
} | ||
// Last option, must be subfolder. | ||
return true; | ||
} | ||
|
||
export { isInsideAnotherPath }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path'; | ||
|
||
jest.mock('path', () => jest.requireActual('path').posix); | ||
|
||
const unixTests: [string, string, boolean][] = [ | ||
// Identical | ||
['/foo', '/foo', false], | ||
// Nothing in common | ||
['/foo', '/bar', false], | ||
// subfolder | ||
['/foo', '/foo/bar', true], | ||
// parallel | ||
['/foo', '/foo/../bar', false], | ||
// relative subfolder | ||
['/foo', '/foo/./bar', true], | ||
]; | ||
|
||
describe('Properly detects ignored sub-folders on Unix', () => { | ||
it('should work on Unix', () => { | ||
unixTests.forEach(([parent, testedPath, expectedResult]) => { | ||
const result = isInsideAnotherPath(parent, testedPath); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
test/unit/utils/path/is-inside-another-path-windows.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path'; | ||
|
||
jest.mock('path', () => jest.requireActual('path').win32); | ||
|
||
const windowsTests: [string, string, boolean][] = [ | ||
// subfolder | ||
['C:\\Foo', 'C:\\Foo\\Bar', true], | ||
// Nothing in common | ||
['C:\\Foo', 'C:\\Bar', false], | ||
// Wrong drive. | ||
['C:\\Foo', 'D:\\Foo\\Bar', false], | ||
]; | ||
|
||
describe('Properly detects ignored sub-folders on Windows', () => { | ||
it('should work on Windows', () => { | ||
windowsTests.forEach(([parent, testedPath, expectedResult]) => { | ||
const result = isInsideAnotherPath(parent, testedPath); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |