-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(create-gatsby): Respect telemetry disable
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 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,61 @@ | ||
let isTrackingEnabled: () => boolean | ||
|
||
const get = jest.fn() | ||
const set = jest.fn() | ||
|
||
jest.doMock(`../get-config-store`, () => { | ||
return { | ||
getConfigStore: (): unknown => { | ||
return { | ||
get, | ||
set, | ||
} | ||
}, | ||
} | ||
}) | ||
|
||
describe(`isTrackingEnabled`, () => { | ||
beforeEach(() => { | ||
jest.resetModules() | ||
isTrackingEnabled = require(`../tracking`).isTrackingEnabled | ||
}) | ||
|
||
it(`is enabled by default`, async () => { | ||
const enabled = isTrackingEnabled() | ||
expect(enabled).toBeTrue() | ||
}) | ||
|
||
it(`respects the setting of the config store`, async () => { | ||
get.mockImplementationOnce(key => { | ||
if (key === `telemetry.enabled`) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
}) | ||
|
||
const enabled = isTrackingEnabled() | ||
expect(enabled).toBeFalse() | ||
|
||
const cachedEnabled = isTrackingEnabled() | ||
expect(cachedEnabled).toBeFalse() | ||
}) | ||
|
||
describe(`isTrackingEnabled / process.env.GATSBY_TELEMETRY_DISABLED`, () => { | ||
beforeAll(() => { | ||
process.env.GATSBY_TELEMETRY_DISABLED = `true` | ||
}) | ||
|
||
it(`respects the setting of the environment variable`, async () => { | ||
const enabled = isTrackingEnabled() | ||
expect(enabled).toBeFalse() | ||
|
||
const cachedEnabled = isTrackingEnabled() | ||
expect(cachedEnabled).toBeFalse() | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.GATSBY_TELEMETRY_DISABLED = undefined | ||
}) | ||
}) | ||
}) |
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,23 @@ | ||
// Copied from gatsby-core-utils to avoid depending on it, similar to get-config-store | ||
// | ||
// Returns true for `true`, true, positive numbers | ||
// Returns false for `false`, false, 0, negative integers and anything else | ||
export function isTruthy(value: any): boolean { | ||
// Return if Boolean | ||
if (typeof value === `boolean`) return value | ||
|
||
// Return false if null or undefined | ||
if (value === undefined || value === null) return false | ||
|
||
// If the String is true or false | ||
if (value.toLowerCase() === `true`) return true | ||
if (value.toLowerCase() === `false`) return false | ||
|
||
// Now check if it's a number | ||
const number = parseInt(value, 10) | ||
if (isNaN(number)) return false | ||
if (number > 0) return true | ||
|
||
// Default to false | ||
return false | ||
} |
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