|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { assert } from 'chai'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import * as typemoq from 'typemoq'; |
| 8 | +import { ConfigurationChangeEvent, EventEmitter, Uri, WorkspaceConfiguration, WorkspaceFolder } from 'vscode'; |
| 9 | +import { IDisposableRegistry } from '../../client/common/types'; |
| 10 | +import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis'; |
| 11 | +import { getEnvsExplicitFalseScope, registerEnvironmentsExtensionTelemetry } from '../../client/envExt/telemetry'; |
| 12 | +import * as telemetry from '../../client/telemetry'; |
| 13 | +import { EventName } from '../../client/telemetry/constants'; |
| 14 | +import { EnvsExplicitFalseScope } from '../../client/telemetry/types'; |
| 15 | + |
| 16 | +type Inspection = { |
| 17 | + defaultValue?: boolean; |
| 18 | + globalValue?: boolean; |
| 19 | + workspaceValue?: boolean; |
| 20 | + workspaceFolderValue?: boolean; |
| 21 | +}; |
| 22 | + |
| 23 | +function configuration(values?: Inspection): WorkspaceConfiguration { |
| 24 | + const config = typemoq.Mock.ofType<WorkspaceConfiguration>(); |
| 25 | + config |
| 26 | + .setup((c) => c.inspect<boolean>('useEnvironmentsExtension')) |
| 27 | + .returns(() => (values ? { key: 'python.useEnvironmentsExtension', ...values } : undefined)); |
| 28 | + return config.object; |
| 29 | +} |
| 30 | + |
| 31 | +suite('Environments extension telemetry', () => { |
| 32 | + const first: WorkspaceFolder = { |
| 33 | + name: 'first', |
| 34 | + index: 0, |
| 35 | + uri: Uri.file(path.join(process.cwd(), 'first')), |
| 36 | + }; |
| 37 | + const second: WorkspaceFolder = { |
| 38 | + name: 'second', |
| 39 | + index: 1, |
| 40 | + uri: Uri.file(path.join(process.cwd(), 'second')), |
| 41 | + }; |
| 42 | + let getConfiguration: sinon.SinonStub; |
| 43 | + let getWorkspaceFolders: sinon.SinonStub; |
| 44 | + let sendTelemetryEvent: sinon.SinonStub; |
| 45 | + let changes: EventEmitter<ConfigurationChangeEvent>; |
| 46 | + let disposables: IDisposableRegistry; |
| 47 | + |
| 48 | + setup(() => { |
| 49 | + getConfiguration = sinon.stub(workspaceApis, 'getConfiguration').returns(configuration({})); |
| 50 | + getWorkspaceFolders = sinon.stub(workspaceApis, 'getWorkspaceFolders').returns(undefined); |
| 51 | + sendTelemetryEvent = sinon.stub(telemetry, 'sendTelemetryEvent'); |
| 52 | + changes = new EventEmitter<ConfigurationChangeEvent>(); |
| 53 | + sinon.stub(workspaceApis, 'onDidChangeConfiguration').callsFake((listener) => changes.event(listener)); |
| 54 | + disposables = []; |
| 55 | + }); |
| 56 | + |
| 57 | + teardown(() => { |
| 58 | + disposables.forEach((disposable) => disposable.dispose()); |
| 59 | + changes.dispose(); |
| 60 | + sinon.restore(); |
| 61 | + }); |
| 62 | + |
| 63 | + const scopeCases: { inspection: Inspection; expected: EnvsExplicitFalseScope }[] = [ |
| 64 | + { inspection: {}, expected: 'none' }, |
| 65 | + { inspection: { globalValue: false }, expected: 'user' }, |
| 66 | + { inspection: { workspaceValue: false }, expected: 'workspace' }, |
| 67 | + { inspection: { workspaceFolderValue: false }, expected: 'folder' }, |
| 68 | + { inspection: { globalValue: false, workspaceValue: false }, expected: 'multiple' }, |
| 69 | + { inspection: { globalValue: false, workspaceFolderValue: false }, expected: 'multiple' }, |
| 70 | + { inspection: { workspaceValue: false, workspaceFolderValue: false }, expected: 'multiple' }, |
| 71 | + { |
| 72 | + inspection: { globalValue: false, workspaceValue: false, workspaceFolderValue: false }, |
| 73 | + expected: 'multiple', |
| 74 | + }, |
| 75 | + ]; |
| 76 | + scopeCases.forEach(({ inspection, expected }) => { |
| 77 | + test(`reports ${expected} for ${JSON.stringify(inspection)}`, () => { |
| 78 | + getConfiguration.returns(configuration(inspection)); |
| 79 | + getWorkspaceFolders.returns([first]); |
| 80 | + |
| 81 | + assert.strictEqual(getEnvsExplicitFalseScope(), expected); |
| 82 | + sinon.assert.alwaysCalledWithExactly(getConfiguration, 'python', first.uri); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + [undefined, false, true].forEach((defaultValue) => { |
| 87 | + test(`ignores the default value ${defaultValue}`, () => { |
| 88 | + getConfiguration.returns(configuration({ defaultValue })); |
| 89 | + |
| 90 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'none'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test('handles unavailable setting inspection', () => { |
| 95 | + getConfiguration.returns(configuration()); |
| 96 | + |
| 97 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'none'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('does not count explicit true settings as opt-outs', () => { |
| 101 | + getWorkspaceFolders.returns([first]); |
| 102 | + getConfiguration.returns( |
| 103 | + configuration({ defaultValue: false, globalValue: true, workspaceValue: true, workspaceFolderValue: true }), |
| 104 | + ); |
| 105 | + |
| 106 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'none'); |
| 107 | + }); |
| 108 | + |
| 109 | + test('reports a user false even when higher-precedence values are true', () => { |
| 110 | + getWorkspaceFolders.returns([first]); |
| 111 | + getConfiguration.returns( |
| 112 | + configuration({ globalValue: false, workspaceValue: true, workspaceFolderValue: true }), |
| 113 | + ); |
| 114 | + |
| 115 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'user'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('checks later folders with their own configuration scope', () => { |
| 119 | + getWorkspaceFolders.returns([first, second]); |
| 120 | + getConfiguration.withArgs('python', first.uri).returns(configuration({ workspaceFolderValue: true })); |
| 121 | + getConfiguration.withArgs('python', second.uri).returns(configuration({ workspaceFolderValue: false })); |
| 122 | + |
| 123 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'folder'); |
| 124 | + sinon.assert.calledWithExactly(getConfiguration, 'python', second.uri); |
| 125 | + }); |
| 126 | + |
| 127 | + test('multiple false folders are one scope kind', () => { |
| 128 | + getWorkspaceFolders.returns([first, second]); |
| 129 | + getConfiguration.returns(configuration({ workspaceFolderValue: false })); |
| 130 | + |
| 131 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'folder'); |
| 132 | + }); |
| 133 | + |
| 134 | + test('inspects user and workspace settings with no folders', () => { |
| 135 | + getConfiguration.returns(configuration({ globalValue: false, workspaceValue: false })); |
| 136 | + |
| 137 | + assert.strictEqual(getEnvsExplicitFalseScope(), 'multiple'); |
| 138 | + sinon.assert.calledOnceWithExactly(getConfiguration, 'python', undefined); |
| 139 | + }); |
| 140 | + |
| 141 | + test('registration does not emit a configuration-change event', () => { |
| 142 | + registerEnvironmentsExtensionTelemetry(disposables); |
| 143 | + |
| 144 | + assert.lengthOf(disposables, 1); |
| 145 | + sinon.assert.notCalled(sendTelemetryEvent); |
| 146 | + }); |
| 147 | + |
| 148 | + test('ignores unrelated configuration changes', () => { |
| 149 | + registerEnvironmentsExtensionTelemetry(disposables); |
| 150 | + changes.fire({ affectsConfiguration: (section) => section === 'python.defaultInterpreterPath' }); |
| 151 | + |
| 152 | + sinon.assert.notCalled(sendTelemetryEvent); |
| 153 | + sinon.assert.notCalled(getConfiguration); |
| 154 | + }); |
| 155 | + |
| 156 | + test('emits the current scope when false is added, moved, and removed', () => { |
| 157 | + registerEnvironmentsExtensionTelemetry(disposables); |
| 158 | + const event: ConfigurationChangeEvent = { |
| 159 | + affectsConfiguration: (section) => section === 'python.useEnvironmentsExtension', |
| 160 | + }; |
| 161 | + getConfiguration.returns(configuration({ globalValue: false })); |
| 162 | + changes.fire(event); |
| 163 | + getConfiguration.returns(configuration({ workspaceValue: false })); |
| 164 | + changes.fire(event); |
| 165 | + getConfiguration.returns(configuration({ workspaceValue: true })); |
| 166 | + changes.fire(event); |
| 167 | + |
| 168 | + assert.deepEqual( |
| 169 | + sendTelemetryEvent.getCalls().map((call) => call.args), |
| 170 | + ['user', 'workspace', 'none'].map((envsExplicitFalseScope) => [ |
| 171 | + EventName.ENVIRONMENTS_EXTENSION_SETTING_CHANGED, |
| 172 | + undefined, |
| 173 | + { envsExplicitFalseScope }, |
| 174 | + ]), |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + test('uses the current workspace folders on configuration changes', () => { |
| 179 | + registerEnvironmentsExtensionTelemetry(disposables); |
| 180 | + getWorkspaceFolders.returns([second]); |
| 181 | + getConfiguration.withArgs('python', second.uri).returns(configuration({ workspaceFolderValue: false })); |
| 182 | + changes.fire({ affectsConfiguration: () => true }); |
| 183 | + |
| 184 | + sinon.assert.calledOnceWithExactly( |
| 185 | + sendTelemetryEvent, |
| 186 | + EventName.ENVIRONMENTS_EXTENSION_SETTING_CHANGED, |
| 187 | + undefined, |
| 188 | + { envsExplicitFalseScope: 'folder' }, |
| 189 | + ); |
| 190 | + }); |
| 191 | + |
| 192 | + test('stops reporting after disposal', () => { |
| 193 | + registerEnvironmentsExtensionTelemetry(disposables); |
| 194 | + disposables.forEach((disposable) => disposable.dispose()); |
| 195 | + changes.fire({ affectsConfiguration: () => true }); |
| 196 | + |
| 197 | + sinon.assert.notCalled(sendTelemetryEvent); |
| 198 | + }); |
| 199 | +}); |
0 commit comments