Skip to content

Commit

Permalink
Download scripts globally beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Jun 14, 2024
1 parent ba92e48 commit b48316b
Show file tree
Hide file tree
Showing 7 changed files with 526 additions and 475 deletions.
6 changes: 6 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let runMock: jest.SpiedFunction<typeof main.run>

describe('run', () => {
beforeEach(() => {
jest.spyOn(Parser, 'downloadSpecial').mockResolvedValue()
jest
.spyOn(inputs, 'loadInputs')
.mockReturnValue({ workingDir: '', basePath: '', patchName: '', prefixList: [], ignoreListDecl: [], ignoreListRsc: [] })
Expand All @@ -27,6 +28,7 @@ describe('run', () => {
const result = await main.run(true)
expect(runMock).toHaveReturned()
expect(cleanup.workflow).toHaveBeenCalledTimes(1)
expect(Parser.downloadSpecial).toHaveBeenCalledTimes(1)
expect(inputs.loadInputs).toHaveBeenCalledTimes(1)
expect(inputs.formatFilters).toHaveBeenCalledTimes(1)
expect(Parser.from).toHaveBeenCalledTimes(1)
Expand All @@ -43,6 +45,7 @@ describe('run', () => {
await main.run(true)
expect(runMock).toHaveReturned()
expect(cleanup.workflow).toHaveBeenCalledTimes(1)
expect(Parser.downloadSpecial).not.toHaveBeenCalled()
expect(inputs.loadInputs).not.toHaveBeenCalled()
expect(inputs.formatFilters).not.toHaveBeenCalled()
expect(Parser.from).not.toHaveBeenCalled()
Expand All @@ -60,6 +63,7 @@ describe('run', () => {
await main.run(true)
expect(runMock).toHaveReturned()
expect(cleanup.workflow).toThrow('test error')
expect(Parser.downloadSpecial).not.toHaveBeenCalled()
expect(inputs.loadInputs).not.toHaveBeenCalled()
expect(inputs.formatFilters).not.toHaveBeenCalled()
expect(Parser.from).not.toHaveBeenCalled()
Expand All @@ -79,6 +83,7 @@ describe('run', () => {
await main.run(true)
expect(runMock).toHaveReturned()
expect(cleanup.workflow).toThrow('test error')
expect(Parser.downloadSpecial).not.toHaveBeenCalled()
expect(inputs.loadInputs).not.toHaveBeenCalled()
expect(inputs.formatFilters).not.toHaveBeenCalled()
expect(Parser.from).not.toHaveBeenCalled()
Expand All @@ -101,6 +106,7 @@ describe('run', () => {

expect(runMock).toHaveReturned()
expect(cleanup.workflow).not.toHaveBeenCalled()
expect(Parser.downloadSpecial).toHaveBeenCalledTimes(1)
expect(inputs.loadInputs).toThrow('test error')
expect(console.error).toHaveBeenCalledWith('test error')
expect(inputs.formatFilters).not.toHaveBeenCalled()
Expand Down
50 changes: 11 additions & 39 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 { posix } from 'path'
import os from 'os'

Expand All @@ -23,10 +22,6 @@ let consoleLogMock: jest.SpiedFunction<typeof console.log>
let fsExistsSyncMock: jest.SpiedFunction<typeof fs.existsSync>
let fsReadFileSyncMock: jest.SpiedFunction<typeof fs.readFileSync>
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>
let tcExtractTarMock: jest.SpiedFunction<typeof tc.extractTar>

describe('Parser', () => {
beforeEach(() => {
Expand Down Expand Up @@ -742,16 +737,21 @@ func void Symbol11(var int Symbol12, var string Symbol13, var Symbol5 Symbol14)
})

describe('parseSpecial', () => {
beforeEach(() => {
ioMkdirPMock = jest.spyOn(io, 'mkdirP').mockResolvedValue()
ioRmRFMock = jest.spyOn(io, 'rmRF').mockResolvedValue()
tcDownloadToolMock = jest.spyOn(tc, 'downloadTool')
tcExtractTarMock = jest.spyOn(tc, 'extractTar')

beforeAll(async () => {
// Fix path in environment variables
if (!('PATH' in process.env) && 'Path' in process.env) {
jest.replaceProperty(process, 'env', { ...process.env, PATH: process.env['Path'] })
}

const tmpDir = posix.join(os.tmpdir(), '.patch-validator-tmp')
jest.replaceProperty(process, 'env', { ...process.env, RUNNER_TEMP: tmpDir })

await Parser.downloadSpecial()
})

beforeEach(() => {
const tmpDir = posix.join(os.tmpdir(), '.patch-validator-tmp')
jest.replaceProperty(process, 'env', { ...process.env, RUNNER_TEMP: tmpDir })
})

afterAll(async () => {
Expand All @@ -766,19 +766,13 @@ func void Symbol11(var int Symbol12, var string Symbol13, var Symbol5 Symbol14)
const parser = new Parser(patchName, filepath)

jest.replaceProperty(process, 'env', { ...process.env, RUNNER_TEMP: undefined })
tcDownloadToolMock.mockResolvedValue('/path/to/ikarus.tar.gz')
tcExtractTarMock.mockResolvedValue('/path/to/ikarus')
const posixJoin = jest.spyOn(posix, 'join')
const parseSrc = jest.spyOn(parser as any, 'parseSrc').mockImplementation()
;(parser as any)['type'] = 'CONTENT'
;(parser as any)['version'] = 1
await parser['parseSpecial']('Ikarus')

expect(posixJoin).toHaveBeenCalledWith(expect.stringMatching(/\.patch-validator-special$/), 'Ikarus-gameversions', 'Ikarus_G1.src')
expect(tcDownloadToolMock).toHaveBeenCalledWith('https://github.com/Lehona/Ikarus/archive/refs/heads/gameversions.tar.gz')
expect(ioMkdirPMock).toHaveBeenCalledWith(expect.stringMatching(/\.patch-validator-special$/))
expect(tcExtractTarMock).toHaveBeenCalledWith('/path/to/ikarus.tar.gz', expect.stringMatching(/\.patch-validator-special$/))
expect(ioRmRFMock).toHaveBeenCalledWith('/path/to/ikarus.tar.gz')
expect(parseSrc).toHaveBeenCalledWith(
expect.stringMatching(/\.patch-validator-special\/Ikarus-gameversions\/Ikarus_G1.src$/),
false,
Expand Down Expand Up @@ -815,17 +809,11 @@ func void Symbol11(var int Symbol12, var string Symbol13, var Symbol5 Symbol14)

const posixJoin = jest.spyOn(posix, 'join')
const parseSrc = jest.spyOn(parser as any, 'parseSrc').mockImplementation()
tcDownloadToolMock.mockResolvedValue('/path/to/lego.tar.gz')
tcExtractTarMock.mockResolvedValue('/path/to/lego')
;(parser as any)['type'] = 'CONTENT'
;(parser as any)['version'] = 2
await parser['parseSpecial']('LeGo')

expect(posixJoin).toHaveBeenCalledWith(expect.stringMatching(/\.patch-validator-special$/), 'LeGo-gameversions', 'Header_G2.src')
expect(tcDownloadToolMock).toHaveBeenCalledWith('https://github.com/Lehona/LeGo/archive/refs/heads/gameversions.tar.gz')
expect(ioMkdirPMock).toHaveBeenCalledWith(expect.stringMatching(/\.patch-validator-special$/))
expect(tcExtractTarMock).toHaveBeenCalledWith('/path/to/lego.tar.gz', expect.stringMatching(/\.patch-validator-special$/))
expect(ioRmRFMock).toHaveBeenCalledWith('/path/to/lego.tar.gz')
expect(parseSrc).toHaveBeenCalledWith(
expect.stringMatching(/\.patch-validator-special\/LeGo-gameversions\/Header_G2.src$/),
false,
Expand Down Expand Up @@ -868,14 +856,6 @@ func void Symbol11(var int Symbol12, var string Symbol13, var Symbol5 Symbol14)
const filepath = '/path/to/Content_G130.src'
const parser = new Parser(patchName, filepath)

ioMkdirPMock.mockRestore()
ioRmRFMock.mockRestore()
tcDownloadToolMock.mockRestore()
tcExtractTarMock.mockRestore()

const tmpDir = posix.join(os.tmpdir(), '.patch-validator-tmp')
jest.replaceProperty(process, 'env', { ...process.env, RUNNER_TEMP: tmpDir })

await parser['parseSpecial']('Ikarus')

expect(parser.symbolTable.length).toBeGreaterThan(5000)
Expand All @@ -890,14 +870,6 @@ func void Symbol11(var int Symbol12, var string Symbol13, var Symbol5 Symbol14)
const filepath = '/path/to/Content_G112.src'
const parser = new Parser(patchName, filepath)

ioMkdirPMock.mockRestore()
ioRmRFMock.mockRestore()
tcDownloadToolMock.mockRestore()
tcExtractTarMock.mockRestore()

const tmpDir = posix.join(os.tmpdir(), '.patch-validator-tmp')
jest.replaceProperty(process, 'env', { ...process.env, RUNNER_TEMP: tmpDir })

await parser['parseSpecial']('LeGo')

expect(parser.symbolTable.length).toBeGreaterThan(5000)
Expand Down
Loading

0 comments on commit b48316b

Please sign in to comment.