Skip to content

Commit

Permalink
chore: internal refactor of privileged commands (#27060)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Jun 16, 2023
1 parent 9cb7e1f commit 89f0fb6
Show file tree
Hide file tree
Showing 56 changed files with 1,560 additions and 384 deletions.
3 changes: 1 addition & 2 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ windowsWorkflowFilters: &windows-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'matth/feat/chrome-headless', << pipeline.git.branch >> ]
- equal: [ 'lmiller/fix-windows-regressions', << pipeline.git.branch >> ]
- equal: [ 'privileged-commands-refactor', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand Down
4 changes: 2 additions & 2 deletions cli/types/cypress-eventemitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ type EventEmitter2 = import("eventemitter2").EventEmitter2

interface CyEventEmitter extends Omit<EventEmitter2, 'waitFor'> {
proxyTo: (cy: Cypress.cy) => null
emitMap: (eventName: string, args: any[]) => Array<(...args: any[]) => any>
emitThen: (eventName: string, args: any[]) => Bluebird.BluebirdStatic
emitMap: (eventName: string, ...args: any[]) => Array<(...args: any[]) => any>
emitThen: (eventName: string, ...args: any[]) => Bluebird.BluebirdStatic
}

// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/events.d.ts
Expand Down
3 changes: 2 additions & 1 deletion packages/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cypress/videos/*
cypress/screenshots/*
cypress/downloads/*

components.d.ts
components.d.ts
12 changes: 0 additions & 12 deletions packages/app/cypress/e2e/runner/reporter-ct-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,6 @@ export const generateCtErrorTests = (server: 'Webpack' | 'Vite', configFile: str
})
})

it('cy.readFile', () => {
const verify = loadErrorSpec({
filePath: 'errors/readfile.cy.js',
failCount: 1,
}, configFile)

verify('existence failure', {
column: [8, 9],
message: 'failed because the file does not exist',
})
})

it('validation errors', () => {
const verify = loadErrorSpec({
filePath: 'errors/validation.cy.js',
Expand Down
12 changes: 0 additions & 12 deletions packages/app/cypress/e2e/runner/reporter.command_errors.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,6 @@ describe('errors ui', {
})
})

it('cy.readFile', () => {
const verify = loadErrorSpec({
filePath: 'errors/readfile.cy.js',
failCount: 1,
})

verify('existence failure', {
column: 8,
message: 'failed because the file does not exist',
})
})

it('validation errors', () => {
const verify = loadErrorSpec({
filePath: 'errors/validation.cy.js',
Expand Down
8 changes: 7 additions & 1 deletion packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,13 @@ export class EventManager {
* Return it's response.
*/
Cypress.primaryOriginCommunicator.on('backend:request', async ({ args }, { source, responseEvent }) => {
const response = await Cypress.backend(...args)
let response

try {
response = await Cypress.backend(...args)
} catch (error) {
response = { error }
}

Cypress.primaryOriginCommunicator.toSource(source, responseEvent, response)
})
Expand Down
31 changes: 26 additions & 5 deletions packages/app/src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,25 @@ function setupRunner () {
createIframeModel()
}

interface GetSpecUrlOptions {
browserFamily?: string
namespace: string
specSrc: string
}

/**
* Get the URL for the spec. This is the URL of the AUT IFrame.
* CT uses absolute URLs, and serves from the dev server.
* E2E uses relative, serving from our internal server's spec controller.
*/
function getSpecUrl (namespace: string, specSrc: string) {
return `/${namespace}/iframes/${specSrc}`
function getSpecUrl ({ browserFamily, namespace, specSrc }: GetSpecUrlOptions) {
let url = `/${namespace}/iframes/${specSrc}`

if (browserFamily) {
url += `?browserFamily=${browserFamily}`
}

return url
}

/**
Expand Down Expand Up @@ -202,13 +214,15 @@ export function addCrossOriginIframe (location) {
return
}

const config = getRunnerConfigFromWindow()

addIframe({
id,
// the cross origin iframe is added to the document body instead of the
// container since it needs to match the size of the top window for screenshots
$container: document.body,
className: 'spec-bridge-iframe',
src: `${location.origin}/${getRunnerConfigFromWindow().namespace}/spec-bridge-iframes`,
src: `${location.origin}/${config.namespace}/spec-bridge-iframes?browserFamily=${config.browser.family}`,
})
}

Expand All @@ -234,7 +248,10 @@ function runSpecCT (config, spec: SpecFile) {
const autIframe = getAutIframeModel()
const $autIframe: JQuery<HTMLIFrameElement> = autIframe.create().appendTo($container)

const specSrc = getSpecUrl(config.namespace, spec.absolute)
const specSrc = getSpecUrl({
namespace: config.namespace,
specSrc: spec.absolute,
})

autIframe._showInitialBlankPage()
$autIframe.prop('src', specSrc)
Expand Down Expand Up @@ -297,7 +314,11 @@ function runSpecE2E (config, spec: SpecFile) {
autIframe.visitBlankPage()

// create Spec IFrame
const specSrc = getSpecUrl(config.namespace, encodeURIComponent(spec.relative))
const specSrc = getSpecUrl({
browserFamily: config.browser.family,
namespace: config.namespace,
specSrc: encodeURIComponent(spec.relative),
})

// FIXME: BILL Determine where to call client with to force browser repaint
/**
Expand Down
1 change: 1 addition & 0 deletions packages/driver/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cypress/videos
cypress/screenshots
cypress/downloads
28 changes: 17 additions & 11 deletions packages/driver/cypress/e2e/commands/exec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ describe('src/cy/commands/exec', () => {
cy.stub(Cypress, 'backend').callThrough()
})

it('triggers \'exec\' with the right options', () => {
it('sends privileged exec to backend with the right options', () => {
Cypress.backend.resolves(okResponse)

cy.exec('ls').then(() => {
expect(Cypress.backend).to.be.calledWith('exec', {
cmd: 'ls',
timeout: 2500,
env: {},
expect(Cypress.backend).to.be.calledWith('run:privileged', {
commandName: 'exec',
userArgs: ['ls'],
options: {
cmd: 'ls',
timeout: 2500,
env: {},
},
})
})
})
Expand All @@ -28,17 +32,19 @@ describe('src/cy/commands/exec', () => {
Cypress.backend.resolves(okResponse)

cy.exec('ls', { env: { FOO: 'foo' } }).then(() => {
expect(Cypress.backend).to.be.calledWith('exec', {
cmd: 'ls',
timeout: 2500,
env: {
FOO: 'foo',
expect(Cypress.backend).to.be.calledWith('run:privileged', {
commandName: 'exec',
userArgs: ['ls', { env: { FOO: 'foo' } }],
options: {
cmd: 'ls',
timeout: 2500,
env: { FOO: 'foo' },
},
})
})
})

it('really works', () => {
it('works e2e', () => {
// output is trimmed
cy.exec('echo foo', { timeout: 20000 }).its('stdout').should('eq', 'foo')
})
Expand Down
114 changes: 77 additions & 37 deletions packages/driver/cypress/e2e/commands/files.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ describe('src/cy/commands/files', () => {
})

describe('#readFile', () => {
it('triggers \'read:file\' with the right options', () => {
it('sends privileged readFile to backend with the right options', () => {
Cypress.backend.resolves(okResponse)

cy.readFile('foo.json').then(() => {
expect(Cypress.backend).to.be.calledWith(
'read:file',
'foo.json',
{ encoding: 'utf8' },
'run:privileged',
{
commandName: 'readFile',
userArgs: ['foo.json'],
options: {
file: 'foo.json',
encoding: 'utf8',
},
},
)
})
})
Expand All @@ -31,9 +37,15 @@ describe('src/cy/commands/files', () => {

cy.readFile('foo.json', 'ascii').then(() => {
expect(Cypress.backend).to.be.calledWith(
'read:file',
'foo.json',
{ encoding: 'ascii' },
'run:privileged',
{
commandName: 'readFile',
userArgs: ['foo.json', 'ascii'],
options: {
file: 'foo.json',
encoding: 'ascii',
},
},
)
})
})
Expand All @@ -47,9 +59,15 @@ describe('src/cy/commands/files', () => {

cy.readFile('foo.json', null).then(() => {
expect(Cypress.backend).to.be.calledWith(
'read:file',
'foo.json',
{ encoding: null },
'run:privileged',
{
commandName: 'readFile',
userArgs: ['foo.json', null],
options: {
file: 'foo.json',
encoding: null,
},
},
)
}).should('eql', Buffer.from('\n'))
})
Expand Down Expand Up @@ -426,17 +444,21 @@ describe('src/cy/commands/files', () => {
})

describe('#writeFile', () => {
it('triggers \'write:file\' with the right options', () => {
it('sends privileged writeFile to backend with the right options', () => {
Cypress.backend.resolves(okResponse)

cy.writeFile('foo.txt', 'contents').then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
'contents',
'run:privileged',
{
encoding: 'utf8',
flag: 'w',
commandName: 'writeFile',
userArgs: ['foo.txt', 'contents'],
options: {
fileName: 'foo.txt',
contents: 'contents',
encoding: 'utf8',
flag: 'w',
},
},
)
})
Expand All @@ -447,12 +469,16 @@ describe('src/cy/commands/files', () => {

cy.writeFile('foo.txt', 'contents', 'ascii').then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
'contents',
'run:privileged',
{
encoding: 'ascii',
flag: 'w',
commandName: 'writeFile',
userArgs: ['foo.txt', 'contents', 'ascii'],
options: {
fileName: 'foo.txt',
contents: 'contents',
encoding: 'ascii',
flag: 'w',
},
},
)
})
Expand All @@ -462,14 +488,20 @@ describe('src/cy/commands/files', () => {
it('explicit null encoding is sent to server as Buffer', () => {
Cypress.backend.resolves(okResponse)

cy.writeFile('foo.txt', Buffer.from([0, 0, 54, 255]), null).then(() => {
const buffer = Buffer.from([0, 0, 54, 255])

cy.writeFile('foo.txt', buffer, null).then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
Buffer.from([0, 0, 54, 255]),
'run:privileged',
{
encoding: null,
flag: 'w',
commandName: 'writeFile',
userArgs: ['foo.txt', buffer, null],
options: {
fileName: 'foo.txt',
contents: buffer,
encoding: null,
flag: 'w',
},
},
)
})
Expand All @@ -480,12 +512,16 @@ describe('src/cy/commands/files', () => {

cy.writeFile('foo.txt', 'contents', { encoding: 'ascii' }).then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
'contents',
'run:privileged',
{
encoding: 'ascii',
flag: 'w',
commandName: 'writeFile',
userArgs: ['foo.txt', 'contents', { encoding: 'ascii' }],
options: {
fileName: 'foo.txt',
contents: 'contents',
encoding: 'ascii',
flag: 'w',
},
},
)
})
Expand Down Expand Up @@ -531,12 +567,16 @@ describe('src/cy/commands/files', () => {

cy.writeFile('foo.txt', 'contents', { flag: 'a+' }).then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
'contents',
'run:privileged',
{
encoding: 'utf8',
flag: 'a+',
commandName: 'writeFile',
userArgs: ['foo.txt', 'contents', { flag: 'a+' }],
options: {
fileName: 'foo.txt',
contents: 'contents',
encoding: 'utf8',
flag: 'a+',
},
},
)
})
Expand Down
Loading

10 comments on commit 89f0fb6

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/linux-arm64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/linux-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/darwin-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/win32-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/darwin-arm64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/linux-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/darwin-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/linux-arm64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/win32-x64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 89f0fb6 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.15.0/darwin-arm64/develop-89f0fb64650d1795a912773b5c0660d1aceea0bc/cypress.tgz

Please sign in to comment.