Skip to content

Commit

Permalink
feat: support multistep glob pattern [sc-18837]
Browse files Browse the repository at this point in the history
  • Loading branch information
maxigimenez committed Mar 6, 2024
1 parent bfc68da commit 64a93da
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default class Deploy extends AuthCommand {
repoUrl: checklyConfig.repoUrl,
checkMatch: checklyConfig.checks?.checkMatch,
browserCheckMatch: checklyConfig.checks?.browserChecks?.testMatch,
multistepCheckMatch: checklyConfig.checks?.multistepChecks?.testMatch,
ignoreDirectoriesMatch: checklyConfig.checks?.ignoreDirectoriesMatch,
checkDefaults: checklyConfig.checks,
browserCheckDefaults: checklyConfig.checks?.browserChecks,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default class Test extends AuthCommand {
repoUrl: checklyConfig.repoUrl,
checkMatch: checklyConfig.checks?.checkMatch,
browserCheckMatch: checklyConfig.checks?.browserChecks?.testMatch,
multistepCheckMatch: checklyConfig.checks?.multistepChecks?.testMatch,
ignoreDirectoriesMatch: checklyConfig.checks?.ignoreDirectoriesMatch,
checkDefaults: checklyConfig.checks,
browserCheckDefaults: checklyConfig.checks?.browserChecks,
Expand Down
17 changes: 14 additions & 3 deletions packages/cli/src/constructs/check-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { Region } from '..'
import type { Frequency } from './frequency'
import type { RetryStrategy } from './retry-strategy'
import { AlertEscalation } from './alert-escalation-policy'
import { MultiStepCheck } from './multi-step-check'
import CheckTypes from '../constants'

const defaultApiCheckDefaults: ApiCheckDefaultConfig = {
headers: [],
Expand Down Expand Up @@ -189,14 +191,21 @@ export class CheckGroup extends Construct {
this.browserChecks = props.browserChecks
const fileAbsolutePath = Session.checkFileAbsolutePath!
if (props.browserChecks?.testMatch) {
this.__addChecks(fileAbsolutePath, props.browserChecks.testMatch)
this.__addChecks(fileAbsolutePath, props.browserChecks.testMatch, CheckTypes.BROWSER)
}
if (props.multiStepChecks?.testMatch) {
this.__addChecks(fileAbsolutePath, props.multiStepChecks.testMatch, CheckTypes.MULTI_STEP)
}
Session.registerConstruct(this)
this.__addSubscriptions()
this.__addPrivateLocationGroupAssignments()
}

private __addChecks (fileAbsolutePath: string, testMatch: string|string[]) {
private __addChecks (
fileAbsolutePath: string,
testMatch: string|string[],
checkType: typeof CheckTypes.BROWSER | typeof CheckTypes.MULTI_STEP,
) {
const parent = path.dirname(fileAbsolutePath)
const matched = glob.sync(testMatch, { nodir: true, cwd: parent })
for (const match of matched) {
Expand All @@ -210,7 +219,9 @@ export class CheckGroup extends Construct {
// the browserChecks props inherited from the group are applied in BrowserCheck.constructor()
}
const checkLogicalId = pathToPosix(path.relative(Session.basePath!, filepath))
const check = new BrowserCheck(checkLogicalId, props)
checkType === CheckTypes.BROWSER
? new BrowserCheck(checkLogicalId, props)
: new MultiStepCheck(checkLogicalId, props)
}
}

Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/services/checkly-config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export type ChecklyConfig = {
*/
testMatch?: string | string[],
},
/**
* Multistep checks default configuration properties.
*/
multistepChecks?: CheckConfigDefaults & {
/**
* Glob pattern where the CLI looks for Playwright test files, i.e. all `.spec.ts` files
*/
testMatch?: string | string[],
},
},
/**
* CLI default configuration properties.
Expand Down
35 changes: 35 additions & 0 deletions packages/cli/src/services/project-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ProjectParseOpts = {
repoUrl?: string,
checkMatch?: string | string[],
browserCheckMatch?: string | string[],
multistepCheckMatch?: string | string[],
ignoreDirectoriesMatch?: string[],
checkDefaults?: CheckConfigDefaults,
browserCheckDefaults?: CheckConfigDefaults,
Expand All @@ -34,6 +35,7 @@ export async function parseProject (opts: ProjectParseOpts): Promise<Project> {
directory,
checkMatch = '**/*.check.{js,ts}',
browserCheckMatch,
multistepCheckMatch,
projectLogicalId,
projectName,
repoUrl,
Expand All @@ -60,6 +62,7 @@ export async function parseProject (opts: ProjectParseOpts): Promise<Project> {
const ignoreDirectories = ['**/node_modules/**', '**/.git/**', ...ignoreDirectoriesMatch]
await loadAllCheckFiles(directory, checkMatch, ignoreDirectories)
await loadAllBrowserChecks(directory, browserCheckMatch, ignoreDirectories, project)
await loadAllMultistepChecks(directory, multistepCheckMatch, ignoreDirectories, project)

// private-location must be processed after all checks and groups are loaded.
await loadAllPrivateLocationsSlugNames(project)
Expand Down Expand Up @@ -124,6 +127,38 @@ async function loadAllBrowserChecks (
}
}

async function loadAllMultistepChecks (
directory: string,
multistepCheckFilePattern: string | string[] | undefined,
ignorePattern: string[],
project: Project,
): Promise<void> {
if (!multistepCheckFilePattern) {
return
}
const checkFiles = await findFilesWithPattern(directory, multistepCheckFilePattern, ignorePattern)
const preexistingCheckFiles = new Set<string>()
Object.values(project.data.check).forEach((check) => {
if ((check instanceof MultiStepCheck || check instanceof BrowserCheck) && check.scriptPath) {
preexistingCheckFiles.add(check.scriptPath)
}
})

for (const checkFile of checkFiles) {
const relPath = pathToPosix(path.relative(directory, checkFile))
// Don't create an additional check if the checkFile was already added to a check in loadAllCheckFiles.
if (preexistingCheckFiles.has(relPath)) {
continue
}
const multistepCheck = new MultiStepCheck(pathToPosix(relPath), {
name: path.basename(checkFile),
code: {
entrypoint: checkFile,
},
})
}
}

// TODO: create a function to process slug names for check or check-group to reduce duplicated code.
async function loadAllPrivateLocationsSlugNames (
project: Project,
Expand Down

0 comments on commit 64a93da

Please sign in to comment.