Skip to content

Commit

Permalink
Make file paths case-insensitive across OS
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed May 6, 2024
1 parent afd580e commit e7beb70
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 33 deletions.
35 changes: 18 additions & 17 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.mock('@actions/core', () => {

import { Parser } from '../src/parser.js'
import fs from 'fs'
import tcp from 'true-case-path'
import * as io from '@actions/io'
import * as tc from '@actions/tool-cache'
import * as glob from '@actions/glob'
Expand All @@ -20,7 +21,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 trueCasePathSyncMock: jest.SpiedFunction<typeof tcp.trueCasePathSync>
let ioMkdirPMock: jest.SpiedFunction<typeof io.mkdirP>
let ioRmRFMock: jest.SpiedFunction<typeof io.rmRF>
let tcDownloadToolMock: jest.SpiedFunction<typeof tc.downloadTool>
Expand All @@ -30,7 +31,7 @@ describe('Parser', () => {
beforeEach(() => {
fsExistsSyncMock = jest.spyOn(fs, 'existsSync')
fsReadFileSyncMock = jest.spyOn(fs, 'readFileSync')
fsRealpathSyncNativeMock = jest.spyOn(fs.realpathSync, 'native')
trueCasePathSyncMock = jest.spyOn(tcp, 'trueCasePathSync')
})

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

fsExistsSyncMock.mockReturnValue(false).mockReturnValueOnce(true)
fsRealpathSyncNativeMock.mockImplementation(() => {
trueCasePathSyncMock.mockImplementation(() => {
throw new Error('ENOENT')
})
fsReadFileSyncMock.mockReturnValue('').mockReturnValueOnce('test.d\n')
Expand All @@ -147,7 +148,7 @@ describe('Parser', () => {
expect(result).toHaveLength(1)
expect(result[0]).toMatchObject(oneParser)
expect(fsExistsSyncMock).toHaveBeenCalledTimes(candidates.length)
expect(fsRealpathSyncNativeMock).toHaveBeenCalledTimes(1)
expect(trueCasePathSyncMock).toHaveBeenCalledTimes(1)
candidates.forEach((candidate) => {
expect(fsExistsSyncMock).toHaveBeenCalledWith(candidate)
})
Expand Down Expand Up @@ -197,11 +198,11 @@ describe('Parser', () => {
const parseD = jest.spyOn(parser as any, 'parseD').mockImplementation()
const parseSrc = jest.spyOn(parser as any, 'parseSrc')

fsRealpathSyncNativeMock
trueCasePathSyncMock
.mockImplementation(() => {
throw new Error('ENOENT')
})
.mockImplementationOnce((path) => String(path))
.mockImplementationOnce((path) => '/path/to/' + String(path))
fsReadFileSyncMock.mockReturnValue('sub\\file.d\nrecurse.src\n')

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

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

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

fsRealpathSyncNativeMock.mockImplementation(() => {
trueCasePathSyncMock.mockImplementation(() => {
throw new Error('ENOENT')
})

Expand All @@ -253,7 +254,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()

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

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

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

await expect(parser['parseSrc'](filepath, true)).rejects.toThrow('Wildcards are not supported')

expect(fsReadFileSyncMock).toHaveBeenCalledWith(filepath, 'ascii')
expect(fsReadFileSyncMock).toHaveBeenCalledWith('path/to/file.src', 'ascii')
expect(fsReadFileSyncMock).toHaveReturnedWith('some/path/*\n')
})

Expand All @@ -280,15 +281,15 @@ describe('Parser', () => {
const filepath = 'path/to/file.src'
const parser = new Parser(patchName, filepath)

fsRealpathSyncNativeMock.mockImplementation((path) => String(path))
trueCasePathSyncMock.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(fsRealpathSyncNativeMock).toHaveBeenCalledWith(filepath)
expect(trueCasePathSyncMock).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 @@ -307,7 +308,7 @@ describe('Parser', () => {
const parser = new Parser(patchName, filepath, workingDir)

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

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

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

parser['parseD'](filepath, false)
Expand All @@ -346,7 +347,7 @@ describe('Parser', () => {
const filepath = '/path/to/nonexistent.d'
const parser = new Parser(patchName, filepath)

fsRealpathSyncNativeMock.mockImplementation(() => {
trueCasePathSyncMock.mockImplementation(() => {
throw new Error('ENOENT')
})

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

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

parser['parseD'](filepath)
Expand Down
8 changes: 4 additions & 4 deletions __tests__/resources.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import path, { posix } from 'path'
import * as glob from 'glob'
import fs from 'fs'
import tcp from 'true-case-path'
import { Resource } from '../src/resources.ts'

let posixResolveMock: jest.SpiedFunction<typeof posix.resolve>
let pathRelativeMock: jest.SpiedFunction<typeof path.relative>
let globGlobSyncMock: jest.SpiedFunction<typeof glob.globSync>
let resourceValidateMock: jest.SpiedFunction<typeof Resource.prototype.validate>
let fsRealpathSyncNativeMock: jest.SpiedFunction<typeof fs.realpathSync.native>
let trueCasePathSyncMock: jest.SpiedFunction<typeof tcp.trueCasePathSync>

describe('Resource', () => {
beforeEach(() => {
posixResolveMock = jest.spyOn(posix, 'resolve')
pathRelativeMock = jest.spyOn(path, 'relative').mockImplementation((from, to) => String(to.replace(from, '').replace(/^\//, '')))
globGlobSyncMock = jest.spyOn(glob, 'globSync')
fsRealpathSyncNativeMock = jest.spyOn(fs.realpathSync, 'native').mockImplementation((path) => String(path))
trueCasePathSyncMock = jest.spyOn(tcp, 'trueCasePathSync').mockImplementation((path) => String(path))
})

describe('constructor', () => {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Resource', () => {
resource.validate()

expect(globGlobSyncMock).toHaveBeenCalled()
expect(fsRealpathSyncNativeMock).toHaveBeenCalled()
expect(trueCasePathSyncMock).toHaveBeenCalled()
expect(pathRelativeMock).toHaveBeenCalled()
expect(resource['extViolations']).toEqual([
{ file: '_work/data/Textures/file.wrg', name: '.wrg', line: 1 },
Expand Down
115 changes: 110 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.

Loading

0 comments on commit e7beb70

Please sign in to comment.