-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix detecting WC in Windows (#548)
* fix: fix detecting WC in Windows Co-authored-by: Adrià Blancafort <[email protected]> * fix: fix isInPathList detection with different separators * chore: update GH Action yml --------- Co-authored-by: Adrià Blancafort <[email protected]>
- Loading branch information
1 parent
d3bb505
commit 3f036e1
Showing
8 changed files
with
185 additions
and
28 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
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,97 @@ | ||
import { describe, expect, it, beforeEach, afterEach, spyOn } from 'bun:test'; | ||
import fs from 'node:fs'; | ||
import isInPathList from '.'; | ||
|
||
const listPath = 'foo.txt'; | ||
let mockExistsSync: ReturnType<typeof spyOn>; | ||
let mockReadFileSync: ReturnType<typeof spyOn>; | ||
|
||
describe('utils / isInPathList', () => { | ||
beforeEach(() => { | ||
mockExistsSync = spyOn(fs, 'existsSync').mockReturnValue(true); | ||
}); | ||
|
||
afterEach(() => { | ||
mockExistsSync.mockRestore(); | ||
}); | ||
|
||
it('should return false when the list is empty', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue(''); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/foo' }, | ||
} as any); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true when the path is in the list', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/foo' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false when the path is not in the list', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/baz' }, | ||
} as any); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true when the path is in the list with different separators', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( | ||
'\\foo\n\\bar', | ||
); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/foo' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return true when the path is in the list with multi different separators', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( | ||
'\\foo\\bar\n\\baz', | ||
); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/foo/bar' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return true when the path is in the list with multi duplicated different separators', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( | ||
'\\\\foo\\\\bar\n\\\\baz', | ||
); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/foo/bar' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return true when the path is in the list with different separators in other way', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue('/foo\n/bar'); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '\\foo' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return true when the path is in the list with different separators in other way', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue( | ||
'/foo/bar\n/bar', | ||
); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '\\foo\\bar' }, | ||
} as any); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false when the list is empty', async () => { | ||
mockReadFileSync = spyOn(fs, 'readFileSync').mockReturnValue(''); | ||
const result = await isInPathList(listPath, { | ||
route: { filePath: '/baz' }, | ||
} as any); | ||
expect(result).toBeFalse(); | ||
}); | ||
}); |
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,29 @@ | ||
import fs from 'node:fs'; | ||
import { getConstants } from '@/constants'; | ||
import type { RequestContext } from '@/types'; | ||
|
||
const SEPS_REGEX = /(\\|\/)+/g; | ||
const TEMP_SEP = '|'; | ||
|
||
export default async function isInPathList( | ||
pathname: string, | ||
request: RequestContext, | ||
) { | ||
const { BUILD_DIR } = getConstants(); | ||
const listText = fs.existsSync(pathname) | ||
? fs.readFileSync(pathname, 'utf-8') | ||
: ''; | ||
|
||
if (!listText) return false; | ||
|
||
const route = (request.route?.filePath ?? '') | ||
.replace(BUILD_DIR, '') | ||
.replace(SEPS_REGEX, TEMP_SEP); | ||
const list = listText.split('\n'); | ||
|
||
for (let i = 0; i < list.length; i += 1) { | ||
if (list[i].replace(SEPS_REGEX, TEMP_SEP) === route) return true; | ||
} | ||
|
||
return false; | ||
} |
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