This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: Project path compare * chore: Create @garment/utils package, add isSubPath win32 test * chore: Change path import
- Loading branch information
Aleksei Dmitriev
authored
Jan 6, 2021
1 parent
a16a853
commit a1a5c06
Showing
9 changed files
with
83 additions
and
7 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
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,10 @@ | ||
{ | ||
"name": "@garment/utils", | ||
"version": "0.0.14", | ||
"main": "lib/index.js", | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"files": [ | ||
"lib" | ||
] | ||
} |
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 @@ | ||
export * from './isSubPath'; |
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,50 @@ | ||
import { isSubPath } from '..'; | ||
import * as Path from 'path'; | ||
|
||
describe('utils | isSubPath', () => { | ||
it('should return true if path is a subpath', () => { | ||
expect(isSubPath('/root', '/root/child')).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if path is not a subpath', () => { | ||
expect(isSubPath('/root/foo', '/root/bar/baz')).toBeFalsy(); | ||
}); | ||
|
||
it('should return false if path has a similar substring', () => { | ||
expect(isSubPath('/root/foo', '/root/foo bar/baz')).toBeFalsy(); | ||
}); | ||
|
||
it("should return true if path has special path .. symbols and it's a subpath", () => { | ||
expect(isSubPath('/root/foo', '/root/../root/foo/bar')).toBeTruthy(); | ||
}); | ||
|
||
it("should return true if path has special path . symbol and it's a subpath", () => { | ||
expect(isSubPath('/root/foo', '/root/./foo/bar')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('utils | isSubPath [win32]', () => { | ||
let relativeSpy: jest.SpyInstance; | ||
let isAbsoluteSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
relativeSpy = jest | ||
.spyOn(Path, 'relative') | ||
.mockImplementationOnce(Path.win32.relative); | ||
isAbsoluteSpy = jest | ||
.spyOn(Path, 'isAbsolute') | ||
.mockImplementationOnce(Path.win32.isAbsolute); | ||
}); | ||
|
||
afterEach(() => { | ||
relativeSpy.mockRestore(); | ||
isAbsoluteSpy.mockRestore(); | ||
}); | ||
|
||
it('should return true if path is a subpath', () => { | ||
expect(isSubPath('C:\\Foo', 'C:\\Foo\\Bar')).toBeTruthy(); | ||
}); | ||
it('should return false if path is not a subpath', () => { | ||
expect(isSubPath('C:\\Foo\\Bar', 'D:\\Foo\\Bar')).toBeFalsy(); | ||
}); | ||
}); |
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,7 @@ | ||
import * as Path from 'path'; | ||
|
||
export const isSubPath = (parent: string, child: string) => { | ||
const relative = Path.relative(parent, child); | ||
|
||
return relative && !relative.startsWith('..') && !Path.isAbsolute(relative); | ||
}; |
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
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