Skip to content

Commit e05dfdb

Browse files
committed
ci: add comprehensive testing infrastructure
- Add unit test workflow for push/PR events - Create test-action workflow for manual integration testing - Include build and package testing - Add local test script for developer convenience - Update DEVELOPMENT.md with new testing instructions - Introduce TypeScript type tests - Add unit tests for main action logic Signed-off-by: diverger <[email protected]>
1 parent 0e9a218 commit e05dfdb

File tree

6 files changed

+970
-6
lines changed

6 files changed

+970
-6
lines changed

.github/workflows/test-action.yml

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
name: Test OSS Helper
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
test_type:
7+
description: 'Type of test to run'
8+
required: true
9+
type: choice
10+
options:
11+
- 'basic-upload'
12+
- 'multiple-files'
13+
- 'directory-upload'
14+
- 'advanced-options'
15+
- 'dry-run'
16+
default: 'dry-run'
17+
region:
18+
description: 'OSS Region (e.g., oss-cn-hangzhou)'
19+
required: false
20+
default: 'oss-cn-hangzhou'
21+
bucket:
22+
description: 'OSS Bucket Name'
23+
required: false
24+
default: 'test-bucket'
25+
custom_assets:
26+
description: 'Custom assets (optional, for advanced testing)'
27+
required: false
28+
default: ''
29+
30+
jobs:
31+
test:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v4
36+
37+
- name: Setup Node.js
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: '20'
41+
42+
- name: Create test files
43+
run: |
44+
# Create test directory structure
45+
mkdir -p test-files/{docs,images,config}
46+
47+
# Create various test files
48+
echo "# Test README" > test-files/README.md
49+
echo "Test content for documentation" > test-files/docs/guide.md
50+
echo "Configuration file content" > test-files/config/app.json
51+
echo "Binary-like content" > test-files/images/logo.png
52+
53+
# Create a larger file for testing
54+
head -c 1024 /dev/urandom > test-files/large-file.bin
55+
56+
# Create files with special characters
57+
echo "Unicode test 中文测试" > "test-files/unicode-测试.txt"
58+
echo "Spaces in filename" > "test-files/file with spaces.txt"
59+
60+
echo "📁 Created test files:"
61+
find test-files -type f -exec ls -lh {} \;
62+
63+
- name: Test - Dry Run
64+
if: inputs.test_type == 'dry-run'
65+
uses: ./
66+
with:
67+
region: ${{ inputs.region }}
68+
key-id: 'test-key-id'
69+
key-secret: 'test-key-secret'
70+
bucket: ${{ inputs.bucket }}
71+
assets: |
72+
test-files/README.md:docs/readme.md
73+
timeout: 60
74+
continue-on-error: true
75+
env:
76+
# This will fail but test the action logic
77+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
79+
- name: Test - Basic Upload (Dry Run)
80+
if: inputs.test_type == 'basic-upload'
81+
uses: ./
82+
with:
83+
region: ${{ inputs.region }}
84+
key-id: ${{ secrets.OSS_KEY_ID || 'test-key' }}
85+
key-secret: ${{ secrets.OSS_KEY_SECRET || 'test-secret' }}
86+
bucket: ${{ inputs.bucket }}
87+
assets: |
88+
test-files/README.md:docs/readme.md
89+
timeout: 120
90+
continue-on-error: true
91+
92+
- name: Test - Multiple Files
93+
if: inputs.test_type == 'multiple-files'
94+
uses: ./
95+
with:
96+
region: ${{ inputs.region }}
97+
key-id: ${{ secrets.OSS_KEY_ID || 'test-key' }}
98+
key-secret: ${{ secrets.OSS_KEY_SECRET || 'test-secret' }}
99+
bucket: ${{ inputs.bucket }}
100+
assets: |
101+
test-files/README.md:docs/readme.md
102+
test-files/docs/guide.md:documentation/guide.md
103+
test-files/config/app.json:config/application.json
104+
timeout: 180
105+
continue-on-error: true
106+
107+
- name: Test - Directory Upload
108+
if: inputs.test_type == 'directory-upload'
109+
uses: ./
110+
with:
111+
region: ${{ inputs.region }}
112+
key-id: ${{ secrets.OSS_KEY_ID || 'test-key' }}
113+
key-secret: ${{ secrets.OSS_KEY_SECRET || 'test-secret' }}
114+
bucket: ${{ inputs.bucket }}
115+
assets: |
116+
test-files/:website/
117+
timeout: 180
118+
continue-on-error: true
119+
120+
- name: Test - Advanced Options
121+
if: inputs.test_type == 'advanced-options'
122+
uses: ./
123+
with:
124+
region: ${{ inputs.region }}
125+
key-id: ${{ secrets.OSS_KEY_ID || 'test-key' }}
126+
key-secret: ${{ secrets.OSS_KEY_SECRET || 'test-secret' }}
127+
bucket: ${{ inputs.bucket }}
128+
assets: |
129+
test-files/README.md:docs/readme.md
130+
test-files/large-file.bin:files/large.bin
131+
timeout: 300
132+
max-retries: 5
133+
enable-gzip: true
134+
public-read: true
135+
headers: '{"Cache-Control":"max-age=3600","Content-Type":"text/html"}'
136+
continue-on-error: true
137+
138+
- name: Test - Custom Assets
139+
if: inputs.custom_assets != ''
140+
uses: ./
141+
with:
142+
region: ${{ inputs.region }}
143+
key-id: ${{ secrets.OSS_KEY_ID || 'test-key' }}
144+
key-secret: ${{ secrets.OSS_KEY_SECRET || 'test-secret' }}
145+
bucket: ${{ inputs.bucket }}
146+
assets: ${{ inputs.custom_assets }}
147+
timeout: 180
148+
continue-on-error: true
149+
150+
- name: Display Results
151+
if: always()
152+
run: |
153+
echo "🧪 Test completed!"
154+
echo "📊 Test type: ${{ inputs.test_type }}"
155+
echo "🌏 Region: ${{ inputs.region }}"
156+
echo "🪣 Bucket: ${{ inputs.bucket }}"
157+
158+
if [ -n "${{ inputs.custom_assets }}" ]; then
159+
echo "📋 Custom assets: ${{ inputs.custom_assets }}"
160+
fi
161+
162+
echo ""
163+
echo "💡 To test with real credentials:"
164+
echo "1. Add OSS_KEY_ID and OSS_KEY_SECRET to repository secrets"
165+
echo "2. Use a real bucket name"
166+
echo "3. Re-run this workflow"
167+
echo ""
168+
echo "🔍 Check the action logs above for detailed results"

