From 8026f8328a1decc321594920d1638646d9cf3ccb Mon Sep 17 00:00:00 2001 From: Matt Carvin <90224411+mcarvin8@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:04:32 -0400 Subject: [PATCH] fix: remove covered line adjustment --- .../transformer/transform.ts | 11 +-- src/helpers/convertToGenericCoverageReport.ts | 25 +++--- src/helpers/getTotalLines.ts | 8 -- src/helpers/setCoveredLines.ts | 37 --------- src/helpers/types.ts | 2 +- test/baselines/classes/AccountProfile.cls | 72 ----------------- .../baselines/triggers/AccountTrigger.trigger | 40 ---------- test/commands/transformer/unit.test.ts | 18 +++-- test/coverage_baseline.xml | 78 +++++++++---------- 9 files changed, 65 insertions(+), 226 deletions(-) delete mode 100644 src/helpers/getTotalLines.ts delete mode 100644 src/helpers/setCoveredLines.ts delete mode 100644 test/baselines/classes/AccountProfile.cls delete mode 100644 test/baselines/triggers/AccountTrigger.trigger diff --git a/src/commands/apex-code-coverage/transformer/transform.ts b/src/commands/apex-code-coverage/transformer/transform.ts index 3dac0a8..c68b5bf 100644 --- a/src/commands/apex-code-coverage/transformer/transform.ts +++ b/src/commands/apex-code-coverage/transformer/transform.ts @@ -1,6 +1,6 @@ 'use strict'; -import { resolve } from 'node:path'; +import { resolve } from 'node:path/posix'; import { writeFile, readFile } from 'node:fs/promises'; import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; @@ -45,12 +45,9 @@ export default class TransformerTransform extends SfCommand { const { flags } = await this.parse(TransformerTransform); - let jsonFilePath = flags['coverage-json']; - let xmlFilePath = flags['xml']; - let sfdxConfigFile = flags['sfdx-configuration']; - jsonFilePath = resolve(jsonFilePath); - xmlFilePath = resolve(xmlFilePath); - sfdxConfigFile = resolve(sfdxConfigFile); + const jsonFilePath = resolve(flags['coverage-json']); + const xmlFilePath = resolve(flags['xml']); + const sfdxConfigFile = resolve(flags['sfdx-configuration']); const jsonData = await readFile(jsonFilePath, 'utf-8'); const coverageData = JSON.parse(jsonData) as CoverageData; diff --git a/src/helpers/convertToGenericCoverageReport.ts b/src/helpers/convertToGenericCoverageReport.ts index 9129e41..c76e2d6 100644 --- a/src/helpers/convertToGenericCoverageReport.ts +++ b/src/helpers/convertToGenericCoverageReport.ts @@ -5,7 +5,6 @@ import { create } from 'xmlbuilder2'; import { CoverageData, CoverageObject, FileObject } from './types.js'; import { findFilePath } from './findFilePath.js'; -import { setCoveredLines } from './setCoveredLines.js'; export async function convertToGenericCoverageReport( data: CoverageData, @@ -24,23 +23,21 @@ export async function convertToGenericCoverageReport( warnings.push(`The file name ${formattedFileName} was not found in any package directory.`); continue; } - const uncoveredLines = Object.keys(fileInfo.s) - .filter((lineNumber) => fileInfo.s[lineNumber] === 0) - .map(Number); - const coveredLines = Object.keys(fileInfo.s) - .filter((lineNumber) => fileInfo.s[lineNumber] === 1) - .map(Number); - const fileObj: FileObject = { '@path': filePath, - lineToCover: uncoveredLines.map((lineNumber: number) => ({ - '@lineNumber': lineNumber, - '@covered': 'false', - })), + lineToCover: [], }; - // this function is only needed until Salesforce fixes the API to correctly return covered lines - await setCoveredLines(coveredLines, uncoveredLines, filePath, fileObj); + for (const lineNumberString in fileInfo.s) { + if (!Object.hasOwn(fileInfo.s, lineNumberString)) continue; + + const covered = fileInfo.s[lineNumberString] === 1 ? 'true' : 'false'; + fileObj.lineToCover.push({ + '@lineNumber': lineNumberString, + '@covered': covered, + }); + } + filesProcessed++; coverageObj.coverage.file.push(fileObj); } diff --git a/src/helpers/getTotalLines.ts b/src/helpers/getTotalLines.ts deleted file mode 100644 index c33d319..0000000 --- a/src/helpers/getTotalLines.ts +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -import { readFile } from 'node:fs/promises'; - -export async function getTotalLines(filePath: string): Promise { - const fileContent = await readFile(filePath, 'utf8'); - return fileContent.split(/\r\n|\r|\n/).length; -} diff --git a/src/helpers/setCoveredLines.ts b/src/helpers/setCoveredLines.ts deleted file mode 100644 index d5fef89..0000000 --- a/src/helpers/setCoveredLines.ts +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -import { getTotalLines } from './getTotalLines.js'; -import { FileObject } from './types.js'; - -export async function setCoveredLines( - coveredLines: number[], - uncoveredLines: number[], - filePath: string, - fileObj: FileObject -): Promise { - const randomLines: number[] = []; - const totalLines = await getTotalLines(filePath); - for (const coveredLine of coveredLines) { - if (coveredLine > totalLines) { - for (let randomLineNumber = 1; randomLineNumber <= totalLines; randomLineNumber++) { - if ( - !uncoveredLines.includes(randomLineNumber) && - !coveredLines.includes(randomLineNumber) && - !randomLines.includes(randomLineNumber) - ) { - fileObj.lineToCover.push({ - '@lineNumber': randomLineNumber, - '@covered': 'true', - }); - randomLines.push(randomLineNumber); - break; - } - } - } else { - fileObj.lineToCover.push({ - '@lineNumber': coveredLine, - '@covered': 'true', - }); - } - } -} diff --git a/src/helpers/types.ts b/src/helpers/types.ts index b6816ba..c2a9348 100644 --- a/src/helpers/types.ts +++ b/src/helpers/types.ts @@ -23,7 +23,7 @@ export interface SfdxProject { } interface LineToCover { - '@lineNumber': number; + '@lineNumber': string; '@covered': string; } diff --git a/test/baselines/classes/AccountProfile.cls b/test/baselines/classes/AccountProfile.cls deleted file mode 100644 index 9b0951e..0000000 --- a/test/baselines/classes/AccountProfile.cls +++ /dev/null @@ -1,72 +0,0 @@ -global class PrepareMySandbox implements SandboxPostCopy { - global PrepareMySandbox() { - // Implementations of SandboxPostCopy must have a no-arg constructor. - // This constructor is used during the sandbox copy process. - } - - global void runApexClass(SandboxContext context) { - System.debug('Org ID: ' + context.organizationId()); - System.debug('Sandbox ID: ' + context.sandboxId()); - System.debug('Sandbox Name: ' + context.sandboxName()); - - updateProfilesAndResetPasswordsForPublicGroupMembers(); - // Additional logic to prepare the sandbox for use can be added here. - } - - public void updateProfilesAndResetPasswordsForPublicGroupMembers() { - String publicGroupId = '00G5a000003ji0R'; - String newProfileId = '00e0b000001KWuY'; - - Group publicGroup = getPublicGroup(publicGroupId); - - if (publicGroup != null) { - List usersToUpdate = getUsersToUpdate(publicGroup, newProfileId); - - if (!usersToUpdate.isEmpty()) { - update usersToUpdate; - System.debug('Profile updated for ' + usersToUpdate.size() + ' users.'); - - // Reset passwords for updated users - resetPasswords(usersToUpdate); - } else { - System.debug('No eligible active users found in the Public Group.'); - } - } else { - System.debug('Public Group not found.'); - } - } - - private Group getPublicGroup(String groupId) { - return [SELECT Id FROM Group WHERE Id = :groupId LIMIT 1]; - } - - private List getUsersToUpdate(Group publicGroup, String newProfileId) { - List usersToUpdate = new List(); - Set userIds = new Set(); - - // Get the current running User's Id - Id currentUserId = UserInfo.getUserId(); - - for (GroupMember member : [SELECT UserOrGroupId FROM GroupMember WHERE GroupId = :publicGroup.Id]) { - Id userOrGroupId = member.UserOrGroupId; - if (userOrGroupId != null && userOrGroupId.getSObjectType() == User.SObjectType && userOrGroupId != currentUserId) { - userIds.add(userOrGroupId); - } - } - - // Query and update active User profiles - for (User user : [SELECT Id, ProfileId FROM User WHERE Id IN :userIds AND IsActive = true]) { - user.ProfileId = newProfileId; - usersToUpdate.add(user); - } - - return usersToUpdate; - } - - private void resetPasswords(List users) { - for (User u : users) { - System.resetPassword(u.Id, true); // The second parameter generates a new password and sends an email - } - System.debug('Passwords reset for ' + users.size() + ' users.'); - } -} \ No newline at end of file diff --git a/test/baselines/triggers/AccountTrigger.trigger b/test/baselines/triggers/AccountTrigger.trigger deleted file mode 100644 index cd99f1a..0000000 --- a/test/baselines/triggers/AccountTrigger.trigger +++ /dev/null @@ -1,40 +0,0 @@ -trigger helloWorldAccountTrigger on Account (before insert) { - - Account[] accs = Trigger.new; - - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); - MyHelloWorld.addHelloWorld(accs); -} diff --git a/test/commands/transformer/unit.test.ts b/test/commands/transformer/unit.test.ts index 1a49520..c02e373 100644 --- a/test/commands/transformer/unit.test.ts +++ b/test/commands/transformer/unit.test.ts @@ -1,8 +1,8 @@ 'use strict'; -import { copyFile, readFile, writeFile, rm, mkdir } from 'node:fs/promises'; +import { readFile, writeFile, rm, mkdir } from 'node:fs/promises'; import { strictEqual } from 'node:assert'; -import { resolve } from 'node:path'; +import { resolve } from 'node:path/posix'; import { TestContext } from '@salesforce/core/lib/testSetup.js'; import { expect } from 'chai'; @@ -12,8 +12,10 @@ import TransformerTransform from '../../../src/commands/apex-code-coverage/trans describe('main', () => { const $$ = new TestContext(); let sfCommandStubs: ReturnType; - const baselineClassPath = resolve('test/baselines/classes/AccountProfile.cls'); - const baselineTriggerPath = resolve('test/baselines/triggers/AccountTrigger.trigger'); + const mockClassContent = '// Test Apex Class'; + const mockTriggerContent = '// Test Apex Trigger'; + const baselineClassPath = resolve('force-app/main/default/classes/AccountProfile.cls'); + const baselineTriggerPath = resolve('packaged/triggers/AccountTrigger.trigger'); const coverageJsonPathNoExts = resolve('test/coverage_no_file_exts.json'); const coverageJsonPathWithExts = resolve('test/coverage_with_file_exts.json'); const baselineXmlPath = resolve('test/coverage_baseline.xml'); @@ -32,8 +34,8 @@ describe('main', () => { before(async () => { await mkdir('force-app/main/default/classes', { recursive: true }); await mkdir('packaged/triggers', { recursive: true }); - await copyFile(baselineClassPath, 'force-app/main/default/classes/AccountProfile.cls'); - await copyFile(baselineTriggerPath, 'packaged/triggers/AccountTrigger.trigger'); + await writeFile(baselineClassPath, mockClassContent); + await writeFile(baselineTriggerPath, mockTriggerContent); await writeFile(sfdxConfigFile, configJsonString); }); @@ -46,8 +48,8 @@ describe('main', () => { }); after(async () => { - await rm('force-app/main/default/classes/AccountProfile.cls'); - await rm('packaged/triggers/AccountTrigger.trigger'); + await rm(baselineClassPath); + await rm(baselineTriggerPath); await rm('force-app', { recursive: true }); await rm('packaged', { recursive: true }); await rm(testXmlPath1); diff --git a/test/coverage_baseline.xml b/test/coverage_baseline.xml index 8a1a438..f1e856d 100644 --- a/test/coverage_baseline.xml +++ b/test/coverage_baseline.xml @@ -3,46 +3,46 @@ + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -55,15 +55,15 @@ - - - - - - - - - - + + + + + + + + + + \ No newline at end of file