Skip to content

Commit

Permalink
use a file instead of marker comment
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Apr 3, 2024
1 parent 13efd26 commit cfb6bfb
Showing 1 changed file with 19 additions and 40 deletions.
59 changes: 19 additions & 40 deletions scripts/run-related-test.mjs
Original file line number Diff line number Diff line change
@@ -1,47 +1,16 @@
/**
* This script finds all files under the paths that contain a marker and reads lines after the marker from the files.
* The marker is defined as a comment line that starts with `// TEST:`.
* This script finds all `.tests` files under the paths and reads lines from these files.
* The script is useful to find all related test cases for a given code change.
* Usage: `node scripts/run-related-test.mjs <path1> <path2> ...`
*/

import { promisify } from 'node:util'
import { exec as execOrg } from 'node:child_process'
import { readFile, constants, access } from 'node:fs/promises'
import { dirname, join } from 'node:path'

const exec = promisify(execOrg)

/**
* Find all files under the paths that contain a marker
* @param {string[]} paths - Paths to search in
* @param {string} marker - Marker to search for
*/
async function findFiles(paths, marker) {
const foundPaths = []
for (const path of paths) {
const command = `find "${path}" -type f -exec grep -l "${marker}" {} +`
const { stdout } = await exec(command).catch((e) => e)
foundPaths.push(...stdout.trim().split('\n'))
}
return foundPaths.filter(Boolean)
}

/**
* Read lines after the marker from a file
* @param {string} filePath - File path to read
* @param {string} marker - Marker to search for
*/
async function readTestLines(filePath, marker) {
const escapedMarker = marker.replaceAll('/', '\\/')
let command = `awk '/${escapedMarker}/{flag=1;next}/^$/{flag=0}flag' ${filePath}`
command += ' | sort | uniq' // Sort and remove duplicates
const { stdout } = await exec(command)

return stdout
.trim()
.replace(/^\/\/ /gm, '') // Remove the '// ' comment prefix
.split('\n')
}

/**
* Get all changed files from git under the packages directory
* @returns {Promise<string[]>} - List of changed files
Expand All @@ -56,12 +25,22 @@ async function getChangedFilesFromPackages(baseBranch = 'origin/canary') {

export async function getRelatedTests(args = []) {
const paths = args.length ? args : await getChangedFilesFromPackages()
const marker = '// TEST:'
const files = await findFiles(paths, marker)
const lines = []
for (const file of files) lines.push(...(await readTestLines(file, marker)))
const relatedTestFile = '.tests'
const tests = []
for (const path of paths) {
const testFile = join(dirname(path), relatedTestFile)
const hasRelatedTests = await access(testFile, constants.F_OK)
.then(() => true)
.catch(() => false)

if (hasRelatedTests) {
const content = await readFile(testFile, 'utf-8')
const lines = content.split('\n').filter(Boolean)
tests.push(...lines)
}
}

return Array.from(new Set(lines))
return Array.from(new Set(tests))
}

// console.log(await getRelatedTest(process.argv.slice(2)))
// console.log(await getRelatedTests(process.argv.slice(2)))

0 comments on commit cfb6bfb

Please sign in to comment.