From dcafddc92bea66d0edfb816edb105c26f237f138 Mon Sep 17 00:00:00 2001 From: peternhale Date: Thu, 4 Jan 2024 15:12:50 -0700 Subject: [PATCH] fix: correct log file name formatting error (#5316) @ W-13682486@ fix logfile name formatting error ensure the directory to the logfiles is present --- .../src/date/format.ts | 2 +- .../test/jest/date/format.test.ts | 65 +++++++++++++++++++ .../src/commands/forceAnonApexExecute.ts | 23 +++---- 3 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 packages/salesforcedx-utils-vscode/test/jest/date/format.test.ts diff --git a/packages/salesforcedx-utils-vscode/src/date/format.ts b/packages/salesforcedx-utils-vscode/src/date/format.ts index 077a3805c0..b287767616 100644 --- a/packages/salesforcedx-utils-vscode/src/date/format.ts +++ b/packages/salesforcedx-utils-vscode/src/date/format.ts @@ -18,7 +18,7 @@ export const getYYYYMMddHHmmssDateFormat = (localUTCDate: Date): string => { }; export const makeDoubleDigit = (currentDigit: number): string => { - return format('%02d', currentDigit); + return format('%d', currentDigit).padStart(2, '0'); }; export const optionYYYYMMddHHmmss: Intl.DateTimeFormatOptions = { diff --git a/packages/salesforcedx-utils-vscode/test/jest/date/format.test.ts b/packages/salesforcedx-utils-vscode/test/jest/date/format.test.ts new file mode 100644 index 0000000000..83a466ea27 --- /dev/null +++ b/packages/salesforcedx-utils-vscode/test/jest/date/format.test.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { + getYYYYMMddHHmmssDateFormat, + makeDoubleDigit, + optionYYYYMMddHHmmss, + optionHHmm, + optionMMddYYYY +} from '../../../src/date/format'; + +describe('getYYYYMMddHHmmssDateFormat', () => { + it('should return the correct date format', () => { + const date = new Date('2022-01-01T12:34:56'); + const formattedDate = getYYYYMMddHHmmssDateFormat(date); + expect(formattedDate).toBe('20220101123456'); + }); +}); + +describe('makeDoubleDigit', () => { + it('should return the double digit format for single digit numbers', () => { + const doubleDigit = makeDoubleDigit(5); + expect(doubleDigit).toBe('05'); + }); + + it('should return the same number for double digit numbers', () => { + const doubleDigit = makeDoubleDigit(12); + expect(doubleDigit).toBe('12'); + }); +}); + +describe('optionYYYYMMddHHmmss', () => { + it('should have the correct options', () => { + expect(optionYYYYMMddHHmmss).toEqual({ + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit' + }); + }); +}); + +describe('optionHHmm', () => { + it('should have the correct options', () => { + expect(optionHHmm).toEqual({ + hour: '2-digit', + minute: '2-digit' + }); + }); +}); + +describe('optionMMddYYYY', () => { + it('should have the correct options', () => { + expect(optionMMddYYYY).toEqual({ + month: '2-digit', + day: '2-digit', + year: 'numeric' + }); + }); +}); diff --git a/packages/salesforcedx-vscode-apex/src/commands/forceAnonApexExecute.ts b/packages/salesforcedx-vscode-apex/src/commands/forceAnonApexExecute.ts index 16b3daecef..177d5c1e80 100644 --- a/packages/salesforcedx-vscode-apex/src/commands/forceAnonApexExecute.ts +++ b/packages/salesforcedx-vscode-apex/src/commands/forceAnonApexExecute.ts @@ -35,7 +35,8 @@ interface ApexExecuteParameters { } export class AnonApexGatherer - implements ParametersGatherer { + implements ParametersGatherer +{ public async gather(): Promise< CancelResponse | ContinueResponse > { @@ -70,12 +71,9 @@ export class AnonApexGatherer } } -export class AnonApexLibraryExecuteExecutor extends LibraryCommandletExecutor< - ApexExecuteParameters -> { - public static diagnostics = vscode.languages.createDiagnosticCollection( - 'apex-errors' - ); +export class AnonApexLibraryExecuteExecutor extends LibraryCommandletExecutor { + public static diagnostics = + vscode.languages.createDiagnosticCollection('apex-errors'); private isDebugging: boolean; @@ -168,6 +166,7 @@ export class AnonApexLibraryExecuteExecutor extends LibraryCommandletExecutor< return false; } + fs.mkdirSync(path.dirname(logFilePath), { recursive: true }); fs.writeFileSync(logFilePath, logs); return true; @@ -203,12 +202,8 @@ export class AnonApexLibraryExecuteExecutor extends LibraryCommandletExecutor< AnonApexLibraryExecuteExecutor.diagnostics.clear(); if (response.diagnostic) { - const { - compileProblem, - exceptionMessage, - lineNumber, - columnNumber - } = response.diagnostic[0]; + const { compileProblem, exceptionMessage, lineNumber, columnNumber } = + response.diagnostic[0]; let message; if (compileProblem && compileProblem !== '') { message = compileProblem; @@ -254,7 +249,7 @@ export class AnonApexLibraryExecuteExecutor extends LibraryCommandletExecutor< } } -export const forceAnonApexExecute = async () => { +export const forceAnonApexExecute = async () => { const commandlet = new SfdxCommandlet( new SfdxWorkspaceChecker(), new AnonApexGatherer(),