Add tests for regex fix functionality and enhance fix logic in fix-regex.js

This commit is contained in:
Mardav Wala
2025-08-15 17:31:52 +00:00
parent 3a231c99dc
commit ca20dc5da1
3 changed files with 326 additions and 23 deletions

View File

@@ -13,36 +13,93 @@ const filesToFix = [
'node_modules/@actions/github/node_modules/@octokit/request/dist-web/index.js',
]
process.stdout.write('🔧 Applying regex fix for @octokit/request...\n')
/**
* Apply regex fix to content
* @param {string} content - The file content to fix
* @returns {string} - The fixed content
*/
function applyRegexFix(content) {
let fixedContent = content
// Fix the problematic regex pattern - add proper grouping to fix operator precedence
fixedContent = fixedContent.replace(/\/\^text\\?\/\|charset=utf-8\$?\//g, '/^(text\\/|charset=utf-8)$/')
fixedContent = fixedContent.replace(/\/\^text\/\|charset=utf-8\$?\//g, '/^(text/|charset=utf-8)$/')
return fixedContent
}
let filesFixed = 0
for (const filePath of filesToFix) {
/**
* Fix a single file
* @param {string} filePath - Path to the file to fix
* @returns {{fixed: boolean, error: string|null}} - Result of the fix operation
*/
function fixFile(filePath) {
try {
let content = fs.readFileSync(filePath, 'utf8')
const content = fs.readFileSync(filePath, 'utf8')
const originalContent = content
const fixedContent = applyRegexFix(content)
// Fix the problematic regex pattern - add proper grouping to fix operator precedence
content = content.replace(/\/\^text\\?\/\|charset=utf-8\$?\//g, '/^(text\\/|charset=utf-8)$/')
content = content.replace(/\/\^text\/\|charset=utf-8\$?\//g, '/^(text\\/|charset=utf-8)$/')
if (content !== originalContent) {
fs.writeFileSync(filePath, content, 'utf8')
process.stdout.write(`✅ Fixed: ${filePath}\n`)
filesFixed++
if (fixedContent !== originalContent) {
fs.writeFileSync(filePath, fixedContent, 'utf8')
return { fixed: true, error: null }
} else {
process.stdout.write(` No changes needed: ${filePath}\n`)
return { fixed: false, error: null }
}
} catch (error) {
if (error.code === 'ENOENT') {
process.stdout.write(`⚠️ File not found: ${filePath}\n`)
} else {
process.stderr.write(`❌ Error fixing ${filePath}: ${error.message}\n`)
}
return { fixed: false, error: error.message }
}
}
process.stdout.write(`\n🎉 Fix complete! ${filesFixed} files updated.\n`)
if (filesFixed > 0) {
process.stdout.write('Run "npm run build:package" to rebuild with the fix.\n')
/**
* Main function to fix all files
* @param {string[]} files - Array of file paths to fix
* @returns {{filesFixed: number, results: Array}} - Summary of fix operations
*/
function fixAllFiles(files = filesToFix) {
const results = []
let filesFixed = 0
for (const filePath of files) {
const result = fixFile(filePath)
results.push({ filePath, ...result })
if (result.fixed) {
filesFixed++
}
}
return { filesFixed, results }
}
// Main execution when run as script
if (require.main === module) {
process.stdout.write('🔧 Applying regex fix for @octokit/request...\n')
const { filesFixed, results } = fixAllFiles()
for (const result of results) {
if (result.error) {
if (result.error.includes('ENOENT') || result.error.includes('no such file')) {
process.stdout.write(`⚠️ File not found: ${result.filePath}\n`)
} else {
process.stderr.write(`❌ Error fixing ${result.filePath}: ${result.error}\n`)
}
} else if (result.fixed) {
process.stdout.write(`✅ Fixed: ${result.filePath}\n`)
} else {
process.stdout.write(` No changes needed: ${result.filePath}\n`)
}
}
process.stdout.write(`\n🎉 Fix complete! ${filesFixed} files updated.\n`)
if (filesFixed > 0) {
process.stdout.write('Run "npm run build:package" to rebuild with the fix.\n')
}
}
// Export functions for testing
module.exports = {
applyRegexFix,
fixFile,
fixAllFiles
}