From 3607eb34c52327cc6ee2f7cee2a1d86630ec7aa1 Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Tue, 26 Mar 2024 20:50:02 +0100 Subject: [PATCH] test: refactor to introduce helper to determine port (#5137) * refactor: mark less recommended path as deprecated * chore: introduce test helper to isolate port selection * refactor: switch to using helper to determine port We need to know about the port ahead of time so that we can use it in our configuration. This supports iterating on fixed ports to random ports so we can support running in parallel and ensure no collision between our tests. * chore: apply formatting --- test/jest/acceptance/analytics.spec.ts | 3 ++- test/jest/acceptance/auth.spec.ts | 3 ++- test/jest/acceptance/cli-args.spec.ts | 4 +++- test/jest/acceptance/cli-json-file-output.spec.ts | 3 ++- test/jest/acceptance/cli-json-output.spec.ts | 3 ++- test/jest/acceptance/cli-token-precedence.spec.ts | 3 ++- test/jest/acceptance/https.spec.ts | 3 ++- test/jest/acceptance/oauth-token.spec.ts | 3 ++- test/jest/acceptance/print-graph.spec.ts | 3 ++- test/jest/acceptance/proxy-behavior.spec.ts | 3 ++- test/jest/acceptance/snyk-apps/config.spec.ts | 3 ++- test/jest/acceptance/snyk-apps/create-app.spec.ts | 3 ++- test/jest/acceptance/snyk-config/snyk-config.spec.ts | 3 ++- test/jest/acceptance/snyk-fix/fix.spec.ts | 3 ++- test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts | 3 ++- test/jest/acceptance/snyk-monitor/target-reference.spec.ts | 3 ++- .../jest/acceptance/snyk-test-all-projects-exit-codes.spec.ts | 3 ++- test/jest/acceptance/snyk-test/all-projects.spec.ts | 3 ++- .../acceptance/snyk-test/app-vuln-container-project.spec.ts | 3 ++- .../acceptance/snyk-test/basic-test-all-languages.spec.ts | 3 ++- test/jest/acceptance/snyk-test/fail-on.spec.ts | 3 ++- test/jest/acceptance/snyk-test/missing-node-modules.spec.ts | 3 ++- test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts | 3 ++- .../acceptance/snyk-test/protect-upgrade-notification.spec.ts | 3 ++- test/jest/acceptance/snyk-test/retry-mechanism.spec.ts | 3 ++- .../acceptance/snyk-test/snyk-test-local-policy-file.spec.ts | 3 ++- .../acceptance/snyk-test/spotlight-vuln-notification.spec.ts | 3 ++- test/jest/acceptance/snyk-test/trust-policies.spec.ts | 3 ++- .../acceptance/snyk-test/with-patched-vulnerabilities.spec.ts | 3 ++- test/jest/acceptance/snyk-test/yarn-workspaces.spec.ts | 3 ++- test/jest/util/createProject.ts | 2 ++ test/jest/util/getServerPort.ts | 3 +++ test/tap/auth.test.ts | 3 ++- test/tap/cli.test.ts | 3 ++- test/tap/remote-package.test.ts | 3 ++- 35 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 test/jest/util/getServerPort.ts diff --git a/test/jest/acceptance/analytics.spec.ts b/test/jest/acceptance/analytics.spec.ts index bea458da2a..95fd81e111 100644 --- a/test/jest/acceptance/analytics.spec.ts +++ b/test/jest/acceptance/analytics.spec.ts @@ -4,13 +4,14 @@ import { createProjectFromWorkspace, } from '../util/createProject'; import { runSnykCLI } from '../util/runSnykCLI'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 30); describe('analytics module', () => { let server; let env: Record; - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); beforeAll((done) => { const baseApi = '/api/v1'; diff --git a/test/jest/acceptance/auth.spec.ts b/test/jest/acceptance/auth.spec.ts index 1d0c15e91d..8da618b6ef 100644 --- a/test/jest/acceptance/auth.spec.ts +++ b/test/jest/acceptance/auth.spec.ts @@ -2,6 +2,7 @@ import { fakeServer, getFirstIPv4Address } from '../../acceptance/fake-server'; import { runSnykCLI } from '../util/runSnykCLI'; import { getCliConfig, restoreCliConfig } from '../../acceptance/config-helper'; import { ciEnvs } from '../../../src/lib/is-ci'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -13,7 +14,7 @@ describe('Auth', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://' + getFirstIPv4Address() + ':' + apiPort + apiPath, diff --git a/test/jest/acceptance/cli-args.spec.ts b/test/jest/acceptance/cli-args.spec.ts index b4fa598b88..b50ced9383 100644 --- a/test/jest/acceptance/cli-args.spec.ts +++ b/test/jest/acceptance/cli-args.spec.ts @@ -3,6 +3,8 @@ import { UnsupportedOptionCombinationError } from '../../../src/lib/errors/unsup import { runSnykCLI } from '../util/runSnykCLI'; import { fakeServer } from '../../acceptance/fake-server'; import { createProject } from '../util/createProject'; +import { getServerPort } from '../util/getServerPort'; + import * as os from 'os'; const isWindows = os.platform().indexOf('win') === 0; @@ -14,7 +16,7 @@ describe('cli args', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/cli-json-file-output.spec.ts b/test/jest/acceptance/cli-json-file-output.spec.ts index 922c33efac..28f5138920 100644 --- a/test/jest/acceptance/cli-json-file-output.spec.ts +++ b/test/jest/acceptance/cli-json-file-output.spec.ts @@ -3,6 +3,7 @@ import * as fs from 'fs'; import { createProjectFromWorkspace } from '../util/createProject'; import { runSnykCLI } from '../util/runSnykCLI'; import { humanFileSize } from '../../utils'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -12,7 +13,7 @@ describe('test --json-file-output', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/cli-json-output.spec.ts b/test/jest/acceptance/cli-json-output.spec.ts index 7990d49033..d1f4d6de37 100644 --- a/test/jest/acceptance/cli-json-output.spec.ts +++ b/test/jest/acceptance/cli-json-output.spec.ts @@ -1,5 +1,6 @@ import { fakeServer } from '../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../util/createProject'; +import { getServerPort } from '../util/getServerPort'; import { runSnykCLI } from '../util/runSnykCLI'; import * as Parser from 'jsonparse'; @@ -11,7 +12,7 @@ describe('test --json', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/cli-token-precedence.spec.ts b/test/jest/acceptance/cli-token-precedence.spec.ts index 240d29636a..6731c4fc92 100644 --- a/test/jest/acceptance/cli-token-precedence.spec.ts +++ b/test/jest/acceptance/cli-token-precedence.spec.ts @@ -1,5 +1,6 @@ import { runSnykCLI } from '../util/runSnykCLI'; import { fakeServer } from '../../acceptance/fake-server'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 30); // 30 seconds @@ -8,7 +9,7 @@ describe('cli token precedence', () => { let env: Record; let initialConfig: Record = {}; - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; const initialEnvVars = { ...process.env, diff --git a/test/jest/acceptance/https.spec.ts b/test/jest/acceptance/https.spec.ts index ba70890692..a69737e07e 100644 --- a/test/jest/acceptance/https.spec.ts +++ b/test/jest/acceptance/https.spec.ts @@ -7,6 +7,7 @@ import { import { createProjectFromWorkspace } from '../util/createProject'; import { getFixturePath } from '../util/getFixturePath'; import { runSnykCLI } from '../util/runSnykCLI'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 30); @@ -18,7 +19,7 @@ describe('https', () => { const ipaddress = getFirstIPv4Address(); console.log('Using ip: ' + ipaddress); - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/oauth-token.spec.ts b/test/jest/acceptance/oauth-token.spec.ts index 1036a0a403..46a0a96255 100644 --- a/test/jest/acceptance/oauth-token.spec.ts +++ b/test/jest/acceptance/oauth-token.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../util/createProject'; import { runSnykCLI } from '../util/runSnykCLI'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -10,7 +11,7 @@ describe('OAuth Token', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/print-graph.spec.ts b/test/jest/acceptance/print-graph.spec.ts index 87c0f0d02e..f539259f70 100644 --- a/test/jest/acceptance/print-graph.spec.ts +++ b/test/jest/acceptance/print-graph.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../acceptance/fake-server'; import { createProjectFromFixture } from '../util/createProject'; import { runSnykCLI } from '../util/runSnykCLI'; +import { getServerPort } from '../util/getServerPort'; import * as path from 'path'; jest.setTimeout(1000 * 30); @@ -10,7 +11,7 @@ describe('`test` command with `--print-graph` option', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/proxy-behavior.spec.ts b/test/jest/acceptance/proxy-behavior.spec.ts index 11f5d9ec13..6dae219170 100644 --- a/test/jest/acceptance/proxy-behavior.spec.ts +++ b/test/jest/acceptance/proxy-behavior.spec.ts @@ -10,6 +10,7 @@ import { TestCLI, startSnykCLI, } from '../../../test/jest/util/startSnykCLI'; +import { getServerPort } from '../util/getServerPort'; import { unlink } from 'fs'; import { execSync } from 'child_process'; import * as os from 'os'; @@ -48,7 +49,7 @@ const containerName = 'proxy_authentication_container'; const hostnameFakeServer = 'host.docker.internal'; const hostnameProxy = 'proxy.snyk.local'; const proxyPort = '3128'; -const port = process.env.PORT || process.env.SNYK_PORT || '12345'; +const port = getServerPort(process); const baseApi = '/api/v1'; const SNYK_API = 'http://' + hostnameFakeServer + ':' + port + baseApi; const HTTP_PROXY_WITH_USER = 'http://patch:dogsrule@localhost:' + proxyPort; diff --git a/test/jest/acceptance/snyk-apps/config.spec.ts b/test/jest/acceptance/snyk-apps/config.spec.ts index 2705efe46d..789118bc55 100644 --- a/test/jest/acceptance/snyk-apps/config.spec.ts +++ b/test/jest/acceptance/snyk-apps/config.spec.ts @@ -1,11 +1,12 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 30); describe('config', () => { let server: ReturnType; - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseURL = '/realbase'; const orgId = '4e0828f9-d92a-4f54-b005-6b9d8150b75f'; const testData = { diff --git a/test/jest/acceptance/snyk-apps/create-app.spec.ts b/test/jest/acceptance/snyk-apps/create-app.spec.ts index d9fc90d76f..e157682929 100644 --- a/test/jest/acceptance/snyk-apps/create-app.spec.ts +++ b/test/jest/acceptance/snyk-apps/create-app.spec.ts @@ -1,5 +1,6 @@ import { fakeServer, FakeServer } from '../../../acceptance/fake-server'; import { startSnykCLI, TestCLI } from '../../util/startSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; describe('snyk-apps: create app', () => { let server: FakeServer; @@ -7,7 +8,7 @@ describe('snyk-apps: create app', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/rest'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-config/snyk-config.spec.ts b/test/jest/acceptance/snyk-config/snyk-config.spec.ts index ef0afb8dd5..33855f932b 100644 --- a/test/jest/acceptance/snyk-config/snyk-config.spec.ts +++ b/test/jest/acceptance/snyk-config/snyk-config.spec.ts @@ -1,6 +1,7 @@ import { runSnykCLI } from '../../util/runSnykCLI'; import { FakeServer, fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../../util/createProject'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -19,7 +20,7 @@ test('returns value in one line', async () => { describe('snyk config set endpoint', () => { let server: FakeServer; - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api'; const token = '123456789'; diff --git a/test/jest/acceptance/snyk-fix/fix.spec.ts b/test/jest/acceptance/snyk-fix/fix.spec.ts index 8b67adb363..cabc3898f8 100644 --- a/test/jest/acceptance/snyk-fix/fix.spec.ts +++ b/test/jest/acceptance/snyk-fix/fix.spec.ts @@ -7,6 +7,7 @@ import { } from '../../util/createProject'; import { runCommand } from '../../util/runCommand'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; // Check for existence of pipenv in the environment const hasPipEnv = spawnSync('pipenv', ['--version']).status === 0; @@ -18,7 +19,7 @@ describe('snyk fix', () => { beforeAll(async () => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts b/test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts index f156838ce5..3a1707fb1e 100644 --- a/test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts +++ b/test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts @@ -3,6 +3,7 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../../util/createProject'; import { requireSnykToken } from '../../util/requireSnykToken'; import { runSnykCLI, runSnykCLIWithArray } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -12,7 +13,7 @@ describe('snyk ignore', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/snyk-monitor/target-reference.spec.ts b/test/jest/acceptance/snyk-monitor/target-reference.spec.ts index 176de443bd..8036c1344a 100644 --- a/test/jest/acceptance/snyk-monitor/target-reference.spec.ts +++ b/test/jest/acceptance/snyk-monitor/target-reference.spec.ts @@ -1,6 +1,7 @@ import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../../util/createProject'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('--target-reference', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test-all-projects-exit-codes.spec.ts b/test/jest/acceptance/snyk-test-all-projects-exit-codes.spec.ts index b127017726..e00d01013d 100644 --- a/test/jest/acceptance/snyk-test-all-projects-exit-codes.spec.ts +++ b/test/jest/acceptance/snyk-test-all-projects-exit-codes.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../acceptance/fake-server'; import { createProject } from '../util/createProject'; import { runSnykCLI } from '../util/runSnykCLI'; +import { getServerPort } from '../util/getServerPort'; jest.setTimeout(1000 * 60 * 5); @@ -9,7 +10,7 @@ describe('snyk test --all-projects with one project that has errors', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/all-projects.spec.ts b/test/jest/acceptance/snyk-test/all-projects.spec.ts index cff1ab65c5..3e85f0219b 100644 --- a/test/jest/acceptance/snyk-test/all-projects.spec.ts +++ b/test/jest/acceptance/snyk-test/all-projects.spec.ts @@ -1,6 +1,7 @@ import { createProjectFromWorkspace } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('snyk test --all-projects (mocked server only)', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/app-vuln-container-project.spec.ts b/test/jest/acceptance/snyk-test/app-vuln-container-project.spec.ts index 7ad8121846..c56e846f21 100644 --- a/test/jest/acceptance/snyk-test/app-vuln-container-project.spec.ts +++ b/test/jest/acceptance/snyk-test/app-vuln-container-project.spec.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import { fakeServer } from '../../../acceptance/fake-server'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; describe('container test projects behavior with --app-vulns, --file and --exclude-base-image-vulns flags', () => { it('should find nothing when only vulns are in base image', async () => { @@ -109,7 +110,7 @@ describe('container test projects behavior with --json flag', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/basic-test-all-languages.spec.ts b/test/jest/acceptance/snyk-test/basic-test-all-languages.spec.ts index 411c22f1b3..8c207a4e87 100644 --- a/test/jest/acceptance/snyk-test/basic-test-all-languages.spec.ts +++ b/test/jest/acceptance/snyk-test/basic-test-all-languages.spec.ts @@ -3,6 +3,7 @@ import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; import { runCommand } from '../../util/runCommand'; import { isDontSkipTestsEnabled } from '../../util/isDontSkipTestsEnabled'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -22,7 +23,7 @@ describe('`snyk test` of basic projects for each language/ecosystem', () => { let dontSkip: boolean; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/fail-on.spec.ts b/test/jest/acceptance/snyk-test/fail-on.spec.ts index e09f5138b5..9e08a2155d 100644 --- a/test/jest/acceptance/snyk-test/fail-on.spec.ts +++ b/test/jest/acceptance/snyk-test/fail-on.spec.ts @@ -1,6 +1,7 @@ import { createProjectFromWorkspace } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -10,7 +11,7 @@ describe('snyk test --fail-on', () => { beforeAll((done) => { const apiPath = '/api/v1'; - const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; + const apiPort = getServerPort(process); env = { ...process.env, SNYK_API: 'http://localhost:' + apiPort + apiPath, diff --git a/test/jest/acceptance/snyk-test/missing-node-modules.spec.ts b/test/jest/acceptance/snyk-test/missing-node-modules.spec.ts index 4996890d1f..25fec86731 100644 --- a/test/jest/acceptance/snyk-test/missing-node-modules.spec.ts +++ b/test/jest/acceptance/snyk-test/missing-node-modules.spec.ts @@ -3,6 +3,7 @@ import { getWorkspaceJSON } from '../../../acceptance/workspace-helper'; import { createProject } from '../../util/createProject'; import { requireSnykToken } from '../../util/requireSnykToken'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -17,7 +18,7 @@ describe('snyk test with missing node_modules', () => { 'no-vulns', 'vulns-result.json', ); - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const BASE_API = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts b/test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts index 0227d0725d..18f7db7e0d 100644 --- a/test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts +++ b/test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts @@ -1,6 +1,7 @@ import { createProjectFromFixture } from '../../../util/createProject'; import { runSnykCLI } from '../../../util/runSnykCLI'; import { fakeServer } from '../../../../acceptance/fake-server'; +import { getServerPort } from '../../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('snyk test --sarif', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/protect-upgrade-notification.spec.ts b/test/jest/acceptance/snyk-test/protect-upgrade-notification.spec.ts index bb84db1c51..c5f901b60e 100644 --- a/test/jest/acceptance/snyk-test/protect-upgrade-notification.spec.ts +++ b/test/jest/acceptance/snyk-test/protect-upgrade-notification.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromFixture } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 30); @@ -9,7 +10,7 @@ describe('analytics module', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/retry-mechanism.spec.ts b/test/jest/acceptance/snyk-test/retry-mechanism.spec.ts index 9e7e7d5618..54cc6e54e0 100644 --- a/test/jest/acceptance/snyk-test/retry-mechanism.spec.ts +++ b/test/jest/acceptance/snyk-test/retry-mechanism.spec.ts @@ -2,6 +2,7 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromWorkspace } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { RETRY_ATTEMPTS } from '../../../../src/lib/snyk-test/common'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(2000 * 60); @@ -10,7 +11,7 @@ describe('snyk test retry mechanism', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/snyk-test-local-policy-file.spec.ts b/test/jest/acceptance/snyk-test/snyk-test-local-policy-file.spec.ts index 189f51d722..b2e6081335 100644 --- a/test/jest/acceptance/snyk-test/snyk-test-local-policy-file.spec.ts +++ b/test/jest/acceptance/snyk-test/snyk-test-local-policy-file.spec.ts @@ -2,6 +2,7 @@ import * as path from 'path'; import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromFixture } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -10,7 +11,7 @@ describe('`snyk test` with `--file=`', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/spotlight-vuln-notification.spec.ts b/test/jest/acceptance/snyk-test/spotlight-vuln-notification.spec.ts index 4ef3f5d68b..6957edecc6 100644 --- a/test/jest/acceptance/snyk-test/spotlight-vuln-notification.spec.ts +++ b/test/jest/acceptance/snyk-test/spotlight-vuln-notification.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromFixture } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('spotlight vuln notification', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/trust-policies.spec.ts b/test/jest/acceptance/snyk-test/trust-policies.spec.ts index 4ae89592e2..39417d882b 100644 --- a/test/jest/acceptance/snyk-test/trust-policies.spec.ts +++ b/test/jest/acceptance/snyk-test/trust-policies.spec.ts @@ -1,6 +1,7 @@ import { createProject } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('trust policies', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/with-patched-vulnerabilities.spec.ts b/test/jest/acceptance/snyk-test/with-patched-vulnerabilities.spec.ts index 8c226c6173..816b109852 100644 --- a/test/jest/acceptance/snyk-test/with-patched-vulnerabilities.spec.ts +++ b/test/jest/acceptance/snyk-test/with-patched-vulnerabilities.spec.ts @@ -1,6 +1,7 @@ import { fakeServer } from '../../../acceptance/fake-server'; import { createProjectFromFixture } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 30); @@ -9,7 +10,7 @@ describe('snyk test with patched vulnerabilities', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/acceptance/snyk-test/yarn-workspaces.spec.ts b/test/jest/acceptance/snyk-test/yarn-workspaces.spec.ts index 4228edd93f..e01d03e2d1 100644 --- a/test/jest/acceptance/snyk-test/yarn-workspaces.spec.ts +++ b/test/jest/acceptance/snyk-test/yarn-workspaces.spec.ts @@ -1,6 +1,7 @@ import { createProjectFromWorkspace } from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; +import { getServerPort } from '../../util/getServerPort'; jest.setTimeout(1000 * 60); @@ -9,7 +10,7 @@ describe('snyk test --yarn-workspaces (mocked server only)', () => { let env: Record; beforeAll((done) => { - const port = process.env.PORT || process.env.SNYK_PORT || '12345'; + const port = getServerPort(process); const baseApi = '/api/v1'; env = { ...process.env, diff --git a/test/jest/util/createProject.ts b/test/jest/util/createProject.ts index f4960b9f1b..08f52ae197 100644 --- a/test/jest/util/createProject.ts +++ b/test/jest/util/createProject.ts @@ -49,6 +49,8 @@ const createProject = async ( /** * Workaround until we move all fixtures to ./test/fixtures + * + * @deprecated Use createProject instead. */ const createProjectFromWorkspace = async ( fixtureName: string, diff --git a/test/jest/util/getServerPort.ts b/test/jest/util/getServerPort.ts new file mode 100644 index 0000000000..53473b25df --- /dev/null +++ b/test/jest/util/getServerPort.ts @@ -0,0 +1,3 @@ +export const getServerPort = (process: NodeJS.Process): string => { + return process.env.PORT || process.env.SNYK_PORT || '12345'; +}; diff --git a/test/tap/auth.test.ts b/test/tap/auth.test.ts index 77c8d431a1..fb717f12df 100644 --- a/test/tap/auth.test.ts +++ b/test/tap/auth.test.ts @@ -4,8 +4,9 @@ import stripAnsi = require('strip-ansi'); import * as isAuthed from '../../src/cli/commands/auth/is-authed'; import * as errors from '../../src/lib/errors/legacy-errors'; import { fakeServer } from '../acceptance/fake-server'; +import { getServerPort } from '../jest/util/getServerPort'; -const port = process.env.PORT || process.env.SNYK_PORT || '12345'; +const port = getServerPort(process); const apiKey = '123456789'; const BASE_API = '/api/v1'; diff --git a/test/tap/cli.test.ts b/test/tap/cli.test.ts index 3b4b89b917..a99dd79d28 100644 --- a/test/tap/cli.test.ts +++ b/test/tap/cli.test.ts @@ -12,6 +12,7 @@ import stripAnsi = require('strip-ansi'); import * as os from 'os'; import * as isDocker from '../../src/lib/is-docker'; import { fakeServer } from '../acceptance/fake-server'; +import { getServerPort } from '../jest/util/getServerPort'; type Ignore = { [path: string]: { @@ -25,7 +26,7 @@ type Policy = { [id: string]: Ignore[]; }; -const port = process.env.PORT || process.env.SNYK_PORT || '12345'; +const port = getServerPort(process); const apiKey = '123456789'; let oldKey; diff --git a/test/tap/remote-package.test.ts b/test/tap/remote-package.test.ts index 3e5f5d0d4b..23650e6b3b 100644 --- a/test/tap/remote-package.test.ts +++ b/test/tap/remote-package.test.ts @@ -2,8 +2,9 @@ import { test } from 'tap'; import * as ciChecker from '../../src/lib/is-ci'; import * as sinon from 'sinon'; import { fakeServer } from '../acceptance/fake-server'; +import { getServerPort } from '../jest/util/getServerPort'; -const port = process.env.PORT || process.env.SNYK_PORT || '12345'; +const port = getServerPort(process); const apiKey = '123456789'; let oldkey;