diff --git a/packages/server/lib/environment.coffee b/packages/server/lib/environment.coffee index fd99167a0a..e8a2742a89 100644 --- a/packages/server/lib/environment.coffee +++ b/packages/server/lib/environment.coffee @@ -1,6 +1,11 @@ require("./util/fs") os = require("os") +## +## NOTE +## +## by loading "./cwd" we are changing the current working directory +## to the "packages/server" folder cwd = require("./cwd") Promise = require("bluebird") diff --git a/scripts/binary/get-config.js b/scripts/binary/get-config.js new file mode 100644 index 0000000000..56a431bad2 --- /dev/null +++ b/scripts/binary/get-config.js @@ -0,0 +1,22 @@ +/** + * Safer konfig load for test code. The original konfig changes the + * current working directory, thus the tests might be affected + * unexpectedly. This function loads the konfig, but then + * restores the current working directory. + * + * The tests should use this function to get `konfig` function like + * + * @example + * const konfig = require('../binary/get-config')() + */ +const getConfig = () => { + const cwd = process.cwd() + const konfig = require('../../packages/server/lib/konfig') + + // restore previous cwd in case it was changed by loading "konfig" + process.chdir(cwd) + + return konfig +} + +module.exports = getConfig diff --git a/scripts/binary/upload-npm-package.coffee b/scripts/binary/upload-npm-package.coffee index 081fd1ccaf..7be6116046 100644 --- a/scripts/binary/upload-npm-package.coffee +++ b/scripts/binary/upload-npm-package.coffee @@ -11,7 +11,7 @@ gulp = require("gulp") human = require("human-interval") R = require("ramda") -konfig = require("../../packages/server/lib/konfig") +konfig = require('../binary/get-config')() uploadUtils = require("./util/upload") npmPackageExtension = ".tgz" diff --git a/scripts/binary/upload-unique-binary.coffee b/scripts/binary/upload-unique-binary.coffee index c2ace9771d..f786d189c8 100644 --- a/scripts/binary/upload-unique-binary.coffee +++ b/scripts/binary/upload-unique-binary.coffee @@ -11,7 +11,7 @@ gulp = require("gulp") human = require("human-interval") R = require("ramda") -konfig = require("../../packages/server/lib/konfig") +konfig = require('../binary/get-config')() uploadUtils = require("./util/upload") # we zip the binary on every platform and upload under same name diff --git a/scripts/binary/upload.coffee b/scripts/binary/upload.coffee index 7568ac3448..e2a1a5cbcd 100644 --- a/scripts/binary/upload.coffee +++ b/scripts/binary/upload.coffee @@ -6,7 +6,7 @@ cp = require("child_process") path = require("path") gulp = require("gulp") human = require("human-interval") -konfig = require("../../packages/server/lib/konfig") +konfig = require('../binary/get-config')() Promise = require("bluebird") meta = require("./meta") la = require("lazy-ass") diff --git a/scripts/binary/util/upload.coffee b/scripts/binary/util/upload.coffee index 8ce77d5f7b..7078e79ba6 100644 --- a/scripts/binary/util/upload.coffee +++ b/scripts/binary/util/upload.coffee @@ -9,7 +9,7 @@ fs = require("fs") os = require("os") Promise = require("bluebird") {configFromEnvOrJsonFile, filenameToShellVariable} = require('@cypress/env-or-json-file') -konfig = require("../../../packages/server/lib/konfig") +konfig = require('../../binary/get-config')() formHashFromEnvironment = () -> env = process.env diff --git a/scripts/unit/konfig-spec.js b/scripts/unit/konfig-spec.js new file mode 100644 index 0000000000..4a16074c0e --- /dev/null +++ b/scripts/unit/konfig-spec.js @@ -0,0 +1,45 @@ +const la = require('lazy-ass') +const is = require('check-more-types') +const { join } = require('path') + +/* eslint-env mocha */ +describe('konfig check', () => { + /* + script tests should NOT suddenly change the current working directory to + packages/server - otherwise the local path filenames might be all wrong + and unexpected. The current working directory changes when we + require `packages/server/lib/konfig` which in tern requires + `lib/cwd` which changes CWD. + + From the scripts unit tests we should not use `lib/konfig` directly, + instead we should use `binary/get-config` script to get the konfig function. + */ + + let cwd + + before(() => { + cwd = process.cwd() + la( + !cwd.includes(join('packages', 'server')), + 'process CWD is set to', + cwd, + 'for some reason' + ) + // if the above assertion breaks, it means some script in binary scripts + // loads "lib/konfig" directly, which unexpectedly changes the CWD. + }) + + it('does not change CWD on load', () => { + const konfig = require('../binary/get-config')() + const cwdAfter = process.cwd() + + la( + cwd === cwdAfter, + 'previous cwd', + cwd, + 'differs after loading konfig', + cwdAfter + ) + la(is.fn(konfig), 'expected konfig to be a function', konfig) + }) +})