|
| 1 | +import { execSync } from 'child_process' |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +const targetBranch = process.env.DRONE_TARGET_BRANCH || 'master' |
| 6 | + |
| 7 | +// INFO: 1 and 2 elements are node and script name respectively |
| 8 | +const scriptDir = path.dirname(process.argv[1]) |
| 9 | +const suitesToCheck = process.argv[2] |
| 10 | + |
| 11 | +const pacToTests = { |
| 12 | + 'web-app-activities': [], |
| 13 | + 'web-app-admin-settings': ['admin-settings', 'keycloak', 'smoke', 'user-settings'], |
| 14 | + 'web-app-app-store': ['app-store'], |
| 15 | + 'web-app-epub-reader': [], |
| 16 | + 'web-app-external': ['app-provider'], |
| 17 | + 'web-app-files': [ |
| 18 | + 'a11y', |
| 19 | + 'admin-settings', |
| 20 | + 'app-provider', |
| 21 | + 'app-store', |
| 22 | + 'file-action', |
| 23 | + 'journeys', |
| 24 | + 'keycloak', |
| 25 | + 'navigation', |
| 26 | + 'ocm', |
| 27 | + 'oidc', |
| 28 | + 'search', |
| 29 | + 'shares', |
| 30 | + 'smoke', |
| 31 | + 'spaces', |
| 32 | + 'user-settings' |
| 33 | + ], |
| 34 | + 'web-app-ocm': ['ocm'], |
| 35 | + 'web-app-password-protected-folders': ['file-action'], |
| 36 | + 'web-app-pdf-viewer': [], |
| 37 | + 'web-app-preview': ['spaces', 'shares', 'file-action', 'navigation', 'ocm'], |
| 38 | + 'web-app-search': [ |
| 39 | + 'a11y', |
| 40 | + 'admin-settings', |
| 41 | + 'app-provider', |
| 42 | + 'app-store', |
| 43 | + 'file-action', |
| 44 | + 'journeys', |
| 45 | + 'keycloak', |
| 46 | + 'navigation', |
| 47 | + 'ocm', |
| 48 | + 'oidc', |
| 49 | + 'search', |
| 50 | + 'shares', |
| 51 | + 'smoke', |
| 52 | + 'spaces', |
| 53 | + 'user-settings' |
| 54 | + ], |
| 55 | + 'web-app-text-editor': ['keycloak', 'oidc'], |
| 56 | + 'web-app-webfinger': [] |
| 57 | +} |
| 58 | + |
| 59 | +// get test suites |
| 60 | +const testSuitesDir = `${scriptDir}/../e2e/cucumber/features` |
| 61 | +const testSuites = fs |
| 62 | + .readdirSync(testSuitesDir) |
| 63 | + .filter((entry) => fs.statSync(path.join(testSuitesDir, entry)).isDirectory()) |
| 64 | + |
| 65 | +function getChangedFiles() { |
| 66 | + const changedFiles = execSync(`git diff --name-only origin/${targetBranch} HEAD`).toString() |
| 67 | + console.log('[INFO] Changed files:\n', changedFiles) |
| 68 | + return [...new Set([...changedFiles.split('\n')])].filter((file) => file) |
| 69 | +} |
| 70 | + |
| 71 | +function getPackageFromFile(file) { |
| 72 | + if (!file.startsWith('packages/')) { |
| 73 | + return |
| 74 | + } |
| 75 | + const packages = Object.keys(pacToTests) |
| 76 | + for (const pkg of packages) { |
| 77 | + if (file.startsWith(`packages/${pkg}`)) { |
| 78 | + return pkg |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function getAffectedTestSuites(changedFiles) { |
| 84 | + const affectedSuites = new Set() |
| 85 | + for (const file of changedFiles) { |
| 86 | + if ( |
| 87 | + file.startsWith('tests/e2e/') || |
| 88 | + file.startsWith('tests/drone/') || |
| 89 | + file === '.drone.star' || |
| 90 | + file === 'package.json' |
| 91 | + ) { |
| 92 | + // run all test suites |
| 93 | + return testSuites |
| 94 | + } |
| 95 | + const packageName = getPackageFromFile(file) |
| 96 | + if (packageName && packageName in pacToTests) { |
| 97 | + pacToTests[packageName].forEach((suite) => affectedSuites.add(suite)) |
| 98 | + } |
| 99 | + } |
| 100 | + return Array.from(affectedSuites) |
| 101 | +} |
| 102 | + |
| 103 | +function createSuitesToRunEnvFile(suites = []) { |
| 104 | + const envContent = ['TEST_SUITES', suites.join(',')] |
| 105 | + if (suites[0].startsWith('cucumber/')) { |
| 106 | + envContent[0] = ['FEATURE_FILES'] |
| 107 | + } |
| 108 | + // create suites.env file in the same directory as the script |
| 109 | + fs.writeFileSync(`${scriptDir}/suites.env`, envContent.join('=')) |
| 110 | +} |
| 111 | + |
| 112 | +function main() { |
| 113 | + const changedFiles = getChangedFiles() |
| 114 | + if (changedFiles.length === 0) { |
| 115 | + console.log('[INFO] No changes detected.') |
| 116 | + process.exit(78) // Skip the pipeline |
| 117 | + } |
| 118 | + const affectedTestSuites = getAffectedTestSuites(changedFiles) |
| 119 | + if (affectedTestSuites.length === 0) { |
| 120 | + console.log('[INFO] No affected test suites to run.') |
| 121 | + process.exit(78) // Skip the pipeline |
| 122 | + } |
| 123 | + if (suitesToCheck) { |
| 124 | + const suitesToRun = suitesToCheck.split(',').filter((suite) => { |
| 125 | + suite = suite.trim() |
| 126 | + if (suite.startsWith('cucumber/')) { |
| 127 | + suite = suite.replace('cucumber/features/', '').split('/').shift() |
| 128 | + } |
| 129 | + return affectedTestSuites.includes(suite) |
| 130 | + }) |
| 131 | + if (suitesToRun.length === 0) { |
| 132 | + console.log( |
| 133 | + '[INFO] The following test suites are not affected and will be skipped:', |
| 134 | + suitesToCheck.split(',').join('\n') |
| 135 | + ) |
| 136 | + process.exit(78) // Skip the pipeline |
| 137 | + } |
| 138 | + createSuitesToRunEnvFile(suitesToRun) |
| 139 | + return |
| 140 | + } |
| 141 | + createSuitesToRunEnvFile(affectedTestSuites) |
| 142 | +} |
| 143 | + |
| 144 | +main() |
0 commit comments