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

[test optimization] Do not init on package managers #4946

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 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 Expand Up @@ -55,6 +71,8 @@ if (shouldInit) {
tracer.init(options)
tracer.use('fs', false)
tracer.use('child_process', false)
} else {
console.log('not init')
}

module.exports = tracer
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))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated fixes in this file: the webserver is useless

})

beforeEach(async function () {
Expand Down
107 changes: 107 additions & 0 deletions integration-tests/test-optimization-startup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'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')

describe('test optimization startup', () => {
let sandbox, cwd, receiver, childProcess

before(async () => {
sandbox = await createSandbox(['yarn', 'npm', 'pnpm'], true)
cwd = sandbox.folder
})

after(async () => {
await sandbox.remove()
})

beforeEach(async function () {
const port = await getPort()
receiver = await new FakeCiVisIntake(port).start()
})

afterEach(async () => {
childProcess.kill()
await receiver.stop()
})

it('skips initialization for yarn', (done) => {
let testOutput

childProcess = exec('node ./node_modules/.bin/yarn -v',
{
cwd,
env: {
...process.env,
NODE_OPTIONS: '-r dd-trace/ci/init',
DD_TRACE_DEBUG: '1'
},
stdio: 'pipe'
}
)

childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})

childProcess.on('exit', () => {
assert.include(testOutput, 'dd-trace is not initialized in a package manager')
done()
})
})

it('skips initialization for npm', (done) => {
let testOutput

childProcess = exec('node ./node_modules/.bin/npm -v',
{
cwd,
env: {
...process.env,
NODE_OPTIONS: '-r dd-trace/ci/init',
DD_TRACE_DEBUG: '1'
},
stdio: 'pipe'
}
)

childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})

childProcess.on('exit', () => {
assert.include(testOutput, 'dd-trace is not initialized in a package manager')
done()
})
})

it('skips initialization for pnpm', (done) => {
let testOutput

childProcess = exec('node ./node_modules/.bin/pnpm -v',
{
cwd,
env: {
...process.env,
NODE_OPTIONS: '-r dd-trace/ci/init',
DD_TRACE_DEBUG: '1'
},
stdio: 'pipe'
}
)

childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})

childProcess.on('exit', () => {
assert.include(testOutput, 'dd-trace is not initialized in a package manager')
done()
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a test that it doesn't skip in other situations?

})
})
Loading