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

fix(shell-api): do not throw exceptions from enable/disableTelemetry() MONGOSH-1769 #1966

Merged
merged 1 commit into from
May 2, 2024
Merged
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
3 changes: 2 additions & 1 deletion packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ export class CliRepl implements MongoshIOProvider {
get forceDisableTelemetry(): boolean {
return (
this.globalConfig?.forceDisableTelemetry ||
(this.isContainerizedEnvironment && !this.mongoshRepl.isInteractive)
(this.isContainerizedEnvironment && !this.mongoshRepl.isInteractive) ||
!!process.env.MONGOSH_FORCE_DISABLE_TELEMETRY_FOR_TESTING
);
}

Expand Down
22 changes: 22 additions & 0 deletions packages/e2e-tests/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,28 @@ describe('e2e', function () {
);
expect((await readConfig()).enableTelemetry).to.equal(false);
});
it('enableTelemetry() returns an error if forceDisableTelemetry is set (but does not throw)', async function () {
await shell.executeLine(
'process.env.MONGOSH_FORCE_DISABLE_TELEMETRY_FOR_TESTING = 1'
);
expect(
await shell.executeLine('enableTelemetry() + "<<<<"')
).to.include(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true<<<<"
);
expect((await readConfig()).enableTelemetry).to.equal(true);
});
it('disableTelemetry() returns an error if forceDisableTelemetry is set (but does not throw)', async function () {
await shell.executeLine(
'process.env.MONGOSH_FORCE_DISABLE_TELEMETRY_FOR_TESTING = 1'
);
expect(
await shell.executeLine('disableTelemetry() + "<<<<"')
).to.include(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true<<<<"
);
expect((await readConfig()).enableTelemetry).to.equal(true);
});
});

describe('log file', function () {
Expand Down
32 changes: 20 additions & 12 deletions packages/shell-api/src/shell-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,32 @@ export default class ShellApi extends ShellApiClass {
@returnsPromise
@platforms(['CLI'])
async enableTelemetry(): Promise<any> {
const result = await this._instanceState.evaluationListener.setConfig?.(
'enableTelemetry',
true
);
if (result === 'success') {
return i18n.__('cli-repl.cli-repl.enabledTelemetry');
try {
const result = await this._instanceState.evaluationListener.setConfig?.(
'enableTelemetry',
true
);
if (result === 'success') {
return i18n.__('cli-repl.cli-repl.enabledTelemetry');
}
} catch (err: unknown) {
return String(err);
}
}

@returnsPromise
@platforms(['CLI'])
async disableTelemetry(): Promise<any> {
const result = await this._instanceState.evaluationListener.setConfig?.(
'enableTelemetry',
false
);
if (result === 'success') {
return i18n.__('cli-repl.cli-repl.disabledTelemetry');
try {
const result = await this._instanceState.evaluationListener.setConfig?.(
'enableTelemetry',
false
);
if (result === 'success') {
return i18n.__('cli-repl.cli-repl.disabledTelemetry');
}
} catch (err: unknown) {
return String(err);
}
}

Expand Down
Loading