Skip to content

Commit

Permalink
style: fix lint issues due to eslint config bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-D committed Mar 7, 2022
1 parent 977f018 commit 67ff19f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
38 changes: 29 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,6 +24,9 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = {
pathSep: sep,
cpExecFile: execFile,
}): Promise<DiskSpace> {
// 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}
*
Expand Down Expand Up @@ -72,10 +75,14 @@ function checkDiskSpace(directoryPath: string, dependencies: Dependencies = {
coefficient = 1,
): Promise<DiskSpace> {
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) => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/types/dependencies.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/__helpers__/mockDependencies.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
2 changes: 1 addition & 1 deletion test/functions/getFirstExistingParentPath.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
8 changes: 4 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand Down

0 comments on commit 67ff19f

Please sign in to comment.