Skip to content

Commit 4a5fad3

Browse files
committed
feat: add scripts/generate-e2e-matrix
1 parent 5608085 commit 4a5fad3

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Generate E2E test matrix by creating cross-product of locales and shards
5+
*
6+
* Usage:
7+
* node generate-e2e-matrix.js '<locale-matrix-json>' <shard-total>
8+
*
9+
* Example:
10+
* node generate-e2e-matrix.js '[{"locale":"zh-hans","secret_project_id":"VERCEL_PROJECT_ID_ZH_HANS"},{"locale":"zh-hant","secret_project_id":"VERCEL_PROJECT_ID_ZH_HANT"}]' 5
11+
*/
12+
13+
function generateE2EMatrix(localesJson, shardTotal) {
14+
try {
15+
// Parse the locales input
16+
const locales = JSON.parse(localesJson);
17+
const shards = Array.from({ length: shardTotal }, (_, i) => i + 1);
18+
19+
// Generate cross-product of locales and shards
20+
const matrix = [];
21+
22+
for (const locale of locales) {
23+
for (const shard of shards) {
24+
matrix.push({
25+
...locale,
26+
shard: shard,
27+
});
28+
}
29+
}
30+
31+
const result = {
32+
include: matrix,
33+
};
34+
35+
return result;
36+
} catch (error) {
37+
console.error('Error generating matrix:', error.message);
38+
throw error;
39+
}
40+
}
41+
42+
// Main execution
43+
if (require.main === module) {
44+
const args = process.argv.slice(2);
45+
46+
if (args.length !== 2) {
47+
console.error(
48+
'Usage: node generate-e2e-matrix.js <locale-matrix-json> <shard-total>',
49+
);
50+
console.error(
51+
'Example: node generate-e2e-matrix.js \'[{"locale":"zh-hans"}]\' 3',
52+
);
53+
process.exit(1);
54+
}
55+
56+
const [localesJson, shardTotalStr] = args;
57+
const shardTotal = Number.parseInt(shardTotalStr, 10);
58+
59+
if (Number.isNaN(shardTotal) || shardTotal < 1) {
60+
console.error('Error: shard-total must be a positive integer');
61+
process.exit(1);
62+
}
63+
64+
try {
65+
const matrix = generateE2EMatrix(localesJson, shardTotal);
66+
67+
// Output results in GitHub Actions format (same as prerelease matrix script)
68+
console.log(`test-matrix=${JSON.stringify(matrix)}`);
69+
} catch (error) {
70+
console.error('Failed to generate matrix:', error.message);
71+
process.exit(1);
72+
}
73+
}
74+
75+
module.exports = { generateE2EMatrix };

.github/workflows/test-e2e.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,34 @@ on:
1414
default: 3
1515

1616
jobs:
17-
prepare-shards:
17+
prepare-matrix:
1818
runs-on: ubuntu-latest
1919
outputs:
20-
shard-array: ${{ steps.generate-shards.outputs.shard-array }}
20+
test-matrix: ${{ steps.generate-matrix.outputs.test-matrix }}
2121
steps:
22-
- name: Generate shard array
23-
id: generate-shards
22+
- name: Checkout code
23+
uses: actions/checkout@v3
24+
with:
25+
fetch-depth: 1
26+
27+
- name: Setup Node
28+
uses: actions/[email protected]
29+
with:
30+
node-version-file: .nvmrc
31+
32+
- name: Generate test matrix
33+
id: generate-matrix
2434
run: |
25-
shards=$(seq 1 ${{ inputs.shard-total }} | jq -s -c 'map(tonumber)')
26-
echo "shard-array=$shards" >> $GITHUB_OUTPUT
35+
# Use the Node.js script to generate the matrix
36+
output=$(node .github/scripts/generate-e2e-matrix.js '${{ inputs.matrix-include }}' ${{ inputs.shard-total }})
37+
echo "$output" >> $GITHUB_OUTPUT
38+
echo "Generated matrix: $output"
2739
2840
test:
29-
needs: prepare-shards
41+
needs: prepare-matrix
3042
runs-on: ubuntu-latest
3143
strategy:
32-
matrix:
33-
include: ${{ fromJson(inputs.matrix-include) }}
34-
shard: ${{ fromJson(needs.prepare-shards.outputs.shard-array) }}
44+
matrix: ${{ fromJson(needs.prepare-matrix.outputs.test-matrix) }}
3545
name: Test ${{ matrix.locale }} (Shard ${{ matrix.shard }}/${{ inputs.shard-total }})
3646
steps:
3747
- name: Checkout code
@@ -62,6 +72,8 @@ jobs:
6272
pnpm --filter @next-i18n/docs playwright:install
6373
6474
- name: Run E2E Tests
75+
env:
76+
LOCALE: ${{ matrix.locale }}
6577
run: |
6678
echo "Running tests for ${{ matrix.locale }} (Shard ${{ matrix.shard }}/${{ inputs.shard-total }})"
6779
pnpm --filter @next-i18n/docs test:e2e --shard ${{ matrix.shard }}/${{ inputs.shard-total }}

0 commit comments

Comments
 (0)