Skip to content

Commit

Permalink
initial tests for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
BoruiGu committed Dec 6, 2021
1 parent 9f471f0 commit 775baa1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}",
"args": ["--experimental-vm-modules", "node_modules/jest/bin/jest.js", "--runInBand", "--watchAll=false"],
"autoAttachChildProcesses": true
"autoAttachChildProcesses": true,
"skipFiles": []
}
]
}
42 changes: 32 additions & 10 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import tty from 'tty'
import { execa, Options } from 'execa'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { jest } from '@jest/globals'

describe('cli', () => {
const cliPath = resolve(dirname(fileURLToPath(import.meta.url)), '../src/cli.ts')
const env = { SPLUNK_URL: 'url', SPLUNK_TOKEN: 'token' }
const options: Options = { env, preferLocal: true }
const options: Options = { env, preferLocal: true, shell: true }

const send = jest.fn()
const flush = jest.fn()
jest.mock('splunk-logging', () => ({
Logger: () => ({ send, flush }),
}))

test('prints help and version', async () => {
let stdout = (await execa('tsm', [cliPath, '-h'], options)).stdout
Expand All @@ -23,25 +30,40 @@ describe('cli', () => {
})

test('prints help and exit if no data piped into command', async () => {
const stdout = (await execa('tsm', [cliPath], options).catch((err) => err)).stdout
const { stdout } = await execa('tsm', [cliPath], options).catch((err) => err)
expect(stdout).toContain('Usage')
})

test('prints help and exit if stdin is tty', async () => {
const stdout = (await execa('tsm', [cliPath], { ...options, stdin: new tty.ReadStream(0) }).catch((err) => err)).stdout
const { stdout } = await execa('tsm', [cliPath], { ...options, stdin: new tty.ReadStream(0) }).catch((err) => err)
expect(stdout).toContain('Usage')
})

// test('prints help and exit if missing environment variables', async () => {
// const echo = execa('echo', ['abc'])
// const ssc = execa('tsm', [cliPath], { stdin: echo.stdout })
// let stdout = await execa(`echo abc | npx tsm ${cliPath}`, { ...options, env: { path: process.env.PATH } }).catch((err) => err)
// // expect(stdout.stdout).toContain('environment variables')
test('exit if missing environment variables', async () => {
const { stdout, stderr } = await execa(`echo abc | tsm ${cliPath}`, {
...options,
env: { ...process.env, SPLUNK_URL: undefined, SPLUNK_TOKEN: undefined },
}).catch((err) => err)

expect(stdout).toBe('')
expect(stderr).toContain('environment variables')
expect(send).not.toHaveBeenCalled()
expect(flush).not.toHaveBeenCalled()
})

// test('send logs to splunk and stdout', async () => {
// jest.spyOn(console, 'log')

// const { stdout, stderr } = await execa(`echo abc | tsm ${cliPath}`, options).catch((err) => err)

// expect(stdout).toBe('abc\ndef\n')
// expect(stderr).toContain('2 logs')
// expect(send).toHaveBeenCalledTimes(2)
// expect(flush).not.toHaveBeenCalled()
// })

// describe('no env var')
// describe('options')
// describe('logging and error handling')
// describe('logging and error handling') (stdout should not have [ssc]!)
// describe('ctrl+c shutdown code')
// describe('ctrl+c after stdin end')
})

0 comments on commit 775baa1

Please sign in to comment.