@@ -58,34 +58,35 @@ let sandbox
58
58
// This _must_ be used with the useSandbox function
59
59
async function runAndCheckWithTelemetry ( filename , expectedOut , expectedTelemetryPoints , expectedSource ) {
60
60
const cwd = sandbox . folder
61
- const cleanup = telemetryForwarder ( expectedTelemetryPoints )
61
+ const cleanup = telemetryForwarder ( expectedTelemetryPoints . length > 0 )
62
62
const pid = await runAndCheckOutput ( filename , cwd , expectedOut , expectedSource )
63
63
const msgs = await cleanup ( )
64
64
if ( expectedTelemetryPoints . length === 0 ) {
65
65
// assert no telemetry sent
66
- try {
67
- assert . deepStrictEqual ( msgs . length , 0 )
68
- } catch ( e ) {
69
- // This console.log is useful for debugging telemetry. Plz don't remove.
70
- // eslint-disable-next-line no-console
71
- console . error ( 'Expected no telemetry, but got:\n' , msgs . map ( msg => JSON . stringify ( msg [ 1 ] . points ) ) . join ( '\n' ) )
72
- throw e
73
- }
74
- return
66
+ assert . strictEqual ( msgs . length , 0 , `Expected no telemetry, but got:\n${
67
+ msgs . map ( msg => JSON . stringify ( msg [ 1 ] . points ) ) . join ( '\n' )
68
+ } `)
69
+ } else {
70
+ assertTelemetryPoints ( pid , msgs , expectedTelemetryPoints )
75
71
}
72
+ }
73
+
74
+ function assertTelemetryPoints ( pid , msgs , expectedTelemetryPoints ) {
76
75
let points = [ ]
77
76
for ( const [ telemetryType , data ] of msgs ) {
78
77
assert . strictEqual ( telemetryType , 'library_entrypoint' )
79
78
assert . deepStrictEqual ( data . metadata , meta ( pid ) )
80
79
points = points . concat ( data . points )
81
80
}
82
- let expectedPoints = getPoints ( ...expectedTelemetryPoints )
83
- // We now have to sort both the expected and actual telemetry points.
84
- // This is because data can come in in any order.
85
- // We'll just contatenate all the data together for each point and sort them.
86
- points = points . map ( p => p . name + '\t' + p . tags . join ( ',' ) ) . sort ( ) . join ( '\n' )
87
- expectedPoints = expectedPoints . map ( p => p . name + '\t' + p . tags . join ( ',' ) ) . sort ( ) . join ( '\n' )
88
- assert . strictEqual ( points , expectedPoints )
81
+ const expectedPoints = getPoints ( ...expectedTelemetryPoints )
82
+ // Sort since data can come in in any order.
83
+ assert . deepStrictEqual ( points . sort ( pointsSorter ) , expectedPoints . sort ( pointsSorter ) )
84
+
85
+ function pointsSorter ( a , b ) {
86
+ a = a . name + '\t' + a . tags . join ( ',' )
87
+ b = b . name + '\t' + b . tags . join ( ',' )
88
+ return a === b ? 0 : a < b ? - 1 : 1
89
+ }
89
90
90
91
function getPoints ( ...args ) {
91
92
const expectedPoints = [ ]
@@ -94,7 +95,7 @@ async function runAndCheckWithTelemetry (filename, expectedOut, expectedTelemetr
94
95
if ( ! currentPoint . name ) {
95
96
currentPoint . name = 'library_entrypoint.' + arg
96
97
} else {
97
- currentPoint . tags = arg . split ( ',' )
98
+ currentPoint . tags = arg . split ( ',' ) . filter ( Boolean )
98
99
expectedPoints . push ( currentPoint )
99
100
currentPoint = { }
100
101
}
@@ -239,7 +240,7 @@ async function createSandbox (dependencies = [], isGitRepo = false,
239
240
}
240
241
}
241
242
242
- function telemetryForwarder ( expectedTelemetryPoints ) {
243
+ function telemetryForwarder ( shouldExpectTelemetryPoints = true ) {
243
244
process . env . DD_TELEMETRY_FORWARDER_PATH =
244
245
path . join ( __dirname , '..' , 'telemetry-forwarder.sh' )
245
246
process . env . FORWARDER_OUT = path . join ( __dirname , `forwarder-${ Date . now ( ) } .out` )
@@ -257,7 +258,7 @@ function telemetryForwarder (expectedTelemetryPoints) {
257
258
try {
258
259
msgs = fs . readFileSync ( process . env . FORWARDER_OUT , 'utf8' ) . trim ( ) . split ( '\n' )
259
260
} catch ( e ) {
260
- if ( expectedTelemetryPoints . length && e . code === 'ENOENT' && retries < 10 ) {
261
+ if ( shouldExpectTelemetryPoints && e . code === 'ENOENT' && retries < 10 ) {
261
262
return tryAgain ( )
262
263
}
263
264
return [ ]
@@ -438,6 +439,8 @@ module.exports = {
438
439
assertObjectContains,
439
440
assertUUID,
440
441
spawnProc,
442
+ telemetryForwarder,
443
+ assertTelemetryPoints,
441
444
runAndCheckWithTelemetry,
442
445
createSandbox,
443
446
curl,
0 commit comments