Skip to content

Commit

Permalink
test(cli): override CI environment variable when testing functionalit…
Browse files Browse the repository at this point in the history
…y that depends on it
  • Loading branch information
juice49 committed Dec 17, 2023
1 parent ef0527c commit 929e2ac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/cli/src/util/createTelemetryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export type ConsentInformation =

export function resolveConsent({env}: Options): Promise<ConsentInformation> {
debug('Resolving consent…')
if (isCi) {
if (isTrueish(isCi)) {
debug('CI environment detected, treating telemetry consent as denied')
return Promise.resolve({status: 'denied'})
}
Expand Down
68 changes: 57 additions & 11 deletions packages/@sanity/cli/test/telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {describeCliTest, testConcurrent} from './shared/describe'
import {describeCliTest} from './shared/describe'
import {runSanityCmdCommand} from './shared/environment'

describeCliTest('CLI: `sanity telemetry status`', () => {
test('sanity telemetry status: granted', async () => {
await runSanityCmdCommand('v3', ['telemetry', 'enable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'status'])
await runSanityCmdCommand('v3', ['telemetry', 'enable'], {
env: {
CI: 'false',
},
})

const result = await runSanityCmdCommand('v3', ['telemetry', 'status'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Enabled
Expand All @@ -18,8 +27,17 @@ https://www.sanity.io/telemetry
})

test('sanity telemetry status: denied', async () => {
await runSanityCmdCommand('v3', ['telemetry', 'disable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'status'])
await runSanityCmdCommand('v3', ['telemetry', 'disable'], {
env: {
CI: 'false',
},
})

const result = await runSanityCmdCommand('v3', ['telemetry', 'status'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Disabled
Expand All @@ -36,6 +54,7 @@ https://www.sanity.io/telemetry
test('sanity telemetry status: denied using DO_NOT_TRACK', async () => {
const result = await runSanityCmdCommand('v3', ['telemetry', 'status'], {
env: {
CI: 'false',
DO_NOT_TRACK: '1',
},
})
Expand All @@ -57,8 +76,17 @@ https://www.sanity.io/telemetry

describeCliTest('CLI: `sanity telemetry enable`', () => {
test('sanity telemetry enable: success', async () => {
await runSanityCmdCommand('v3', ['telemetry', 'disable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'enable'])
await runSanityCmdCommand('v3', ['telemetry', 'disable'], {
env: {
CI: 'false',
},
})

const result = await runSanityCmdCommand('v3', ['telemetry', 'enable'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Enabled
Expand All @@ -72,7 +100,11 @@ https://www.sanity.io/telemetry
})

test('sanity telemetry enable: success (already enabled)', async () => {
const result = await runSanityCmdCommand('v3', ['telemetry', 'enable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'enable'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Enabled
Expand All @@ -88,8 +120,17 @@ https://www.sanity.io/telemetry

describeCliTest('CLI: `sanity telemetry disable`', () => {
test('sanity telemetry disable: success', async () => {
await runSanityCmdCommand('v3', ['telemetry', 'enable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'disable'])
await runSanityCmdCommand('v3', ['telemetry', 'enable'], {
env: {
CI: 'false',
},
})

const result = await runSanityCmdCommand('v3', ['telemetry', 'disable'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Disabled
Expand All @@ -104,7 +145,11 @@ https://www.sanity.io/telemetry
})

test('sanity telemetry disable: success (already denied)', async () => {
const result = await runSanityCmdCommand('v3', ['telemetry', 'disable'])
const result = await runSanityCmdCommand('v3', ['telemetry', 'disable'], {
env: {
CI: 'false',
},
})

expect(result.stdout).toMatchInlineSnapshot(`
"Status: Disabled
Expand All @@ -121,6 +166,7 @@ https://www.sanity.io/telemetry
test('sanity telemetry disable: success (already denied using DO_NOT_TRACK)', async () => {
const result = await runSanityCmdCommand('v3', ['telemetry', 'disable'], {
env: {
CI: 'false',
DO_NOT_TRACK: '1',
},
})
Expand Down

0 comments on commit 929e2ac

Please sign in to comment.