Skip to content

Commit

Permalink
[test optimization] Do not init on package managers (#4946)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored Nov 28, 2024
1 parent 63b6cf8 commit 2ad4cd0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
16 changes: 16 additions & 0 deletions ci/init.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions integration-tests/test-api-manual.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
84 changes: 84 additions & 0 deletions integration-tests/test-optimization-startup.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit 2ad4cd0

Please sign in to comment.