From 67ff19fe30069f0c6dfe030ecd031507c14d46a4 Mon Sep 17 00:00:00 2001 From: Alex-D Date: Mon, 7 Mar 2022 03:02:48 +0100 Subject: [PATCH] style: fix lint issues due to eslint config bump --- src/index.ts | 38 ++++++++++++++----- src/types/dependencies.ts | 8 ++-- test/__helpers__/mockDependencies.ts | 6 +-- .../getFirstExistingParentPath.spec.ts | 2 +- test/index.spec.ts | 8 ++-- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index ec7c2f3..8601a06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ -import {execFile} from 'child_process' -import {existsSync} from 'fs' -import {release} from 'os' -import {normalize, sep} from 'path' +import { execFile } from 'child_process' +import { existsSync } from 'fs' +import { release } from 'os' +import { normalize, sep } from 'path' import InvalidPathError from '@/src/errors/invalidPathError' import NoMatchError from '@/src/errors/noMatchError' @@ -24,6 +24,9 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = { pathSep: sep, cpExecFile: execFile, }): Promise { + // Note: This function contains other functions in order + // to wrap them in a common context and make unit tests easier + /** * Maps command output to a normalized object {diskPath, free, size} * @@ -72,10 +75,14 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = { coefficient = 1, ): Promise { return new Promise((resolve, reject) => { - const [file, ...args] = cmd + const [ + file, + ...args + ] = cmd + /* istanbul ignore if */ if (file === undefined) { - return Promise.reject('cmd must contain at least one item') + return Promise.reject(new Error('cmd must contain at least one item')) } dependencies.cpExecFile(file, args, (error, stdout) => { @@ -104,8 +111,16 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = { }) } - const powershellCmd = ['powershell', 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size'] - const wmicCmd = ['wmic', 'logicaldisk', 'get', 'size,freespace,caption'] + const powershellCmd = [ + 'powershell', + 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size', + ] + const wmicCmd = [ + 'wmic', + 'logicaldisk', + 'get', + 'size,freespace,caption', + ] const cmd = hasPowerShell3(dependencies.release) ? powershellCmd : wmicCmd return check( @@ -138,7 +153,12 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = { const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies) return check( - ['df', '-Pk', '--', pathToCheck], + [ + 'df', + '-Pk', + '--', + pathToCheck, + ], () => true, // We should only get one line, so we did not need to filter { diskPath: 5, diff --git a/src/types/dependencies.ts b/src/types/dependencies.ts index a4cfe33..073e335 100644 --- a/src/types/dependencies.ts +++ b/src/types/dependencies.ts @@ -1,6 +1,8 @@ -import {ChildProcess, ExecFileException} from 'child_process' -import {existsSync} from 'fs' -import {normalize, sep} from 'path' +import { ChildProcess, ExecException } from 'child_process' +import { existsSync } from 'fs' +import { normalize, sep } from 'path' + +type ExecFileException = ExecException & NodeJS.ErrnoException type Dependencies = { platform: NodeJS.Platform diff --git a/test/__helpers__/mockDependencies.ts b/test/__helpers__/mockDependencies.ts index e6fe799..b1a97ed 100644 --- a/test/__helpers__/mockDependencies.ts +++ b/test/__helpers__/mockDependencies.ts @@ -1,6 +1,6 @@ -import {ChildProcess} from 'child_process' -import {EventEmitter} from 'events' -import {normalize} from 'path' +import { ChildProcess } from 'child_process' +import { EventEmitter } from 'events' +import { normalize } from 'path' import Dependencies from '@/src/types/dependencies' diff --git a/test/functions/getFirstExistingParentPath.spec.ts b/test/functions/getFirstExistingParentPath.spec.ts index c5da6ea..3b8dcdf 100644 --- a/test/functions/getFirstExistingParentPath.spec.ts +++ b/test/functions/getFirstExistingParentPath.spec.ts @@ -1,5 +1,5 @@ import test from 'ava' -import {PathLike} from 'fs' +import { PathLike } from 'fs' import getFirstExistingParentPath from '@/src/functions/getFirstExistingParentPath' import mockDependencies from '@/test/__helpers__/mockDependencies' diff --git a/test/index.spec.ts b/test/index.spec.ts index f375f52..6ab83a0 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -160,7 +160,7 @@ test('win32: path did not match any disk', async t => { }) const error = await t.throwsAsync(checkDiskSpace('Z:/shouldfail', dependencies)) - t.is(error.name, 'NoMatchError') + t.is(error?.name, 'NoMatchError') }) test('win32: invalid path', async t => { @@ -171,7 +171,7 @@ test('win32: invalid path', async t => { }) const error = await t.throwsAsync(checkDiskSpace('an invalid path', dependencies)) - t.is(error.name, 'InvalidPathError') + t.is(error?.name, 'InvalidPathError') }) test('unix: invalid path', async t => { @@ -182,7 +182,7 @@ test('unix: invalid path', async t => { }) const error = await t.throwsAsync(checkDiskSpace('an invalid path', dependencies)) - t.is(error.name, 'InvalidPathError') + t.is(error?.name, 'InvalidPathError') }) test('exec has an error', async t => { @@ -193,7 +193,7 @@ test('exec has an error', async t => { }) const error = await t.throwsAsync(checkDiskSpace('C:/something', dependencies)) - t.is(error.message, 'some error') + t.is(error?.message, 'some error') }) test('run without dependencies mock', async t => {