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
4 changes: 3 additions & 1 deletion src/hooks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module.exports = async function (opts) {
// init event does not post telemetry, it stores some info that will be used later
// this will prompt to optIn/Out if telemetry.optIn is undefined
// todo: don't prompt if it is a telemetry command, like `aio telemetry off` should not ask if you want to turn it on first ...
if ((opts.argv.indexOf('--no-telemetry') < 0) && telemetryLib.isNull()) {
if ((opts.argv.indexOf('--no-telemetry') < 0) &&
!process.env.CI &&
telemetryLib.isNull()) {
// let's ask!
// unfortunately the `oclif-dev readme` run by prepack fires this event, which hangs CI
if (opts.id !== 'readme') {
Expand Down
35 changes: 34 additions & 1 deletion test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('hook interfaces', () => {
* post results
*/
test('init prompt accept:true', async () => {
const preEnv = process.env
process.env = { ...preEnv, CI: undefined }
const hook = require('../src/hooks/init')
expect(typeof hook).toBe('function')
inquirer.prompt = jest.fn().mockResolvedValue({ accept: true })
Expand All @@ -54,9 +56,10 @@ describe('hook interfaces', () => {
expect(fetch).toHaveBeenCalledWith(expect.any(String),
expect.objectContaining({ body: expect.stringContaining('"_adobeio":{"eventType":"telemetry-prompt","eventData":"accepted"') }))
expect(fetch).toHaveBeenCalledTimes(1)
process.env = preEnv
})

test('init prompt - dont run in CI', async () => {
test('init prompt - dont run when oclif is generating readme', async () => {
const hook = require('../src/hooks/init')
expect(typeof hook).toBe('function')
inquirer.prompt = jest.fn().mockResolvedValue({ accept: true })
Expand All @@ -66,11 +69,40 @@ describe('hook interfaces', () => {
expect(fetch).not.toHaveBeenCalled()
})

test('init prompt - dont run when oclif is generating readme and CI is off', async () => {
const preEnv = process.env
process.env = { ...preEnv, CI: undefined }
const hook = require('../src/hooks/init')
expect(typeof hook).toBe('function')
inquirer.prompt = jest.fn().mockResolvedValue({ accept: true })
config.get = jest.fn().mockReturnValue(undefined)
await hook({ id: 'readme', config: { name: 'name', version: '0.0.1' }, argv: [] })
expect(inquirer.prompt).not.toHaveBeenCalled()
expect(fetch).not.toHaveBeenCalled()
process.env = preEnv
})

test('no prompt when process.env.CI', async () => {
const preEnv = process.env
process.env = { ...preEnv, CI: 'true' }
const hook = require('../src/hooks/init')
expect(typeof hook).toBe('function')
inquirer.prompt = jest.fn().mockResolvedValue({ accept: false })
config.get = jest.fn().mockReturnValue(undefined)
expect(inquirer.prompt).not.toHaveBeenCalled()
await hook({ config: { name: 'name', version: '0.0.1' }, argv: ['--verbose'] })
expect(fetch).not.toHaveBeenCalled()
expect(inquirer.prompt).not.toHaveBeenCalled()
process.env = preEnv
})

/**
* Should prompt when config.get(optOut) returns undefined
* should still post after prompt even though it is declined, this is the last post
*/
test('init prompt accept:false', async () => {
const preEnv = process.env
process.env = { ...preEnv, CI: undefined }
const hook = require('../src/hooks/init')
expect(typeof hook).toBe('function')
inquirer.prompt = jest.fn().mockResolvedValue({ accept: false })
Expand All @@ -80,6 +112,7 @@ describe('hook interfaces', () => {
expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith(expect.any(String),
expect.objectContaining({ body: expect.stringContaining('"_adobeio":{"eventType":"telemetry-prompt","eventData":"declined"') }))
process.env = preEnv
})

test('telemetry', async () => {
Expand Down