forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.unit.test.ts
More file actions
143 lines (118 loc) · 5.33 KB
/
Copy pathindex.unit.test.ts
File metadata and controls
143 lines (118 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import rewiremock from 'rewiremock';
import * as sinon from 'sinon';
import * as fs from 'fs-extra';
import {
_resetSharedProperties,
clearTelemetryReporter,
sendTelemetryEvent,
setSharedProperty,
} from '../../client/telemetry';
suite('Telemetry', () => {
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
let readJSONSyncStub: sinon.SinonStub;
class Reporter {
public static eventName: string[] = [];
public static properties: Record<string, string>[] = [];
public static measures: {}[] = [];
public static exception: Error | undefined;
public static clear() {
Reporter.eventName = [];
Reporter.properties = [];
Reporter.measures = [];
}
public sendTelemetryEvent(eventName: string, properties?: {}, measures?: {}) {
Reporter.eventName.push(eventName);
Reporter.properties.push(properties!);
Reporter.measures.push(measures!);
}
public sendTelemetryErrorEvent(eventName: string, properties?: {}, measures?: {}) {
this.sendTelemetryEvent(eventName, properties, measures);
}
public sendTelemetryException(_error: Error, _properties?: {}, _measures?: {}): void {
throw new Error('sendTelemetryException is unsupported');
}
}
setup(() => {
process.env.VSC_PYTHON_UNIT_TEST = undefined;
process.env.VSC_PYTHON_CI_TEST = undefined;
readJSONSyncStub = sinon.stub(fs, 'readJSONSync');
readJSONSyncStub.returns({ enableTelemetry: true });
clearTelemetryReporter();
Reporter.clear();
});
teardown(() => {
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
rewiremock.disable();
_resetSharedProperties();
sinon.restore();
});
test('Send Telemetry', () => {
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
const eventName = 'Testing';
const properties = { hello: 'world', foo: 'bar' };
const measures = { start: 123, end: 987 };
sendTelemetryEvent(eventName as any, measures, properties as any);
expect(Reporter.eventName).to.deep.equal([eventName]);
expect(Reporter.measures).to.deep.equal([measures]);
expect(Reporter.properties).to.deep.equal([properties]);
});
test('Send Telemetry with no properties', () => {
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
const eventName = 'Testing';
sendTelemetryEvent(eventName as any);
expect(Reporter.eventName).to.deep.equal([eventName]);
expect(Reporter.measures).to.deep.equal([undefined], 'Measures should be empty');
expect(Reporter.properties).to.deep.equal([{}], 'Properties should be empty');
});
test('Send Telemetry with shared properties', () => {
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
const eventName = 'Testing';
const properties = { hello: 'world', foo: 'bar' };
const measures = { start: 123, end: 987 };
const expectedProperties = { ...properties, one: 'two' };
setSharedProperty('one' as any, 'two' as any);
sendTelemetryEvent(eventName as any, measures, properties as any);
expect(Reporter.eventName).to.deep.equal([eventName]);
expect(Reporter.measures).to.deep.equal([measures]);
expect(Reporter.properties).to.deep.equal([expectedProperties]);
});
test('Shared properties will replace existing ones', () => {
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
const eventName = 'Testing';
const properties = { hello: 'world', foo: 'bar' };
const measures = { start: 123, end: 987 };
const expectedProperties = { ...properties, foo: 'baz' };
setSharedProperty('foo' as any, 'baz' as any);
sendTelemetryEvent(eventName as any, measures, properties as any);
expect(Reporter.eventName).to.deep.equal([eventName]);
expect(Reporter.measures).to.deep.equal([measures]);
expect(Reporter.properties).to.deep.equal([expectedProperties]);
});
test('Send Exception Telemetry', () => {
rewiremock.enable();
const error = new Error('Boo');
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
const eventName = 'Testing';
const measures = { start: 123, end: 987 };
const properties = { hello: 'world', foo: 'bar' };
sendTelemetryEvent(eventName as any, measures, properties as any, error);
const expectedProperties = {
...properties,
errorName: error.name,
errorStack: error.stack,
};
expect(Reporter.eventName).to.deep.equal([eventName]);
expect(Reporter.properties).to.deep.equal([expectedProperties]);
expect(Reporter.measures).to.deep.equal([measures]);
});
});