.github/workflows/test.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: [ main, master, develop ]
6+
pull_request:
7+
branches: [ main, master ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
node-version: [18, 20]
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
cache: 'npm'
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run linting
31+
run: npm run lint
32+
33+
- name: Run TypeScript check
34+
run: npm run check
35+
36+
- name: Run unit tests
37+
run: npm test
38+
39+
- name: Run tests with coverage
40+
if: matrix.node-version == 20
41+
run: npm run test:coverage
42+
43+
- name: Build action
44+
run: npm run build
45+
46+
- name: Verify build output
47+
run: |
48+
if [ ! -f "dist/index.js" ]; then
49+
echo "❌ Build failed: dist/index.js not found"
50+
exit 1
51+
fi
52+
echo "✅ Build verification passed"
53+
echo "📊 Build size: $(du -h dist/index.js | cut -f1)"
54+
55+
- name: Test action execution (dry run)
56+
run: |
57+
# Set environment variables for testing
58+
export INPUT_REGION="oss-cn-hangzhou"
59+
export INPUT_KEY_ID="test-key-id"
60+
export INPUT_KEY_SECRET="test-key-secret"
61+
export INPUT_BUCKET="test-bucket"
62+
export INPUT_ASSETS="package.json:test.json"
63+
export INPUT_TIMEOUT="60"
64+
export INPUT_CONTINUE_ON_ERROR="true"
65+
66+
# This will test the action's input parsing and initial logic
67+
echo "🧪 Testing action execution (will fail at OSS connection, which is expected)..."
68+
node dist/index.js || echo "Expected failure - no real OSS credentials"
69+
70+
- name: Upload coverage reports
71+
if: matrix.node-version == 20 && github.event_name == 'push'
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: coverage-report
75+
path: coverage/
76+
retention-days: 30

DEVELOPMENT.md

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ The script automatically:
197197
## 🧪 Testing
198198

199199
### Local Testing
200+
201+
#### **Quick Local Tests**
202+
```bash
203+
# Run comprehensive local test suite
204+
./test-local.sh
205+
206+
# Run specific tests
207+
./test-local.sh unit # Unit tests only
208+
./test-local.sh build # Build test only
209+
./test-local.sh action # Action execution test
210+
./test-local.sh ts # TypeScript check only
211+
./test-local.sh lint # Linting only
212+
```
213+
214+
#### **Manual Testing**
200215
```bash
201216
# Run all tests
202217
npm test
@@ -206,11 +221,35 @@ npm run test:watch
206221

207222
# Run with coverage
208223
npm run test:coverage
224+
225+
# TypeScript compilation check
226+
npm run check
227+
228+
# Build and validate
229+
./build.sh
209230
```
210231

211232
### GitHub Actions Testing
212-
- **Build Tests:** Automated testing of built action
213-
- **Integration Tests:** Test with actual OSS uploads (if configured)
233+
234+
#### **1. Unit Tests** (`test.yml`)
235+
- **Triggers:** Push, PR, manual dispatch
236+
- **Tests:** Unit tests, linting, TypeScript, build validation
237+
- **Matrix:** Node.js 18 and 20
238+
- **Coverage:** Generates coverage reports
239+
240+
#### **2. Integration Testing** (`test-action.yml`)
241+
- **Triggers:** Manual dispatch with options
242+
- **Test Types:**
243+
- `dry-run` - Test with fake credentials (safe)
244+
- `basic-upload` - Single file upload
245+
- `multiple-files` - Multiple file upload
246+
- `directory-upload` - Directory upload
247+
- `advanced-options` - Test all features
248+
- **Configuration:** Choose region, bucket, custom assets
249+
250+
#### **3. Build Tests** (`build-and-package.yml`)
251+
- **Automatic:** Builds on every push/PR
252+
- **Validation:** Ensures action builds correctly
214253

215254
## 📦 Distribution
216255

@@ -312,10 +351,12 @@ git push origin v1.2.0
312351
- Keep DEVELOPMENT.md updated with process changes
313352

314353
### 🧪 **Testing**
315-
- Test with `dry_run: true` first
316-
- Use test workflows for comprehensive validation
317-
- Verify error handling and retry logic
318-
- Test timeout settings work correctly
354+
- **Use local tests**: Run `./test-local.sh` for comprehensive local testing
355+
- **Test before releases**: Use GitHub Actions "Test OSS Helper" workflow
356+
- **Unit test coverage**: Maintain good test coverage for core functions
357+
- **Integration testing**: Test with real OSS credentials in private repos
358+
- **Dry run testing**: Always test with fake credentials first
359+
- **Multiple environments**: Test on different Node.js versions
319360

320361
## 🐛 Troubleshooting
321362

0 commit comments

Comments
 (0)