|
| 1 | +import fs from 'fs' |
| 2 | +import * as util_os_processes from '../../os/processes' |
| 3 | +import { getDiskInfo_unix } from '../unix' |
| 4 | +import type { Path } from 'backend/schemas' |
| 5 | + |
| 6 | +describe('getDiskInfo_unix', () => { |
| 7 | + it('Works with root path', async () => { |
| 8 | + const accessSyp = jest |
| 9 | + .spyOn(fs.promises, 'access') |
| 10 | + .mockImplementation(async () => Promise.resolve()) |
| 11 | + const spawnWrapperSpy = jest |
| 12 | + .spyOn(util_os_processes, 'genericSpawnWrapper') |
| 13 | + .mockImplementation(async () => { |
| 14 | + return Promise.resolve({ |
| 15 | + stdout: |
| 16 | + 'Filesystem 1024-blocks Used Available Capacity Mounted on\n/dev/sda1 100 90 10 90% /', |
| 17 | + stderr: '', |
| 18 | + exitCode: null, |
| 19 | + signalName: null |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + const ret = await getDiskInfo_unix('/' as Path) |
| 24 | + expect(ret.totalSpace).toBe(100 * 1024) |
| 25 | + expect(ret.freeSpace).toBe(10 * 1024) |
| 26 | + expect(accessSyp).toHaveBeenCalledTimes(1) |
| 27 | + expect(spawnWrapperSpy).toHaveBeenCalledWith('df', ['-P', '-k', '/']) |
| 28 | + expect(spawnWrapperSpy).toHaveBeenCalledTimes(1) |
| 29 | + }) |
| 30 | + |
| 31 | + it('Works with nested path', async () => { |
| 32 | + const accessSyp = jest |
| 33 | + .spyOn(fs.promises, 'access') |
| 34 | + .mockImplementation(async (path) => { |
| 35 | + if (path === '/foo/bar/baz') { |
| 36 | + return Promise.reject() |
| 37 | + } |
| 38 | + return Promise.resolve() |
| 39 | + }) |
| 40 | + const spawnWrapperSpy = jest |
| 41 | + .spyOn(util_os_processes, 'genericSpawnWrapper') |
| 42 | + .mockImplementation(async () => { |
| 43 | + return Promise.resolve({ |
| 44 | + stdout: |
| 45 | + 'Filesystem 1024-blocks Used Available Capacity Mounted on\n/dev/sda1 100 90 10 90% /foo/bar', |
| 46 | + stderr: '', |
| 47 | + exitCode: null, |
| 48 | + signalName: null |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + const ret = await getDiskInfo_unix('/foo/bar/baz' as Path) |
| 53 | + expect(ret.totalSpace).toBe(100 * 1024) |
| 54 | + expect(ret.freeSpace).toBe(10 * 1024) |
| 55 | + expect(accessSyp).toHaveBeenCalledTimes(2) |
| 56 | + expect(spawnWrapperSpy).toHaveBeenCalledWith('df', ['-P', '-k', '/foo/bar']) |
| 57 | + expect(spawnWrapperSpy).toHaveBeenCalledTimes(1) |
| 58 | + }) |
| 59 | +}) |
0 commit comments