|
22 | 22 | coverage: "" |
23 | 23 | - scenario: failing-command |
24 | 24 | coverage: "" |
| 25 | + - scenario: failing-tests |
| 26 | + coverage: github |
| 27 | + - scenario: no-reports |
| 28 | + coverage: "" |
25 | 29 | steps: |
26 | 30 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 |
27 | 31 | with: |
@@ -54,40 +58,100 @@ jobs: |
54 | 58 | if (scenario === 'failing-command') { |
55 | 59 | manifest.scripts['test:ci'] = ['@test --log-junit=junit.xml --no-coverage', "@php -r 'exit(7);'"]; |
56 | 60 | } |
| 61 | + if (scenario === 'failing-tests') { |
| 62 | + const test = path.join(directory, 'tests/SampleTest.php'); |
| 63 | + await fs.writeFile(test, (await fs.readFile(test, 'utf8')).replace("'sample'", "'unexpected'")); |
| 64 | + } |
| 65 | + if (scenario === 'no-reports') manifest.scripts['test:ci'] = "@php -r 'exit(0);'"; |
57 | 66 | await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n'); |
| 67 | + await fs.writeFile(path.join(directory, '.private-fixture'), 'Must not be uploaded'); |
58 | 68 | core.setOutput('directory', directory); |
59 | 69 | - uses: $/actions/setup-php |
60 | 70 | with: |
61 | 71 | working-directory: ${{ steps.fixture.outputs.directory }} |
62 | 72 | composer-directories: '["."]' |
63 | | - - name: Act - Run project tests |
| 73 | + - name: Act - Run project tests without comment permission |
64 | 74 | id: tests |
65 | | - continue-on-error: ${{ matrix.scenario == 'failing-command' }} |
| 75 | + continue-on-error: ${{ startsWith(matrix.scenario, 'failing-') }} |
66 | 76 | uses: $/actions/test |
67 | 77 | with: |
68 | 78 | working-directory: ${{ steps.fixture.outputs.directory }} |
69 | 79 | coverage: ${{ matrix.coverage }} |
| 80 | + - name: Assert - Download the native report artifact |
| 81 | + if: steps.tests.outputs.report-artifact-id != '' |
| 82 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 83 | + with: |
| 84 | + artifact-ids: ${{ steps.tests.outputs.report-artifact-id }} |
| 85 | + path: downloaded-reports |
| 86 | + merge-multiple: true |
70 | 87 | - name: Assert - Reports and project preparation |
71 | 88 | uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 |
72 | 89 | env: |
73 | 90 | DIRECTORY: ${{ steps.fixture.outputs.directory }} |
74 | 91 | OUTCOME: ${{ steps.tests.outcome }} |
75 | 92 | SCENARIO: ${{ matrix.scenario }} |
| 93 | + ARTIFACT_ID: ${{ steps.tests.outputs.report-artifact-id }} |
| 94 | + COVERAGE: ${{ matrix.coverage }} |
76 | 95 | with: |
77 | 96 | script: |- |
78 | 97 | const assert = require('node:assert/strict'); |
79 | 98 | const fs = require('node:fs'); |
80 | 99 | const path = require('node:path'); |
81 | 100 | const directory = process.env.DIRECTORY; |
82 | 101 | const scenario = process.env.SCENARIO; |
83 | | - assert.equal(process.env.OUTCOME, scenario === 'failing-command' ? 'failure' : 'success'); |
84 | | - assert.match(fs.readFileSync(path.join(directory, 'junit.xml'), 'utf8'), /<testcase\b/); |
| 102 | + assert.equal(process.env.OUTCOME, scenario.startsWith('failing-') ? 'failure' : 'success'); |
| 103 | + if (scenario === 'no-reports') { |
| 104 | + assert.equal(process.env.ARTIFACT_ID, ''); |
| 105 | + return; |
| 106 | + } |
| 107 | + assert.match(process.env.ARTIFACT_ID, /^\d+$/); |
| 108 | + const files = await (await glob.create('downloaded-reports/**/*', {matchDirectories: false})).glob(); |
| 109 | + assert.deepEqual(files.map(file => path.basename(file)).sort(), process.env.COVERAGE === 'github' ? ['cobertura.xml', 'junit.xml'] : ['junit.xml']); |
| 110 | + for (const file of files) assert.equal(fs.readFileSync(file, 'utf8'), fs.readFileSync(path.join(directory, path.basename(file)), 'utf8')); |
| 111 | + const junit = fs.readFileSync(path.join(directory, 'junit.xml'), 'utf8'); |
| 112 | + assert.match(junit, /<testcase\b/); |
| 113 | + assert.equal(junit.includes('<failure'), scenario === 'failing-tests'); |
85 | 114 | const cobertura = path.join(directory, 'cobertura.xml'); |
86 | | - if (['reports', 'publication-disabled'].includes(scenario)) { |
| 115 | + if (['reports', 'publication-disabled', 'failing-tests'].includes(scenario)) { |
87 | 116 | assert.match(fs.readFileSync(cobertura, 'utf8'), /<coverage\b[^>]*\bline-rate=/); |
88 | 117 | } else { |
89 | 118 | assert.equal(fs.existsSync(cobertura), false, 'Only the Composer script controls report generation.'); |
90 | 119 | } |
91 | 120 | if (scenario === 'preparation') { |
92 | 121 | assert.equal(fs.readFileSync(path.join(directory, 'prepared.txt'), 'utf8'), 'ready'); |
93 | 122 | } |
| 123 | +
|
| 124 | + individual-comment: |
| 125 | + runs-on: ubuntu-latest |
| 126 | + permissions: |
| 127 | + contents: read |
| 128 | + pull-requests: write |
| 129 | + steps: |
| 130 | + - name: Arrange - Checkout |
| 131 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 132 | + with: |
| 133 | + persist-credentials: false |
| 134 | + - name: Arrange - Install the Composer fixture |
| 135 | + uses: $/actions/setup-php |
| 136 | + with: |
| 137 | + working-directory: tests/php-project |
| 138 | + composer-directories: '["."]' |
| 139 | + - name: Act - Publish an individual report |
| 140 | + uses: $/actions/test |
| 141 | + with: |
| 142 | + working-directory: tests/php-project |
| 143 | + comment: "true" |
| 144 | + - name: Assert - One comment with collapsed test and coverage details |
| 145 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 146 | + with: |
| 147 | + script: | |
| 148 | + const assert = require('node:assert/strict'); |
| 149 | + const pr = context.payload.pull_request; |
| 150 | + if (context.eventName !== 'pull_request' || pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) return; |
| 151 | + const comments = await github.paginate(github.rest.issues.listComments, {...context.repo, issue_number: pr.number, per_page: 100}); |
| 152 | + const reports = comments.filter(comment => comment.user.login === 'github-actions[bot]' && comment.body.startsWith('PHP test results\n')); |
| 153 | + assert.equal(reports.length, 1); |
| 154 | + assert.match(reports[0].body, /<details>/); |
| 155 | + assert.match(reports[0].body, /Test Results/); |
| 156 | + assert.match(reports[0].body, /\*\*Tests:\*\* 1 passed · 0 failed/); |
| 157 | + assert.match(reports[0].body, /Coverage/); |
0 commit comments