Skip to content

Commit

Permalink
Parse SRC file list case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed May 6, 2024
1 parent 5afb0d7 commit afd580e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
51 changes: 26 additions & 25 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import os from 'os'

let fsExistsSyncMock: jest.SpiedFunction<typeof fs.existsSync>
let fsReadFileSyncMock: jest.SpiedFunction<typeof fs.readFileSync>
let fsRealpathSyncNativeMock: jest.SpiedFunction<typeof fs.realpathSync.native>
let ioMkdirPMock: jest.SpiedFunction<typeof io.mkdirP>
let ioRmRFMock: jest.SpiedFunction<typeof io.rmRF>
let tcDownloadToolMock: jest.SpiedFunction<typeof tc.downloadTool>
Expand All @@ -29,6 +30,7 @@ describe('Parser', () => {
beforeEach(() => {
fsExistsSyncMock = jest.spyOn(fs, 'existsSync')
fsReadFileSyncMock = jest.spyOn(fs, 'readFileSync')
fsRealpathSyncNativeMock = jest.spyOn(fs.realpathSync, 'native')
})

describe('constructor', () => {
Expand Down Expand Up @@ -136,12 +138,16 @@ describe('Parser', () => {
} as unknown as Parser

fsExistsSyncMock.mockReturnValue(false).mockReturnValueOnce(true)
fsRealpathSyncNativeMock.mockImplementation(() => {
throw new Error('ENOENT')
})
fsReadFileSyncMock.mockReturnValue('').mockReturnValueOnce('test.d\n')

const result = await Parser.from(patchName, basePath, workingDir)
expect(result).toHaveLength(1)
expect(result[0]).toMatchObject(oneParser)
expect(fsExistsSyncMock).toHaveBeenCalledTimes(candidates.length + 1)
expect(fsExistsSyncMock).toHaveBeenCalledTimes(candidates.length)
expect(fsRealpathSyncNativeMock).toHaveBeenCalledTimes(1)
candidates.forEach((candidate) => {
expect(fsExistsSyncMock).toHaveBeenCalledWith(candidate)
})
Expand Down Expand Up @@ -191,7 +197,11 @@ describe('Parser', () => {
const parseD = jest.spyOn(parser as any, 'parseD').mockImplementation()
const parseSrc = jest.spyOn(parser as any, 'parseSrc')

fsExistsSyncMock.mockReturnValue(false).mockReturnValueOnce(true)
fsRealpathSyncNativeMock
.mockImplementation(() => {
throw new Error('ENOENT')
})
.mockImplementationOnce((path) => String(path))
fsReadFileSyncMock.mockReturnValue('sub\\file.d\nrecurse.src\n')

await parser['parseSrc'](filepath)
Expand All @@ -209,7 +219,7 @@ describe('Parser', () => {
const parseD = jest.spyOn(parser as any, 'parseD')
const parseSrc = jest.spyOn(parser as any, 'parseSrc')

fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValue('sub\\file.txt\n')

await parser['parseSrc'](filepath, true)
Expand All @@ -223,7 +233,9 @@ describe('Parser', () => {
const filepath = '/path/to/file.src'
const parser = new Parser(patchName, filepath)

fsExistsSyncMock.mockReturnValue(false)
fsRealpathSyncNativeMock.mockImplementation(() => {
throw new Error('ENOENT')
})

await parser['parseSrc'](filepath, true)

Expand All @@ -241,7 +253,7 @@ describe('Parser', () => {
jest.spyOn(parser as any, 'stripPath').mockReturnValue({ fullPath: '/path/to/file.src', relPath: 'file.src' })
const parseSpecial = jest.spyOn(parser as any, 'parseSpecial').mockImplementation()

fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValue('non-path\n')

await parser['parseSrc'](filepath, true)
Expand All @@ -254,7 +266,7 @@ describe('Parser', () => {
const filepath = '/path/to/file.src'
const parser = new Parser(patchName, filepath)

fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValue('some/path/*\n')

await expect(parser['parseSrc'](filepath, true)).rejects.toThrow('Wildcards are not supported')
Expand All @@ -268,15 +280,15 @@ describe('Parser', () => {
const filepath = 'path/to/file.src'
const parser = new Parser(patchName, filepath)

fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValue('some/path/*\n')
jest.spyOn(posix, 'join')
jest.spyOn(glob, 'create').mockResolvedValue({ glob: async () => ['some/path/glob.ext'] } as glob.Globber)

await parser['parseSrc'](filepath, false, true)

expect(posix.join).toHaveBeenCalledWith('path/to', 'some/path/*')
expect(fsExistsSyncMock).toHaveBeenCalledWith(filepath)
expect(fsRealpathSyncNativeMock).toHaveBeenCalledWith(filepath)
expect(fsReadFileSyncMock).toHaveBeenCalledWith(filepath, 'ascii')
expect(glob.create).toHaveBeenCalledWith('path/to/some/path/*')
expect(posix.join).toHaveBeenCalledWith('path/to', 'some/path/glob.ext')
Expand All @@ -295,7 +307,7 @@ describe('Parser', () => {
const parser = new Parser(patchName, filepath, workingDir)

const stripPath = jest.spyOn(parser as any, 'stripPath')
fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValueOnce('const int Symbol1 = 0;')

parser['parseD'](filepath, true)
Expand All @@ -316,7 +328,7 @@ describe('Parser', () => {
const parser = new Parser(patchName, filepath, workingDir)

const stripPath = jest.spyOn(parser as any, 'stripPath')
fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
fsReadFileSyncMock.mockReturnValueOnce('const int Symbol1 = 0;')

parser['parseD'](filepath, false)
Expand All @@ -329,25 +341,14 @@ describe('Parser', () => {
expect(parser.referenceTable).toEqual([])
})

it('should not parse the file if it has an invalid extension', () => {
const patchName = 'test'
const filepath = '/path/to/file.txt'
const parser = new Parser(patchName, filepath)

parser['parseD'](filepath)

expect(fsReadFileSyncMock).not.toHaveBeenCalled()
expect(parser.filelist).toEqual([])
expect(parser.symbolTable).toEqual([])
expect(parser.referenceTable).toEqual([])
})

it('should not parse the file if it does not exist', () => {
const patchName = 'test'
const filepath = '/path/to/nonexistent.d'
const parser = new Parser(patchName, filepath)

fsExistsSyncMock.mockReturnValue(false)
fsRealpathSyncNativeMock.mockImplementation(() => {
throw new Error('ENOENT')
})

parser['parseD'](filepath)

Expand All @@ -364,7 +365,7 @@ describe('Parser', () => {
const parser = new Parser(patchName, filepath)

const stripPath = jest.spyOn(parser as any, 'stripPath')
fsExistsSyncMock.mockReturnValue(true)
fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
;(parser as any).filelist = [relPath]

parser['parseD'](filepath)
Expand Down
20 changes: 15 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,14 @@ export class Parser {
* @throws An error if wildcards are used in the filepath.
*/
protected async parseSrc(filepath: string, root: boolean = false, exclude: boolean = false): Promise<void> {
const { fullPath } = this.stripPath(filepath)
if (!fs.existsSync(fullPath)) return
let { fullPath } = this.stripPath(filepath)

// Check if file exists and correct case
try {
fullPath = normalizePath(fs.realpathSync.native(fullPath))
} catch {
return
}

const srcRootPath = posix.dirname(fullPath)
const input = fs.readFileSync(fullPath, 'ascii')
Expand Down Expand Up @@ -275,11 +281,17 @@ export class Parser {
*
* @param filepath - The path of the file to parse.
* @param exclude - Indicates whether the file is not part of the patch.
* @throws Error if wildcards are used in the filepath.
*/
protected parseD(filepath: string, exclude: boolean = false): void {
const { fullPath, relPath } = this.stripPath(filepath)
if (!fs.existsSync(fullPath)) return
const { fullPath: _fullPath, relPath } = this.stripPath(filepath)

// Check if file exists and correct case
let fullPath: string
try {
fullPath = normalizePath(fs.realpathSync.native(_fullPath))
} catch {
return
}

if (this.filelist.includes(relPath)) return
this.filelist.push(relPath)
Expand Down

0 comments on commit afd580e

Please sign in to comment.