Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move max buffer to constants #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/helpers/constansts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const DEFAULT_UPLOAD_HOST = 'https://codecov.io'

export const SPAWNPROCESSBUFFERSIZE = 1_048_576 * 100 // 100 MiB
3 changes: 2 additions & 1 deletion src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UploaderArgs } from '../types'
import { logError, UploadLogger } from './logger'
import { runExternalProgram } from './util'
import micromatch from "../vendor/micromatch/index.js";
import { SPAWNPROCESSBUFFERSIZE } from './constansts'

export const MARKER_NETWORK_END = '\n<<<<<< network\n'
export const MARKER_FILE_END = '<<<<<< EOF\n'
Expand Down Expand Up @@ -238,7 +239,7 @@ export function getAllFiles(
const { stdout, status, error } = spawnSync(
'git',
['-C', dirPath, 'ls-files'],
{ encoding: 'utf8', maxBuffer: 100 * 1024 * 1024 },
{ encoding: 'utf8', maxBuffer: SPAWNPROCESSBUFFERSIZE },
)

let files = []
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import childprocess from 'child_process'
import { SPAWNPROCESSBUFFERSIZE } from './constansts'
export { SPAWNPROCESSBUFFERSIZE } from './constansts'


export const SPAWNPROCESSBUFFERSIZE = 1_048_576 * 10 // 10 MiB

export function isProgramInstalled(programName: string): boolean {
return !childprocess.spawnSync(programName).error
Expand Down
83 changes: 41 additions & 42 deletions test/helpers/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
import childProcess from 'child_process'
import td from 'testdouble'
import {
isProgramInstalled,
runExternalProgram,
SPAWNPROCESSBUFFERSIZE,
} from '../../src/helpers/util'
import { isProgramInstalled, runExternalProgram } from '../../src/helpers/util'
import { SPAWNPROCESSBUFFERSIZE } from '../../src/helpers/constansts'

describe('isProgramInstalled()', () => {
afterEach(() => {
td.reset()
})

it('should throw an error when program is not found', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(spawnSync('fooProg')).thenReturn({
error: 'Huh?',
})
expect(isProgramInstalled('fooProg')).toBeFalsy()

afterEach(() => {
td.reset()
})

it('should throw an error when program is not found', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(spawnSync('fooProg')).thenReturn({
error: 'Huh?',
})
expect(isProgramInstalled('fooProg')).toBeFalsy()
})
})

describe('runExternalProgram()', () => {
afterEach(() => {
td.reset()
afterEach(() => {
td.reset()
})

it('should throw an error when program is not found', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }),
).thenReturn({
error: 'Huh?',
})

it('should throw an error when program is not found', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE })).thenReturn({
error: 'Huh?',
})
expect(() => runExternalProgram('ls', ['-geewhiz'])).toThrowError(/Huh\?/)

expect(() => runExternalProgram('ls', ['-geewhiz'])).toThrowError(/Huh\?/)
})

it('should return stdout on success', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }),
).thenReturn({
stdout: 'I am output',
})
expect(runExternalProgram('ls', ['-geewhiz'])).toEqual('I am output')
})

it('should return stdout on success', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE })).thenReturn({
stdout: 'I am output',
})
expect(runExternalProgram('ls', ['-geewhiz'])).toEqual("I am output")

})

it('should return a trimmed string with no newlines', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE })).thenReturn({
stdout: `
it('should return a trimmed string with no newlines', () => {
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('ls', ['-geewhiz'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }),
).thenReturn({
stdout: `


I am output


`,
})
expect(runExternalProgram('ls', ['-geewhiz'])).toEqual("I am output")

})
expect(runExternalProgram('ls', ['-geewhiz'])).toEqual('I am output')
})
})