A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
TypeScript amazon-primeelectronepic-games-launcherepic-games-storeepic-storegaminggoggog-galaxyheroic-games-launcherlinuxplay-gamesprotonsteamwine
import { exec, spawn } from 'child_process'
import { existsSync, realpathSync } from 'graceful-fs'
import { EventEmitter } from 'events'
import { moveOnUnix } from '../utils'
jest.mock('child_process', () => ({ spawn: jest.fn(), exec: jest.fn() }))
jest.mock('backend/logger', () => ({
LogPrefix: { Backend: 'Backend', Gog: 'Gog' },
logInfo: jest.fn(),
logWarning: jest.fn(),
logError: jest.fn(),
logDebug: jest.fn()
}))
jest.mock('graceful-fs', () => ({
...jest.requireActual('graceful-fs'),
existsSync: jest.fn(() => true),
realpathSync: jest.fn((value: string) => value)
}))
type FakeChild = EventEmitter & { stdout: EventEmitter; stderr: EventEmitter }
function mockRun(exitCode: number) {
;(spawn as unknown as jest.Mock).mockImplementation(() => {
const child = new EventEmitter() as FakeChild
child.stdout = new EventEmitter()
child.stderr = new EventEmitter()
setImmediate(() => child.emit('close', exitCode))
return child
})
}
function spawnCalls(): Array<[string, string[]]> {
return (spawn as unknown as jest.Mock).mock.calls.map(
(call) => call.slice(0, 2) as [string, string[]]
)
}
const gameInfo = {
app_name: 'test',
title: 'Test',
runner: 'gog',
install: { install_path: '/games/Test' }
} as never
describe('moveOnUnix', () => {
beforeEach(() => {
jest.clearAllMocks()
;(exec as unknown as jest.Mock).mockImplementation(
(_command: string, callback: (err: null, out: string) => void) =>
callback(null, '/usr/bin/rsync')
)
;(existsSync as jest.Mock).mockReturnValue(true)
;(realpathSync as unknown as jest.Mock).mockImplementation(
(value: string) => value
)
})
it('refuses to move a game into its own folder', async () => {
const result = await moveOnUnix('/games', gameInfo)
expect(result.status).toBe('error')
expect(spawnCalls().some(([command]) => command === 'rm')).toBe(false)
expect(spawnCalls().some(([command]) => command === 'rsync')).toBe(false)
})
it('refuses the same folder reached through a symlink', async () => {
// Fedora Atomic and friends mount the home as a symlink, so two paths that
// look different are the same folder on disk.
;(realpathSync as unknown as jest.Mock).mockImplementation(
(value: string) =>
value.startsWith('/home/user')
? value.replace('/home/user', '/var/home/user')
: value
)
const result = await moveOnUnix('/var/home/user/games', {
app_name: 'test',
title: 'Test',
runner: 'gog',
install: { install_path: '/home/user/games/Test' }
} as never)
expect(result.status).toBe('error')
expect(spawnCalls().some(([command]) => command === 'rm')).toBe(false)
expect(spawnCalls().some(([command]) => command === 'rsync')).toBe(false)
})
it('does not delete the install when the copy fails', async () => {
mockRun(11)
const result = await moveOnUnix('/new-place', gameInfo)
expect(result.status).toBe('error')
expect(spawnCalls().some(([command]) => command === 'rsync')).toBe(true)
expect(spawnCalls().some(([command]) => command === 'rm')).toBe(false)
})
it('removes the old folder only after a successful copy', async () => {
mockRun(0)
const result = await moveOnUnix('/new-place', gameInfo)
expect(result.status).toBe('done')
expect(spawnCalls().some(([command]) => command === 'rm')).toBe(true)
})
})