Skip to content

Commit fee12d3

Browse files
committed
fix regex and add missing test
1 parent 00647e1 commit fee12d3

File tree

2 files changed

+70
-41
lines changed

2 files changed

+70
-41
lines changed

integration-tests/init.spec.js

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const assert = require('assert')
34
const semver = require('semver')
45
const {
56
runAndCheckWithTelemetry: testFile,
@@ -99,70 +100,98 @@ function testRuntimeVersionChecks (arg, filename) {
99100
}
100101
}
101102

102-
if (!currentVersionIsSupported) {
103-
context('when node version is less than engines field', () => {
104-
useEnv({ NODE_OPTIONS })
103+
let pkgPath
104+
let pkgStr
105105

106-
it('should not initialize the tracer', () => doTest('false\n', []))
106+
before(() => {
107+
pkgPath = `${sandboxCwd()}/node_modules/dd-trace/package.json`
108+
pkgStr = fs.readFileSync(pkgPath, 'utf8')
109+
})
107110

108-
context('with DD_INJECTION_ENABLED', () => {
109-
useEnv({ DD_INJECTION_ENABLED })
111+
after(() => {
112+
fs.writeFileSync(pkgPath, pkgStr)
113+
})
110114

111-
context('without debug', () => {
112-
it('should not initialize the tracer', () => doTest('false\n', telemetryAbort))
115+
it('should be able to use the engines field', () => {
116+
const engines = require(`${sandboxCwd()}/node_modules/dd-trace/package.json`).engines.node
113117

114-
it('should initialize the tracer, if DD_INJECT_FORCE', () => doTestForced('true\n', telemetryForced))
115-
})
118+
assert.match(engines, /^>=[\d]+(\.[\d]+){0,2} <[\d]+(\.[\d]+){0,2}?/)
119+
})
116120

117-
context('with debug', () => {
118-
useEnv({ DD_TRACE_DEBUG })
121+
context('when node version is out of range of the engines field', () => {
122+
useEnv({ NODE_OPTIONS })
123+
124+
before(() => {
125+
const pkg = JSON.parse(pkgStr)
126+
pkg.engines.node = '>=0 <0'
127+
fs.writeFileSync(pkgPath, JSON.stringify(pkg))
128+
})
119129

120-
it('should not initialize the tracer', () =>
121-
doTest(`Aborting application instrumentation due to incompatible_runtime.
130+
it('should not initialize the tracer', () => doTest('false\n', []))
131+
132+
context('with DD_INJECTION_ENABLED', () => {
133+
useEnv({ DD_INJECTION_ENABLED })
134+
135+
context('without debug', () => {
136+
it('should not initialize the tracer', () => doTest('false\n', telemetryAbort))
137+
138+
it('should initialize the tracer, if DD_INJECT_FORCE', () => doTestForced('true\n', telemetryForced))
139+
})
140+
141+
context('with debug', () => {
142+
useEnv({ DD_TRACE_DEBUG })
143+
144+
it('should not initialize the tracer', () =>
145+
doTest(`Aborting application instrumentation due to incompatible_runtime.
122146
Found incompatible runtime Node.js ${process.versions.node}, Supported runtimes: Node.js \
123-
${engines.node}.
147+
>=0 <0.
124148
false
125149
`, telemetryAbort))
126150

127-
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
128-
doTestForced(`Aborting application instrumentation due to incompatible_runtime.
151+
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
152+
doTestForced(`Aborting application instrumentation due to incompatible_runtime.
129153
Found incompatible runtime Node.js ${process.versions.node}, Supported runtimes: Node.js \
130-
${engines.node}.
154+
>=0 <0.
131155
DD_INJECT_FORCE enabled, allowing unsupported runtimes and continuing.
132156
Application instrumentation bootstrapping complete
133157
true
134158
`, telemetryForced))
135-
})
136159
})
137160
})
138-
} else {
139-
context('when node version is more than engines field', () => {
140-
useEnv({ NODE_OPTIONS })
161+
})
162+
163+
context('when node version is in range of the engines field', () => {
164+
useEnv({ NODE_OPTIONS })
165+
166+
before(() => {
167+
const pkg = JSON.parse(pkgStr)
168+
pkg.engines.node = '>=0 <1000'
169+
fs.writeFileSync(pkgPath, JSON.stringify(pkg))
170+
})
141171

142-
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => doTest('true\n', [], 'manual'))
172+
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => doTest('true\n', [], 'manual'))
143173

144-
context('with DD_INJECTION_ENABLED', () => {
145-
useEnv({ DD_INJECTION_ENABLED })
174+
context('with DD_INJECTION_ENABLED', () => {
175+
useEnv({ DD_INJECTION_ENABLED })
146176

147-
context('without debug', () => {
148-
it('should initialize the tracer', () => doTest('true\n', telemetryGood, 'ssi'))
177+
context('without debug', () => {
178+
it('should initialize the tracer', () => doTest('true\n', telemetryGood, 'ssi'))
149179

150-
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
151-
doTestForced('true\n', telemetryGood, 'ssi'))
152-
})
180+
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
181+
doTestForced('true\n', telemetryGood, 'ssi'))
182+
})
153183

154-
context('with debug', () => {
155-
useEnv({ DD_TRACE_DEBUG })
184+
context('with debug', () => {
185+
useEnv({ DD_TRACE_DEBUG })
156186

157-
it('should initialize the tracer', () =>
158-
doTest('Application instrumentation bootstrapping complete\ntrue\n', telemetryGood, 'ssi'))
187+
it('should initialize the tracer', () =>
188+
doTest('Application instrumentation bootstrapping complete\ntrue\n', telemetryGood, 'ssi'))
159189

160-
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
161-
doTestForced('Application instrumentation bootstrapping complete\ntrue\n', telemetryGood, 'ssi'))
162-
})
190+
it('should initialize the tracer, if DD_INJECT_FORCE', () =>
191+
doTestForced('Application instrumentation bootstrapping complete\ntrue\n', telemetryGood, 'ssi'))
163192
})
164193
})
165-
}
194+
})
166195
})
167196
}
168197

packages/dd-trace/src/guardrails/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ function guard (fn) {
1414
var clobberBailout = false
1515
var forced = isTrue(process.env.DD_INJECT_FORCE)
1616
var engines = require('../../../../package.json').engines
17-
var majors = engines.node.match(/\d+/)
18-
var minMajor = parseInt(majors[0])
19-
var maxMajor = parseInt(majors[1])
17+
var versions = engines.node.match(/[\d.]+/g)
18+
var minMajor = versions[0].split('.')[0]
19+
var maxMajor = versions[1].split('.')[0]
2020
var version = process.versions.node
2121

2222
if (process.env.DD_INJECTION_ENABLED) {

0 commit comments

Comments
 (0)