|
| 1 | +// |
| 2 | +// This script is used by pr.yml to determine if a domain must be tested based |
| 3 | +// on the files modified in the pull request. |
| 4 | +// |
| 5 | + |
| 6 | +// Given a domain name and set of files, return true if the domain should be |
| 7 | +// tested |
| 8 | +function matchesPattern(domain, filePaths) { |
| 9 | + // filter files that end in .md |
| 10 | + filePaths = filePaths.filter( |
| 11 | + (filePath) => |
| 12 | + !filePath.endsWith(".md") && |
| 13 | + !filePath.startsWith("docs/") && |
| 14 | + !filePath.startsWith("third-party-programs/"), |
| 15 | + ); |
| 16 | + // These directories contain domain specific code |
| 17 | + const dirs = "(tests/unit_tests|examples|src|include/oneapi/mkl)"; |
| 18 | + const domains = "(blas|lapack|rng|dft)"; |
| 19 | + // matches changes to the domain of interest or non domain-specific code |
| 20 | + const re = new RegExp(`^(${dirs}/${domain}|(?!${dirs}/${domains}))`); |
| 21 | + const match = filePaths.some((filePath) => re.test(filePath)); |
| 22 | + return match; |
| 23 | +} |
| 24 | + |
| 25 | +// Return the list of files modified in the pull request |
| 26 | +async function prFiles(github, context) { |
| 27 | + const response = await github.rest.pulls.listFiles({ |
| 28 | + owner: context.repo.owner, |
| 29 | + repo: context.repo.repo, |
| 30 | + pull_number: context.payload.pull_request.number, |
| 31 | + }); |
| 32 | + const prFiles = response.data.map((file) => file.filename); |
| 33 | + return prFiles; |
| 34 | +} |
| 35 | + |
| 36 | +// Called by pr.yml. See: |
| 37 | +// https://github.com/actions/github-script/blob/main/README.md for more |
| 38 | +// information on the github and context parameters |
| 39 | +module.exports = async ({ github, context, domain }) => { |
| 40 | + if (!context.payload.pull_request) { |
| 41 | + console.log("Not a pull request. Testing all domains."); |
| 42 | + return true; |
| 43 | + } |
| 44 | + const files = await prFiles(github, context); |
| 45 | + const match = matchesPattern(domain, files); |
| 46 | + console.log("Domain: ", domain); |
| 47 | + console.log("PR files: ", files); |
| 48 | + console.log("Match: ", match); |
| 49 | + return match; |
| 50 | +}; |
| 51 | + |
| 52 | +// |
| 53 | +// Test the matchesPattern function |
| 54 | +// |
| 55 | +// Run this script with `node domain-check.js` It should exit with code 0 if |
| 56 | +// all tests pass. |
| 57 | +// |
| 58 | +// If you need to change the set of files that are ignored, add a test pattern |
| 59 | +// below with positive and negative examples. It is also possible to test by |
| 60 | +// setting up a fork and then submitting pull requests that modify files, but |
| 61 | +// it requires a lot of manual work. |
| 62 | +// |
| 63 | +test_patterns = [ |
| 64 | + { |
| 65 | + domain: "blas", |
| 66 | + files: ["tests/unit_tests/blas/test_blas.cpp"], |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + domain: "rng", |
| 71 | + files: ["examples/rng/example_rng.cpp"], |
| 72 | + expected: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + domain: "lapack", |
| 76 | + files: ["include/oneapi/mkl/lapack/lapack.hpp"], |
| 77 | + expected: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + domain: "dft", |
| 81 | + files: ["src/dft/lapack.hpp"], |
| 82 | + expected: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + domain: "dft", |
| 86 | + files: ["src/dft/lapack.md"], |
| 87 | + expected: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + domain: "blas", |
| 91 | + files: ["tests/unit_tests/dft/test_blas.cpp"], |
| 92 | + expected: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + domain: "rng", |
| 96 | + files: ["examples/blas/example_rng.cpp"], |
| 97 | + expected: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + domain: "lapack", |
| 101 | + files: ["include/oneapi/mkl/rng/lapack.hpp"], |
| 102 | + expected: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + domain: "dft", |
| 106 | + files: ["src/lapack/lapack.hpp"], |
| 107 | + expected: false, |
| 108 | + }, |
| 109 | + { |
| 110 | + domain: "dft", |
| 111 | + files: ["docs/dft/dft.rst"], |
| 112 | + expected: false, |
| 113 | + }, |
| 114 | + { |
| 115 | + domain: "dft", |
| 116 | + files: ["third-party-programs/dft/dft.rst"], |
| 117 | + expected: false, |
| 118 | + }, |
| 119 | +]; |
| 120 | + |
| 121 | +function testPattern(test) { |
| 122 | + const result = matchesPattern(test.domain, test.files); |
| 123 | + if (result !== test.expected) { |
| 124 | + console.log("Fail:"); |
| 125 | + console.log(" domain:", test.domain); |
| 126 | + console.log(" files:", test.files); |
| 127 | + console.log(" expected:", test.expected); |
| 128 | + console.log(" result:", result); |
| 129 | + process.exit(1); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +if (require.main === module) { |
| 134 | + // invoke test for each test pattern |
| 135 | + test_patterns.forEach(testPattern); |
| 136 | + console.log("All tests pass"); |
| 137 | +} |
0 commit comments