-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate tests against package directories
- Loading branch information
Showing
7 changed files
with
155 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
'use strict' | ||
'use strict'; | ||
import { retrieveCommitMessages } from './retrieveCommitMessages.js'; | ||
import { validateClassPaths } from './validateClassPaths.js'; | ||
|
||
export function extractTestClasses(fromRef: string, toRef: string, regex: string): string { | ||
export async function extractTestClasses( | ||
fromRef: string, | ||
toRef: string, | ||
regex: string, | ||
sfdxConfigFile: string | ||
): Promise<{ validatedClasses: string; warnings: string[] }> { | ||
const testClasses: Set<string> = new Set(); | ||
const matchedMessages = retrieveCommitMessages(fromRef, toRef, regex); | ||
|
||
matchedMessages.forEach((message: string) => { | ||
// Split the commit message by commas or spaces | ||
const classes = message.split(/,|\s/); | ||
// Split the commit message by commas or spaces | ||
const classes = message.split(/,|\s/); | ||
|
||
classes.forEach((testClass: string) => { | ||
// Remove leading/trailing whitespaces and add non-empty strings to the set | ||
const trimmedClass = testClass.trim(); | ||
if (trimmedClass !== '') { | ||
testClasses.add(trimmedClass); | ||
} | ||
}); | ||
classes.forEach((testClass: string) => { | ||
// Remove leading/trailing whitespaces and add non-empty strings to the set | ||
const trimmedClass = testClass.trim(); | ||
if (trimmedClass !== '') { | ||
testClasses.add(trimmedClass); | ||
} | ||
}); | ||
}); | ||
|
||
// Sort test classes alphabetically and then return a space-separated string | ||
const sortedClasses = Array.from(testClasses).sort((a, b) => a.localeCompare(b)); | ||
return sortedClasses.join(' '); | ||
const unvalidatedClasses: string[] = Array.from(testClasses); | ||
let validatedClasses: string = ''; | ||
const result = | ||
unvalidatedClasses.length > 0 | ||
? await validateClassPaths(unvalidatedClasses, sfdxConfigFile) | ||
: { validatedClasses: new Set(), warnings: [] }; | ||
let sortedClasses: string[] = []; | ||
if (result.validatedClasses.size > 0) { | ||
sortedClasses = Array.from(result.validatedClasses) as string[]; | ||
sortedClasses = sortedClasses.sort((a, b) => a.localeCompare(b)); | ||
validatedClasses = sortedClasses.join(' '); | ||
} | ||
|
||
return { validatedClasses, warnings: result.warnings }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
/* eslint-disable no-await-in-loop */ | ||
|
||
import * as fs from 'node:fs'; | ||
import * as promises from 'node:fs/promises'; | ||
|
||
interface SfdxProject { | ||
packageDirectories: Array<{ path: string }>; | ||
} | ||
|
||
export async function getPackageDirectories(dxConfigFile: string): Promise<string[]> { | ||
if (!fs.existsSync(dxConfigFile)) { | ||
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigFile}`); | ||
} | ||
|
||
const sfdxProjectRaw: string = await promises.readFile(dxConfigFile, 'utf-8'); | ||
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject; | ||
const packageDirectories = sfdxProject.packageDirectories.map((directory) => directory.path); | ||
return packageDirectories; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
/* eslint-disable no-await-in-loop */ | ||
|
||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
|
||
import { getPackageDirectories } from './getPackageDirectories.js'; | ||
|
||
export async function validateClassPaths( | ||
unvalidatedClasses: string[], | ||
dxConfigFile: string | ||
): Promise<{ validatedClasses: Set<string>; warnings: string[] }> { | ||
const packageDirectories = await getPackageDirectories(dxConfigFile); | ||
const warnings: string[] = []; | ||
|
||
const validatedClasses: Set<string> = new Set(); | ||
for (const unvalidatedClass of unvalidatedClasses) { | ||
let validated: boolean = false; | ||
for (const directory of packageDirectories) { | ||
const relativeFilePath = await searchRecursively(`${unvalidatedClass}.cls`, directory); | ||
if (relativeFilePath !== undefined) { | ||
validatedClasses.add(unvalidatedClass); | ||
validated = true; | ||
break; | ||
} | ||
} | ||
if (!validated) | ||
warnings.push( | ||
`The class ${unvalidatedClass} was not found in any package directory and will not be added to the delta test classes.` | ||
); | ||
} | ||
return { validatedClasses, warnings }; | ||
} | ||
|
||
async function searchRecursively(fileName: string, dxDirectory: string): Promise<string | undefined> { | ||
const files = await fs.promises.readdir(dxDirectory); | ||
for (const file of files) { | ||
const filePath = path.join(dxDirectory, file); | ||
const stats = await fs.promises.stat(filePath); | ||
if (stats.isDirectory()) { | ||
const result = await searchRecursively(fileName, filePath); | ||
if (result) { | ||
return result; | ||
} | ||
} else if (file === fileName) { | ||
return filePath; | ||
} | ||
} | ||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters