diff --git a/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.test.tsx b/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.test.tsx index 40bb37c22..231933397 100644 --- a/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.test.tsx +++ b/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.test.tsx @@ -33,6 +33,7 @@ jest.mock("../../../../UserContext", () => ({ })); jest.mock("../Utils/CommonUtils", () => ({ + ...jest.requireActual("../Utils/CommonUtils"), getHostFromUrl: jest.fn().mockReturnValue("test-mongo.documents.azure.com"), })); @@ -124,7 +125,10 @@ describe("MongoShellHandler", () => { describe("getTerminalSuppressedData", () => { it("should return the correct warning message", () => { - expect(mongoShellHandler.getTerminalSuppressedData()).toEqual(["Warning: Non-Genuine MongoDB Detected"]); + expect(mongoShellHandler.getTerminalSuppressedData()).toEqual([ + "Warning: Non-Genuine MongoDB Detected", + "Telemetry is now disabled.", + ]); }); }); }); diff --git a/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.tsx b/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.tsx index dfebe6831..ee56f59f6 100644 --- a/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.tsx +++ b/src/Explorer/Tabs/CloudShellTab/ShellTypes/MongoShellHandler.tsx @@ -1,10 +1,11 @@ import { userContext } from "../../../../UserContext"; -import { getHostFromUrl } from "../Utils/CommonUtils"; +import { filterAndCleanTerminalOutput, getHostFromUrl, getMongoShellRemoveInfoText } from "../Utils/CommonUtils"; import { AbstractShellHandler, DISABLE_TELEMETRY_COMMAND } from "./AbstractShellHandler"; export class MongoShellHandler extends AbstractShellHandler { private _key: string; private _endpoint: string | undefined; + private _removeInfoText: string[] = getMongoShellRemoveInfoText(); constructor(private key: string) { super(); this._key = key; @@ -44,6 +45,10 @@ export class MongoShellHandler extends AbstractShellHandler { } public getTerminalSuppressedData(): string[] { - return ["Warning: Non-Genuine MongoDB Detected"]; + return ["Warning: Non-Genuine MongoDB Detected", "Telemetry is now disabled."]; + } + + updateTerminalData(data: string): string { + return filterAndCleanTerminalOutput(data, this._removeInfoText); } } diff --git a/src/Explorer/Tabs/CloudShellTab/ShellTypes/VCoreMongoShellHandler.tsx b/src/Explorer/Tabs/CloudShellTab/ShellTypes/VCoreMongoShellHandler.tsx index c42598395..85b2a7bb6 100644 --- a/src/Explorer/Tabs/CloudShellTab/ShellTypes/VCoreMongoShellHandler.tsx +++ b/src/Explorer/Tabs/CloudShellTab/ShellTypes/VCoreMongoShellHandler.tsx @@ -1,13 +1,10 @@ import { userContext } from "../../../../UserContext"; +import { filterAndCleanTerminalOutput, getMongoShellRemoveInfoText } from "../Utils/CommonUtils"; import { AbstractShellHandler, DISABLE_TELEMETRY_COMMAND } from "./AbstractShellHandler"; export class VCoreMongoShellHandler extends AbstractShellHandler { private _endpoint: string | undefined; - private _textFilterRules: string[] = [ - "For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/", - "disableTelemetry() command", - "https://www.mongodb.com/legal/privacy-policy", - ]; + private _removeInfoText: string[] = getMongoShellRemoveInfoText(); constructor() { super(); @@ -38,12 +35,7 @@ export class VCoreMongoShellHandler extends AbstractShellHandler { return ["Warning: Non-Genuine MongoDB Detected", "Telemetry is now disabled."]; } - updateTerminalData(content: string): string { - const updatedContent = content - .split("\n") - .filter((line) => !this._textFilterRules.some((part) => line.includes(part))) - .filter((line, idx, arr) => (arr.length > 3 && idx <= arr.length - 3 ? !["", "\r"].includes(line) : true)) // Filter out empty lines and carriage returns, but keep the last 3 lines if they exist - .join("\n"); - return updatedContent; + updateTerminalData(data: string): string { + return filterAndCleanTerminalOutput(data, this._removeInfoText); } } diff --git a/src/Explorer/Tabs/CloudShellTab/Utils/AttachAddOn.tsx b/src/Explorer/Tabs/CloudShellTab/Utils/AttachAddOn.tsx index 0fbdc0aee..0a388b28a 100644 --- a/src/Explorer/Tabs/CloudShellTab/Utils/AttachAddOn.tsx +++ b/src/Explorer/Tabs/CloudShellTab/Utils/AttachAddOn.tsx @@ -135,7 +135,11 @@ export class AttachAddon implements ITerminalAddon { } if (this._allowTerminalWrite) { - const updatedData = this._shellHandler?.updateTerminalData(data) ?? data; + const updatedData = + typeof this._shellHandler?.updateTerminalData === "function" + ? this._shellHandler.updateTerminalData(data) + : data; + const suppressedData = this._shellHandler?.getTerminalSuppressedData(); const shouldNotWrite = suppressedData.filter(Boolean).some((item) => updatedData.includes(item)); diff --git a/src/Explorer/Tabs/CloudShellTab/Utils/CommonUtils.tsx b/src/Explorer/Tabs/CloudShellTab/Utils/CommonUtils.tsx index 42bdfff2e..a088c6edd 100644 --- a/src/Explorer/Tabs/CloudShellTab/Utils/CommonUtils.tsx +++ b/src/Explorer/Tabs/CloudShellTab/Utils/CommonUtils.tsx @@ -50,3 +50,34 @@ export const getShellNameForDisplay = (terminalKind: TerminalKind): string => { return ""; } }; + +/** + * Get MongoDB shell information text that should be removed from terminal output + */ +export const getMongoShellRemoveInfoText = (): string[] => { + return [ + "For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/", + "disableTelemetry() command", + "https://www.mongodb.com/legal/privacy-policy", + ]; +}; + +export const filterAndCleanTerminalOutput = (data: string, removeInfoText: string[]): string => { + if (!data || removeInfoText.length === 0) { + return data; + } + + const lines = data.split("\n"); + const filteredLines: string[] = []; + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + const shouldRemove = removeInfoText.some((text) => line.includes(text)); + + if (!shouldRemove) { + filteredLines.push(line); + } + } + + return filteredLines.join("\n").replace(/((\r\n)|\n|\r){2,}/g, "\r\n"); +};