Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow cypress.config.js to be a default config file #18221

Merged
merged 39 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
12381b0
feat: allow cypress.config.js to be a config file
elevatebart Sep 22, 2021
c1735ae
test: some tests fixes
elevatebart Sep 22, 2021
60b4d6c
make special error for default config file missing
elevatebart Sep 22, 2021
87fb7ba
refactor: use isTexttermial to simplify/optimize
elevatebart Sep 22, 2021
ee63d00
fix: in write pass the options to read
elevatebart Sep 23, 2021
b473ebd
refactor: consolidate settings unit tests
elevatebart Sep 23, 2021
29cc77d
tests: all settings unit test
elevatebart Sep 23, 2021
e6a9b99
refactor: adjust types
elevatebart Sep 23, 2021
6e21954
tests: types of fixtures
elevatebart Sep 23, 2021
8c0587c
test: fixture
elevatebart Sep 23, 2021
0c4a599
test: fix some project tests
elevatebart Sep 23, 2021
0fbf280
Merge branch 'develop' into feat/multiple-defaults
elevatebart Sep 23, 2021
69a2ce2
tests: unit scaffold
elevatebart Sep 23, 2021
9d65318
tests: screenshots
elevatebart Sep 23, 2021
dc16448
tests: unit socket & specs
elevatebart Sep 23, 2021
42a288c
fix: browser type
elevatebart Sep 23, 2021
72d423d
test: project unit getId
elevatebart Sep 23, 2021
9cad1b3
refactor: restore some of the changes
elevatebart Sep 23, 2021
948ac7b
test: settings unit tests
elevatebart Sep 23, 2021
5418afe
test: fix unit test for project
elevatebart Sep 23, 2021
b6d8020
test: integration cypress run mode
elevatebart Sep 23, 2021
c82ecf9
test: add e2e test for new defaults
elevatebart Sep 23, 2021
8807a2f
fix: changes to desktop-gui
elevatebart Sep 23, 2021
e6fbd53
fix: events fro open project
elevatebart Sep 23, 2021
01d104f
Merge branch 'develop' into feat/multiple-defaults
elevatebart Sep 24, 2021
3bd037f
fix: add language conflict error
elevatebart Sep 24, 2021
a666120
test: add test for Config file not found
elevatebart Sep 24, 2021
4fa31d3
tests: add project utils default resolve unit
elevatebart Sep 24, 2021
bad0cf2
convert settings to typescript
elevatebart Sep 24, 2021
3e7a99b
fix: types for server
elevatebart Sep 24, 2021
8966bc9
style: fix text of language conflict error
elevatebart Sep 24, 2021
8951411
Merge branch 'develop' into feat/multiple-defaults
elevatebart Sep 27, 2021
53e9012
fixed Jennifer's feedback on desktop-gui
elevatebart Sep 27, 2021
ad58e82
add extra tests for the new behavior
elevatebart Sep 27, 2021
0262279
fix: have a default config file when error before open
elevatebart Sep 27, 2021
ce02137
Merge branch 'develop' into feat/multiple-defaults
elevatebart Sep 27, 2021
8a8fb68
Update packages/desktop-gui/src/settings/node-version.jsx
elevatebart Sep 28, 2021
e81fa90
fix another Typo
elevatebart Sep 28, 2021
22cfcc9
fix copy to clipbard
elevatebart Sep 28, 2021
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: 2 additions & 2 deletions packages/desktop-gui/src/lib/config-file-formatted.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const configFileFormatted = (configFile) => {
return <><code>cypress.json</code> file (currently disabled by <code>--config-file false</code>)</>
}

