diff --git a/ci/init.js b/ci/init.js index b54e29abd4d..7b15ed15151 100644 --- a/ci/init.js +++ b/ci/init.js @@ -1,11 +1,22 @@ /* eslint-disable no-console */ const tracer = require('../packages/dd-trace') const { isTrue } = require('../packages/dd-trace/src/util') +const log = require('../packages/dd-trace/src/log') const isJestWorker = !!process.env.JEST_WORKER_ID const isCucumberWorker = !!process.env.CUCUMBER_WORKER_ID const isMochaWorker = !!process.env.MOCHA_WORKER_ID +const packageManagers = [ + 'npm', + 'yarn', + 'pnpm' +] + +const isPackageManager = () => { + return packageManagers.some(packageManager => process.argv[1]?.includes(`bin/${packageManager}`)) +} + const options = { startupLogs: false, isCiVisibility: true, @@ -14,6 +25,11 @@ const options = { let shouldInit = true +if (isPackageManager()) { + log.debug('dd-trace is not initialized in a package manager.') + shouldInit = false +} + const isAgentlessEnabled = isTrue(process.env.DD_CIVISIBILITY_AGENTLESS_ENABLED) if (isAgentlessEnabled) { diff --git a/integration-tests/test-api-manual.spec.js b/integration-tests/test-api-manual.spec.js index 419c7c736c5..c403168206a 100644 --- a/integration-tests/test-api-manual.spec.js +++ b/integration-tests/test-api-manual.spec.js @@ -10,24 +10,20 @@ const { getCiVisAgentlessConfig } = require('./helpers') const { FakeCiVisIntake } = require('./ci-visibility-intake') -const webAppServer = require('./ci-visibility/web-app-server') const { TEST_STATUS } = require('../packages/dd-trace/src/plugins/util/test') describe('test-api-manual', () => { - let sandbox, cwd, receiver, childProcess, webAppPort + let sandbox, cwd, receiver, childProcess before(async () => { sandbox = await createSandbox([], true) cwd = sandbox.folder - webAppPort = await getPort() - webAppServer.listen(webAppPort) }) after(async () => { await sandbox.remove() - await new Promise(resolve => webAppServer.close(resolve)) }) beforeEach(async function () { diff --git a/integration-tests/test-optimization-startup.spec.js b/integration-tests/test-optimization-startup.spec.js new file mode 100644 index 00000000000..a15d49cf8ef --- /dev/null +++ b/integration-tests/test-optimization-startup.spec.js @@ -0,0 +1,84 @@ +'use strict' + +const { exec } = require('child_process') + +const getPort = require('get-port') +const { assert } = require('chai') + +const { createSandbox } = require('./helpers') +const { FakeCiVisIntake } = require('./ci-visibility-intake') + +const packageManagers = ['yarn', 'npm', 'pnpm'] + +describe('test optimization startup', () => { + let sandbox, cwd, receiver, childProcess, processOutput + + before(async () => { + sandbox = await createSandbox(packageManagers, true) + cwd = sandbox.folder + }) + + after(async () => { + await sandbox.remove() + }) + + beforeEach(async function () { + processOutput = '' + const port = await getPort() + receiver = await new FakeCiVisIntake(port).start() + }) + + afterEach(async () => { + childProcess.kill() + await receiver.stop() + }) + + packageManagers.forEach(packageManager => { + it(`skips initialization for ${packageManager}`, (done) => { + childProcess = exec(`node ./node_modules/.bin/${packageManager} -v`, + { + cwd, + env: { + ...process.env, + NODE_OPTIONS: '-r dd-trace/ci/init', + DD_TRACE_DEBUG: '1' + }, + stdio: 'pipe' + } + ) + + childProcess.stdout.on('data', (chunk) => { + processOutput += chunk.toString() + }) + + childProcess.on('exit', () => { + assert.include(processOutput, 'dd-trace is not initialized in a package manager') + done() + }) + }) + }) + + it('does not skip initialization for non package managers', (done) => { + childProcess = exec('node -e "console.log(\'hello!\')"', + { + cwd, + env: { + ...process.env, + NODE_OPTIONS: '-r dd-trace/ci/init', + DD_TRACE_DEBUG: '1' + }, + stdio: 'pipe' + } + ) + + childProcess.stdout.on('data', (chunk) => { + processOutput += chunk.toString() + }) + + childProcess.on('exit', () => { + assert.include(processOutput, 'hello!') + assert.notInclude(processOutput, 'dd-trace is not initialized in a package manager') + done() + }) + }) +})