Skip to content

Commit b591245

Browse files
feat: Add glob dependency and refactor glob pattern matching in helper functions (#45)
* feat: Add glob dependency and refactor glob pattern matching in helper functions * Glob Version update * Eslint Fix
1 parent 3c723ab commit b591245

File tree

2 files changed

+14
-108
lines changed

2 files changed

+14
-108
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"@cypress/request": "^3.0.1",
4646
"strip-ansi": "^6.0.1",
4747
"winston-transport": "^4.5.0",
48-
"uuid": "^9.0.0"
48+
"uuid": "^9.0.0",
49+
"glob": "^7.2.0"
4950
},
5051
"devDependencies": {
5152
"chai": "^4.3.7",

src/utils/helper.js

Lines changed: 12 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const os = require('os');
22
const fs = require('fs');
33
const fsPromises = fs.promises;
44
const path = require('path');
5+
const glob = require('glob');
56
const {promisify} = require('util');
67
const gitRepoInfo = require('git-repo-info');
78
const gitconfig = require('gitconfiglocal');
@@ -888,84 +889,7 @@ exports.truncateString = (field, truncateSizeInBytes) => {
888889

889890
// Helper function to check if a pattern contains glob characters
890891
exports.isGlobPattern = (pattern) => {
891-
return pattern.includes('*') || pattern.includes('?') || pattern.includes('[');
892-
};
893-
894-
// Helper function to recursively find files matching a pattern
895-
exports.findFilesRecursively = (dir, pattern) => {
896-
const files = [];
897-
try {
898-
if (!fs.existsSync(dir)) {
899-
return files;
900-
}
901-
902-
const entries = fs.readdirSync(dir, {withFileTypes: true});
903-
904-
for (const entry of entries) {
905-
const fullPath = path.join(dir, entry.name);
906-
907-
if (entry.isDirectory()) {
908-
// Recursively search subdirectories
909-
files.push(...exports.findFilesRecursively(fullPath, pattern));
910-
} else if (entry.isFile()) {
911-
const relativePath = path.relative(process.cwd(), fullPath);
912-
913-
// Enhanced pattern matching for glob patterns
914-
if (exports.matchesGlobPattern(relativePath, pattern)) {
915-
files.push(relativePath);
916-
}
917-
}
918-
}
919-
} catch (err) {
920-
Logger.debug(`Error reading directory ${dir}: ${err.message}`);
921-
}
922-
923-
return files;
924-
};
925-
926-
// Helper function to match a file path against a glob pattern
927-
exports.matchesGlobPattern = (filePath, pattern) => {
928-
// Normalize paths to use forward slashes
929-
const normalizedPath = filePath.replace(/\\/g, '/');
930-
const normalizedPattern = pattern.replace(/\\/g, '/');
931-
932-
// Convert glob pattern to regex step by step
933-
let regexPattern = normalizedPattern;
934-
935-
// First, handle ** patterns (must be done before single *)
936-
// ** should match zero or more directories
937-
regexPattern = regexPattern.replace(/\*\*/g, '§DOUBLESTAR§');
938-
939-
// Escape regex special characters except the placeholders
940-
regexPattern = regexPattern.replace(/[.+^${}()|[\]\\]/g, '\\$&');
941-
942-
// Now handle single * and ? patterns
943-
regexPattern = regexPattern.replace(/\*/g, '[^/]*'); // * matches anything except path separators
944-
regexPattern = regexPattern.replace(/\?/g, '[^/]'); // ? matches single character except path separator
945-
946-
// Finally, replace ** placeholder with regex for any path (including zero directories)
947-
regexPattern = regexPattern.replace(/§DOUBLESTAR§/g, '.*?');
948-
949-
// Special case: if pattern ends with /**/* we need to handle direct files in the base directory
950-
// Convert patterns like "dir/**/*" to also match "dir/*"
951-
if (normalizedPattern.includes('/**/')) {
952-
const baseRegex = regexPattern;
953-
const alternativeRegex = regexPattern.replace(/\/\.\*\?\//g, '/');
954-
regexPattern = `(?:${baseRegex}|${alternativeRegex})`;
955-
}
956-
957-
// Ensure pattern matches from start to end
958-
regexPattern = '^' + regexPattern + '$';
959-
960-
try {
961-
const regex = new RegExp(regexPattern);
962-
963-
return regex.test(normalizedPath);
964-
} catch (err) {
965-
Logger.debug(`Error in glob pattern matching: ${err.message}`);
966-
967-
return false;
968-
}
892+
return glob.hasMagic(pattern);
969893
};
970894

971895
// Helper function to resolve and collect test files from a path/pattern
@@ -1039,36 +963,17 @@ exports.findTestFilesInDirectory = (dir) => {
1039963
exports.expandGlobPattern = (pattern) => {
1040964
Logger.debug(`Expanding glob pattern: ${pattern}`);
1041965

1042-
// Extract the base directory from the pattern
1043-
const parts = pattern.split(/[/\\]/);
1044-
let baseDir = '.';
1045-
let patternStart = 0;
1046-
1047-
// Find the first part that contains glob characters
1048-
for (let i = 0; i < parts.length; i++) {
1049-
if (exports.isGlobPattern(parts[i])) {
1050-
patternStart = i;
1051-
break;
1052-
}
1053-
if (i === 0 && parts[i] !== '.') {
1054-
baseDir = parts[i];
1055-
} else if (i > 0) {
1056-
baseDir = path.join(baseDir, parts[i]);
1057-
}
1058-
}
1059-
1060-
// If baseDir doesn't exist, try current directory
1061-
if (!fs.existsSync(baseDir)) {
1062-
Logger.debug(`Base directory ${baseDir} doesn't exist, using current directory`);
1063-
baseDir = '.';
966+
try {
967+
const files = glob.sync(pattern);
968+
969+
Logger.debug(`Found ${files.length} files matching pattern: ${pattern}`);
970+
971+
return files;
972+
} catch (err) {
973+
Logger.debug(`Error expanding glob pattern: ${err.message}`);
974+
975+
return [];
1064976
}
1065-
1066-
Logger.debug(`Base directory: ${baseDir}, Pattern: ${pattern}`);
1067-
1068-
const files = exports.findFilesRecursively(baseDir, pattern);
1069-
Logger.debug(`Found ${files.length} files matching pattern: ${pattern}`);
1070-
1071-
return files;
1072977
};
1073978

1074979
/**

0 commit comments

Comments
 (0)