if (isUndefined(configFile) || configFile === 'cypress.json') {
return <><code>cypress.json</code> file</>
if (isUndefined(configFile) || ['cypress.json', 'cypress.config.js'].includes(configFile)) {
return <><code>{configFile}</code> file</>
}

return <>custom config file <code>{configFile}</code></>
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scaffold from './scaffold'
import { fs } from './util/fs'
import keys from './util/keys'
import origin from './util/origin'
import settings from './util/settings'
import * as settings from './util/settings'
import Debug from 'debug'
import pathHelpers from './util/path_helpers'
import findSystemNode from './util/find_system_node'
Expand Down
2 changes: 2 additions & 0 deletions packages/server/lib/configFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// the first file is the default created file
export const CYPRESS_CONFIG_FILES = ['cypress.json', 'cypress.config.js']
13 changes: 13 additions & 0 deletions packages/server/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,19 @@ const getMsgByType = function (type, arg1 = {}, arg2, arg3) {
${chalk.yellow(arg1.error)}

Learn more at https://on.cypress.io/reporters`
// TODO: update with vetted cypress language
case 'NO_DEFAULT_CONFIG_FILE_FOUND':
return stripIndent`\
Could not find a Cypress configuration file, exiting.

We looked but did not find a default config file in this folder: ${chalk.blue(arg1)}`
// TODO: update with vetted cypress language
case 'CONFIG_FILES_LANGUAGE_CONFLICT':
return stripIndent`
There is both a \`${arg2}\` and a \`${arg3}\` at the location below:
${arg1}
Cypress does not know which one to read for config. Please remove one of the two and try again.
elevatebart marked this conversation as resolved.
Show resolved Hide resolved
`
case 'CONFIG_FILE_NOT_FOUND':
return stripIndent`\
Could not find a Cypress configuration file, exiting.
Expand Down
15 changes: 15 additions & 0 deletions packages/server/lib/gui/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-case-declarations */
const _ = require('lodash')
const path = require('path')
const ipc = require('electron').ipcMain
const { clipboard } = require('electron')
const debug = require('debug')('cypress:server:events')
Expand Down Expand Up @@ -323,6 +324,20 @@ const handleEvent = function (options, bus, event, id, type, arg) {
onWarning,
})
}).call('getConfig')
.then((config) => {
if (config.configFile && path.isAbsolute(config.configFile)) {
config.configFile = path.relative(arg, config.configFile)
}

// those two values make no sense to display in
// the GUI
if (config.resolved) {
config.resolved.configFile = undefined
config.resolved.testingType = undefined
}

return config
})
.then(send)
.catch(sendErr)

Expand Down
24 changes: 11 additions & 13 deletions packages/server/lib/modes/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const logSymbols = require('log-symbols')

const recordMode = require('./record')
const errors = require('../errors')
const ProjectStatic = require('../project_static')
const Reporter = require('../reporter')
const browserUtils = require('../browsers')
const { openProject } = require('../open_project')
Expand Down Expand Up @@ -609,22 +608,21 @@ const openProjectCreate = (projectRoot, socketId, args) => {
return openProject.create(projectRoot, args, options)
}

const createAndOpenProject = function (socketId, options) {
const createAndOpenProject = async function (socketId, options) {
const { projectRoot, projectId } = options

return ProjectStatic.ensureExists(projectRoot, options)
.then(() => {
// open this project without
// adding it to the global cache
return openProjectCreate(projectRoot, socketId, options)
})
.call('getProject')
return openProjectCreate(projectRoot, socketId, options)
.then((open_project) => open_project.getProject())
.then((project) => {
return Promise.props({
return Promise.all([
project,
config: project.getConfig(),
projectId: getProjectId(project, projectId),
})
project.getConfig(),
getProjectId(project, projectId),
]).then(([project, config, projectId]) => ({
project,
config,
projectId,
}))
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/open_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface LaunchArgs {
_: [string] // Cypress App binary location
config: Record<string, unknown>
cwd: string
browser: Browser
browser?: Browser['name']
configFile?: string
project: string // projectRoot
projectRoot: string // same as above
Expand Down Expand Up @@ -456,7 +456,7 @@ export class OpenProject {
try {
await this.openProject.initializeConfig()
await this.openProject.open()
} catch (err) {
} catch (err: any) {
if (err.isCypressErr && err.portInUse) {
errors.throw(err.type, err.port)
} else {
Expand Down
10 changes: 8 additions & 2 deletions packages/server/lib/project-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import system from './util/system'
import user from './user'
import { ensureProp } from './util/class-helpers'
import { fs } from './util/fs'
import settings from './util/settings'
import * as settings from './util/settings'
import plugins from './plugins'
import specsUtil from './util/specs'
import Watchers from './watchers'
import devServer from './plugins/dev-server'
import preprocessor from './plugins/preprocessor'
import { SpecsStore } from './specs-store'
import { checkSupportFile } from './project_utils'
import { checkSupportFile, getDefaultConfigFilePath } from './project_utils'
import type { LaunchArgs } from './open_project'

// Cannot just use RuntimeConfigOptions as is because some types are not complete.
Expand Down Expand Up @@ -690,6 +690,12 @@ export class ProjectBase<TServer extends ServerE2E | ServerCt> extends EE {
}

async initializeConfig (): Promise<Cfg> {
// set default for "configFile" if undefined
if (this.options.configFile === undefined
|| this.options.configFile === null) {
this.options.configFile = await getDefaultConfigFilePath(this.projectRoot, !this.options.isTextTerminal)
}

let theCfg: Cfg = await config.get(this.projectRoot, this.options)

if (theCfg.browsers) {
Expand Down
14 changes: 6 additions & 8 deletions packages/server/lib/project_static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import api from './api'
import cache from './cache'
import user from './user'
import keys from './util/keys'
import settings from './util/settings'
import * as settings from './util/settings'
import { ProjectBase } from './project-base'
import { getDefaultConfigFilePath } from './project_utils'

const debug = Debug('cypress:server:project_static')

Expand Down Expand Up @@ -60,7 +61,7 @@ export async function _getProject (clientProject, authToken) {
debug('got project from api')

return _mergeDetails(clientProject, project)
} catch (err) {
} catch (err: any) {
debug('failed to get project from api', err.statusCode)
switch (err.statusCode) {
case 404:
Expand Down Expand Up @@ -153,13 +154,10 @@ export async function add (path, options) {
}
}

export function getId (path) {
return new ProjectBase({ projectRoot: path, testingType: 'e2e', options: {} }).getProjectId()
}
export async function getId (path) {
const configFile = await getDefaultConfigFilePath(path)

export function ensureExists (path, options) {
// is there a configFile? is the root writable?
return settings.exists(path, options)
return new ProjectBase({ projectRoot: path, testingType: 'e2e', options: { configFile } }).getProjectId()
}

export async function writeProjectId (id: string, projectRoot: string) {
Expand Down
26 changes: 25 additions & 1 deletion packages/server/lib/project_utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Debug from 'debug'
import path from 'path'

import settings from './util/settings'
import * as settings from './util/settings'
import errors from './errors'
import { fs } from './util/fs'
import { escapeFilenameInUrl } from './util/escape_filename'
import { CYPRESS_CONFIG_FILES } from './configFiles'

const debug = Debug('cypress:server:project_utils')

Expand Down Expand Up @@ -131,3 +132,26 @@ export const checkSupportFile = async ({

return
}

export async function getDefaultConfigFilePath (projectRoot: string, returnDefaultValueIfNotFound: boolean = true): Promise<string | undefined> {
const filesInProjectDir = await fs.readdir(projectRoot)

const foundConfigFiles = CYPRESS_CONFIG_FILES.filter((file) => filesInProjectDir.includes(file))

// if we only found one default file, it is the one
if (foundConfigFiles.length === 1) {
return foundConfigFiles[0]
}

// if we found more than one, throw a language conflict
if (foundConfigFiles.length > 1) {
throw errors.throw('CONFIG_FILES_LANGUAGE_CONFLICT', projectRoot, ...foundConfigFiles)
}

if (returnDefaultValueIfNotFound) {
// Default is to create a new `cypress.json` file if one does not exist.
return CYPRESS_CONFIG_FILES[0]
}

throw errors.get('NO_DEFAULT_CONFIG_FILE_FOUND', projectRoot)
}
4 changes: 0 additions & 4 deletions packages/server/lib/util/require_async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ let requireProcess: cp.ChildProcess | null
interface RequireAsyncOptions{
projectRoot: string
loadErrorCode: string
/**
* members of the object returned that are functions and will need to be wrapped
*/
functionNames: string[]
}

interface ChildOptions{
Expand Down
Loading