-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
3,758 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- node | ||
cache: | ||
yarn: true | ||
directories: | ||
- node_modules | ||
script: | ||
- yarn run test:all | ||
after_success: | ||
- yarn run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* tslint:disable variable-name */ | ||
|
||
import { ChildProcess } from 'child_process'; | ||
import { EventEmitter } from 'events'; | ||
import { Readable } from 'stream'; | ||
|
||
const spawn: jest.Mock<ChildProcess> & typeof mockMethods = jest.fn() as any; | ||
|
||
beforeEach(() => spawn.__setOutput('default_stdout')); | ||
afterEach(() => spawn.mockReset()); | ||
|
||
function createStream(value?: string | null) { | ||
return new Readable({ | ||
read() { | ||
if (value != null) { | ||
this.push(Buffer.from(value)); | ||
this.destroy(); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
const mockMethods = { | ||
__emitError(error?: any) { | ||
const child = new EventEmitter() as ChildProcess; | ||
spawn.mockImplementation(() => { | ||
child.stdout = createStream(); | ||
child.stderr = createStream(); | ||
process.nextTick(() => child.emit('error', error)); | ||
return child; | ||
}); | ||
}, | ||
__setOutput(stdoutData: string | null, stderrData?: string) { | ||
const child = new EventEmitter() as ChildProcess; | ||
const close = () => child.emit('close'); | ||
spawn.mockImplementation(() => { | ||
child.stdout = createStream(stdoutData).on('close', close); | ||
child.stderr = createStream(stderrData).on('close', close); | ||
return child; | ||
}); | ||
}, | ||
}; | ||
|
||
declare module 'child_process' { | ||
namespace spawn { | ||
export const __setOutput: typeof mockMethods['__setOutput']; | ||
export const __emitError: typeof mockMethods['__emitError']; | ||
} | ||
} | ||
|
||
Object.assign(spawn, mockMethods); | ||
|
||
export { spawn }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { spawn } from 'child_process'; | ||
import osascript from '../lib'; | ||
|
||
jest.mock('child_process'); | ||
|
||
beforeEach(() => { | ||
Object.defineProperty(process, 'platform', { value: 'darwin' }); | ||
}); | ||
|
||
describe('osascript.jxa', () => { | ||
it('calls osascript with correct arguments', async () => { | ||
await osascript.jxa`test`; | ||
const expectedArgs = ['-l', 'JavaScript', '-e', '(function(...argv){test})()']; | ||
expect(spawn).toHaveBeenCalledWith('osascript', expectedArgs); | ||
}); | ||
|
||
it('calls osascript with custom options', async () => { | ||
await osascript.jxa({ flags: 'oe', argv: ['foo', 1] })`test`; | ||
const expectedArgs = ['-l', 'JavaScript', '-s', 'oe', '-e', '(function(...argv){test})("foo",1)']; | ||
expect(spawn).toHaveBeenCalledWith('osascript', expectedArgs); | ||
}); | ||
|
||
test('returns parsed result', async () => { | ||
spawn.__setOutput('{ "foo": 1, "bar": true, "baz": "baz"}'); | ||
const result = await osascript.jxa({ parse: true })`test`; | ||
const expectedArgs = ['-l', 'JavaScript', '-s', 's', '-e', '(function(...argv){test})()']; | ||
expect(spawn).toHaveBeenCalledWith('osascript', expectedArgs); | ||
expect(result).toEqual({ foo: 1, bar: true, baz: 'baz' }); | ||
}); | ||
|
||
test('rejects when parsing fails', async () => { | ||
spawn.__setOutput('not_valid_json'); | ||
try { | ||
await osascript.jxa({ parse: true })``; | ||
} catch (error) { | ||
expect(error.message).toBe('Unexpected token o in JSON at position 1'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { spawn } from 'child_process'; | ||
import osascript from '../lib'; | ||
|
||
jest.mock('child_process'); | ||
|
||
beforeEach(() => { | ||
Object.defineProperty(process, 'platform', { value: 'darwin' }); | ||
}); | ||
|
||
describe('osascript', () => { | ||
it('throws when not using macOS', () => { | ||
const original = process.platform; | ||
Object.defineProperty(process, 'platform', { value: 'lcars' }); | ||
expect(osascript``).rejects.toThrowError('osascript-tag requires macOS'); | ||
}); | ||
|
||
it('calls osascript', async () => { | ||
try { | ||
await osascript`test`; | ||
} finally { | ||
expect(spawn).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
|
||
it('calls osascript with the default argument', async () => { | ||
await osascript`test`; | ||
// @ts-ignore | ||
await osascript()`test`; | ||
const expectedArgs = ['-l', 'AppleScript', '-e', 'test']; | ||
expect(spawn).toHaveBeenNthCalledWith(1, 'osascript', expectedArgs); | ||
expect(spawn).toHaveBeenNthCalledWith(2, 'osascript', expectedArgs); | ||
}); | ||
|
||
it('calls osascript with custom argument', async () => { | ||
await osascript({ flags: 'eh', language: 'JavaScript' })`test`; | ||
const expectedArgs = ['-l', 'JavaScript', '-s', 'eh', '-e', expect.stringContaining('test')]; | ||
expect(spawn).toHaveBeenCalledWith('osascript', expectedArgs); | ||
}); | ||
|
||
it('resolves with the script stdout', () => { | ||
spawn.__setOutput('result'); | ||
expect(osascript`test`).resolves.toBe('result'); | ||
}); | ||
|
||
it('rejects with the script stderr', () => { | ||
spawn.__setOutput(null, 'error'); | ||
expect(osascript`test`).rejects.toBe('error'); | ||
}); | ||
|
||
it('rejects with on child process error', () => { | ||
spawn.__emitError('error'); | ||
expect(osascript`test`).rejects.toBe('error'); | ||
}); | ||
|
||
it('builds template strings', async () => { | ||
await osascript`foo ${'bar'}`; | ||
expect(spawn).toHaveBeenCalledWith('osascript', expect.arrayContaining(['foo bar'])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"declaration": true | ||
}, | ||
"include": ["lib"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.