Skip to content

Commit

Permalink
chore: add testingType, initialLaunch to update check (cypress-io#15699)
Browse files Browse the repository at this point in the history
* add testingType, initialLaunch to update check
  • Loading branch information
kuceb authored Apr 1, 2021
1 parent bf6368e commit f067110
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/desktop-gui/cypress/integration/footer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ describe('Footer', () => {

it('shows update indicator', () => {
cy.get('.update-indicator').should('be.visible')
cy.wrap(ipc.updaterCheck).should('be.calledOnceWith', {
initialLaunch: true,
testingType: 'e2e',
})

cy.percySnapshot()
})

Expand Down
9 changes: 8 additions & 1 deletion packages/desktop-gui/src/update/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import updateStore from '../update/update-store'
import ipc from '../lib/ipc'
import { useLifecycle } from '../lib/use-lifecycle'

let initialLaunch = true

const checkForUpdate = () => {
ipc.offUpdaterCheck()

ipc.updaterCheck()
ipc.updaterCheck({
initialLaunch,
testingType: 'e2e',
})
.then((version) => {
if (version) updateStore.setNewVersion(version)
})
.catch((error) => {
console.warn('Error checking for updates:', error) // eslint-disable-line no-console
})

initialLaunch = false
}

export const useUpdateChecker = () => {
Expand Down
28 changes: 25 additions & 3 deletions packages/server-ct/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
/* eslint-disable no-console */
import chalk from 'chalk'
import browsers from '@packages/server/lib/browsers'
import openProject from '@packages/server/lib/open_project'
import chalk from 'chalk'
import human from 'human-interval'
import _ from 'lodash'

export * from './src/socket-ct'
export * from './src/project-ct'

export * from './src/server-ct'

export * from './src/project-ct'
export * from './src/socket-ct'

export * from './src/specs-store'

const Updater = require('@packages/server/lib/updater')

const registerCheckForUpdates = () => {
const checkForUpdates = (initialLaunch) => {
Updater.check({
initialLaunch,
testingType: 'ct',
onNewVersion: _.noop,
onNoNewVersion: _.noop,
})
}

setInterval(() => checkForUpdates(false), human('60 minutes'))
checkForUpdates(true)
}

// Partial because there are probably other options that are not included in this type.
export const start = async (projectRoot: string, args: Record<string, any>) => {
if (process.env['CYPRESS_INTERNAL_ENV'] === 'production') {
registerCheckForUpdates()
}

// add chrome as a default browser if none has been specified
return browsers.ensureAndGetByNameOrPath(args.browser)
.then((browser: Cypress.Browser) => {
Expand Down
42 changes: 42 additions & 0 deletions packages/server-ct/test/unit/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Updater = require('@packages/server/lib/updater')
import openProject from '@packages/server/lib/open_project'
import browsers from '@packages/server/lib/browsers'
import sinon from 'sinon'
import { expect } from 'chai'
import * as Index from '../../index'

describe('index.spec', () => {
let backupEnv
let stub_setInterval

beforeEach(() => {
backupEnv = process.env
process.env.CYPRESS_INTERNAL_ENV = 'production'
stub_setInterval = sinon.spy(global, 'setInterval')
sinon.stub(Updater, 'check').resolves()
sinon.stub(openProject, 'create').resolves()
sinon.stub(openProject, 'launch').resolves()
sinon.stub(browsers, 'ensureAndGetByNameOrPath').resolves()
})

afterEach(() => {
if (backupEnv) {
process.env = backupEnv
backupEnv = null
}
})

it('registers update check', async () => {
await Index.start('/path/to/project', {
browser: 'chrome',
})

expect(stub_setInterval.callCount).eq(1)
expect(stub_setInterval.firstCall.args[1]).eq(1000 * 60 * 60)
expect(Updater.check.callCount).eq(1)
expect(Updater.check.firstCall.args[0]).includes({
testingType: 'ct',
initialLaunch: true,
})
})
})
1 change: 1 addition & 0 deletions packages/server/lib/gui/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ const handleEvent = function (options, bus, event, id, type, arg) {

case 'updater:check':
return Updater.check({
...arg,
onNewVersion ({ version }) {
return send(version)
},
Expand Down
15 changes: 11 additions & 4 deletions packages/server/lib/updater.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
const os = require('os')
const debug = require('debug')('cypress:server:updater')
const semver = require('semver')
const rp = require('@cypress/request-promise')
const pkg = require('@packages/root')
const { agent } = require('@packages/network')
const konfig = require('./konfig')
const { machineId } = require('./util/machine_id')

const _getManifest = (id) => {
const _getManifest = ({ id, initialLaunch, testingType }) => {
const url = konfig('desktop_manifest_url')

return rp.get({
url,
headers: {
'x-cypress-version': pkg.version,
'x-os-name': os.platform(),
'x-machine-id': id,
'x-initial-launch:': String(initialLaunch),
'x-testing-type': testingType,
},
agent,
proxy: null,
json: true,
})
}

const check = async ({ onNewVersion, onNoNewVersion } = {}) => {
const check = async ({ testingType, initialLaunch, onNewVersion, onNoNewVersion } = {}) => {
try {
const id = await machineId()
const manifest = await _getManifest(id)
const manifest = await _getManifest({
id,
testingType,
initialLaunch,
})

if (!manifest || !manifest.version) {
throw new Error('manifest is empty or does not have a version')
Expand Down
5 changes: 4 additions & 1 deletion packages/server/test/unit/updater_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ describe('lib/updater', () => {
it('sends the right headers', () => {
sinon.stub(rp, 'get').resolves({})

Updater._getManifest('machine-id')
Updater._getManifest({ testingType: 'type', initialLaunch: true, id: 'machine-id' })

expect(rp.get).to.be.calledWithMatch({
headers: {
'x-cypress-version': pkg.version,
'x-os-name': 'linux',
'x-machine-id': 'machine-id',
'x-initial-launch:': 'true',
'x-testing-type': 'type',
},
})
})
Expand Down

0 comments on commit f067110

Please sign in to comment.