Skip to content
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
8 changes: 4 additions & 4 deletions release/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/formatter/regexp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { newLine } from './'

describe('newLine', () => {
const realProcess = global.process

afterEach(() => {
global.process = realProcess
})

it('should return correct new line regexp entry for windows', () => {
Object.defineProperty(process, 'platform', {
value: 'win32'
Expand All @@ -16,4 +22,13 @@ describe('newLine', () => {

expect(newLine()).toEqual('\\n')
})

it('should return the unix new line when there is no process', () => {
// The formatter runs in browsers too, where process does not exist. This
// is the case that used to throw a ReferenceError.
// @ts-expect-error - simulating a browser, which has no process object
delete global.process

expect(newLine()).toEqual('\\n')
})
})
18 changes: 14 additions & 4 deletions src/formatter/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// FIXME: if isWindows is used, it breaks cli.spec.ts because Logger is undefined
// import { isWindows } from '../'
// export const newLine = () => (isWindows() ? '\\r\\n' : '\\n')
// Kept self-contained rather than importing isWindows from '../': that module
// imports path and is loaded from the package root, which pulls Logger into
// this module and breaks cli.spec.ts with an undefined Logger.

export const newLine = () => (process.platform === 'win32' ? '\\r\\n' : '\\n')
/**
* The line ending to join formatted rows with.
*
* process may not exist - the formatter runs in browsers, to build request
* bodies - so the platform is read defensively and a browser gets the unix
* ending.
*/
export const newLine = () =>
typeof process !== 'undefined' && process.platform === 'win32'
? '\\r\\n'
: '\\n'
34 changes: 34 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isLinux, isWindows } from './utils'

describe('platform helpers', () => {
const realProcess = global.process

afterEach(() => {
global.process = realProcess
})

it('reports windows from process.platform', () => {
Object.defineProperty(process, 'platform', { value: 'win32' })

expect(isWindows()).toBe(true)
expect(isLinux()).toBe(false)
})

it('reports linux from process.platform', () => {
Object.defineProperty(process, 'platform', { value: 'linux' })

expect(isWindows()).toBe(false)
expect(isLinux()).toBe(true)
})

it('answers false for both when there is no process', () => {
// These helpers are exported from the package root and called from browser
// code, where process does not exist. This is the case that used to throw
// a ReferenceError.
// @ts-expect-error - simulating a browser, which has no process object
delete global.process

expect(isWindows()).toBe(false)
expect(isLinux()).toBe(false)
})
})
11 changes: 9 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ export const uuidv4 = () =>

export const uniqArray = (data: any[]) => Array.from(new Set(data))

export const isWindows = () => process.platform === 'win32'
/**
* Whether the host is Windows. process may not exist - these helpers are
* exported from the package root and called from browser code - so the
* platform is read defensively and a browser answers false.
*/
export const isWindows = () =>
typeof process !== 'undefined' && process.platform === 'win32'

export const isLinux = () => process.platform === 'linux'
export const isLinux = () =>
typeof process !== 'undefined' && process.platform === 'linux'

/**
* replace single backslash with double backslash
Expand Down
Loading