Skip to content

Commit 037285c

Browse files
author
Jaco Labuschagne
committed
Implement comprehensive testing workflows and GitHub Actions for CI
- Updated README.md to reflect the new testing workflow and continuous integration processes. - Added GitHub Actions workflows for testing, coverage reporting, and security scanning. - Created a pre-commit hook script to ensure tests run before commits. - Introduced a test script for local testing with options for verbosity, race detection, and coverage. - Documented the testing process and guidelines in docs/testing.md, including examples and setup instructions. - Added benchmark tests for performance evaluation in test/samples/benchmarks_test.go.
1 parent 019ec08 commit 037285c

9 files changed

Lines changed: 450 additions & 4 deletions

File tree

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Description
2+
3+
<!-- Provide a brief summary of the changes made in this PR -->
4+
5+
## Type of Change
6+
7+
<!-- Check the appropriate option(s) that best reflect the type of change being made -->
8+
- [ ] Bug fix (non-breaking change which fixes an issue)
9+
- [ ] New feature (non-breaking change which adds functionality)
10+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
11+
- [ ] Documentation update
12+
- [ ] Code refactoring (no functional changes)
13+
- [ ] Dependencies update
14+
15+
## Testing
16+
17+
<!-- Describe the tests that you ran to verify your changes -->
18+
- [ ] I have added unit tests for new functionality
19+
- [ ] I have updated existing tests to cover changes
20+
- [ ] I have run the test script locally (`./scripts/test.sh -a`)
21+
- [ ] All tests pass in the CI pipeline
22+
- [ ] I have added benchmark tests (if appropriate)
23+
24+
## Test Coverage
25+
26+
<!-- Provide information about test coverage -->
27+
- Current coverage: XX%
28+
- Coverage after changes: XX%
29+
30+
## Checklist
31+
32+
<!-- Check items that apply -->
33+
- [ ] My code follows the style guidelines of this project
34+
- [ ] I have performed a self-review of my own code
35+
- [ ] I have commented my code, particularly in hard-to-understand areas
36+
- [ ] I have made corresponding changes to the documentation
37+
- [ ] My changes generate no new warnings or linting errors
38+
- [ ] I have added tests that prove my fix is effective or that my feature works
39+
- [ ] New and existing unit tests pass locally with my changes
40+
- [ ] Any dependent changes have been merged and published
41+
42+
## Additional Notes
43+
44+
<!-- Add any other relevant information about the PR here -->

.github/workflows/coverage.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Coverage
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
coverage:
11+
name: Coverage
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: 1.24.x
18+
19+
- name: Check out code
20+
uses: actions/checkout@v4
21+
22+
- name: Get dependencies
23+
run: go mod download
24+
25+
- name: Generate coverage report
26+
run: go test -coverprofile=coverage.out -covermode=atomic ./...
27+
28+
- name: Upload coverage report to Codecov
29+
uses: codecov/codecov-action@v3
30+
with:
31+
file: ./coverage.out
32+
fail_ci_if_error: false

.github/workflows/security.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Security Scanning
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
schedule:
9+
- cron: '0 0 * * 0' # Run once a week on Sunday at midnight
10+
11+
jobs:
12+
gosec:
13+
name: GoSec Security Scan
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Check out code
17+
uses: actions/checkout@v4
18+
19+
- name: Run Gosec Security Scanner
20+
uses: securego/gosec@master
21+
with:
22+
args: ./...
23+
24+
dependency-scan:
25+
name: Dependency Check
26+
runs-on: ubuntu-latest
27+
steps:
28+
- name: Check out code
29+
uses: actions/checkout@v4
30+
31+
- name: Set up Go
32+
uses: actions/setup-go@v4
33+
with:
34+
go-version: 1.24.x
35+
36+
- name: Nancy
37+
uses: sonatype-nexus-community/nancy-github-action@main
38+
with:
39+
golist-mod: go list -json -m all

.github/workflows/test.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Go Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Test
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: 1.24.x
18+
19+
- name: Check out code
20+
uses: actions/checkout@v4
21+
22+
- name: Get dependencies
23+
run: go mod download
24+
25+
- name: Run tests
26+
run: go test -v ./...
27+
28+
- name: Run tests with race detection
29+
run: go test -race -v ./...
30+
31+
lint:
32+
name: Lint
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Set up Go
36+
uses: actions/setup-go@v4
37+
with:
38+
go-version: 1.24.x
39+
40+
- name: Check out code
41+
uses: actions/checkout@v4
42+
43+
- name: Install golangci-lint
44+
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.0
45+
46+
- name: Run golangci-lint
47+
run: $(go env GOPATH)/bin/golangci-lint run -v

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,40 @@ The library can identify the following statement types:
306306

307307
### Testing
308308

309-
The library includes comprehensive tests:
309+
The project has a comprehensive testing workflow to ensure code quality:
310310

311311
```bash
312-
# Run all tests
312+
# Run the test script with all checks
313+
./scripts/test.sh -a
314+
315+
# Run basic tests
313316
go test ./...
314317

315-
# Run tests for a specific package
316-
go test ./pkg/splitter
318+
# Run tests with race detector
319+
go test -race ./...
317320

318321
# Run tests with coverage
319322
go test -cover ./...
320323
```
321324

325+
For more details about testing, see [docs/testing.md](docs/testing.md).
326+
327+
#### Continuous Integration
328+
329+
This project uses GitHub Actions for continuous integration:
330+
331+
- **Test workflow**: Runs tests and linting on push and pull requests
332+
- **Coverage workflow**: Generates and uploads code coverage reports
333+
- **Security scanning**: Checks for security issues in the code and dependencies
334+
335+
#### Contributing Tests
336+
337+
When adding new features or fixing bugs, please include appropriate tests:
338+
339+
- Add unit tests for new functionality
340+
- Ensure existing tests continue to pass
341+
- Consider adding benchmark tests for performance-critical code
342+
322343
## Future Enhancements
323344

324345
Planned enhancements for future releases:

docs/testing.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Testing Workflow
2+
3+
This document outlines the testing workflow for the go-plsql-statement-splitter project.
4+
5+
## Local Testing
6+
7+
### Running Tests
8+
9+
The project includes a test script that can be used to run tests locally. The script is located at `scripts/test.sh` and supports the following options:
10+
11+
```bash
12+
./scripts/test.sh [flags]
13+
14+
Flags:
15+
-v Verbose mode
16+
-r Run tests with race detection
17+
-c Generate coverage report
18+
-a Run all checks (equivalent to -v -r -c)
19+
```
20+
21+
The script will exit with a non-zero status code if any of the tests fail, making it suitable for use in CI/CD pipelines and Git hooks.
22+
23+
### Examples
24+
25+
Run basic tests:
26+
```bash
27+
./scripts/test.sh
28+
```
29+
30+
Run tests in verbose mode with race detection:
31+
```bash
32+
./scripts/test.sh -v -r
33+
```
34+
35+
Run all checks (tests, race detection, coverage):
36+
```bash
37+
./scripts/test.sh -a
38+
```
39+
40+
### Coverage Reports
41+
42+
To generate a coverage report:
43+
```bash
44+
./scripts/test.sh -c
45+
```
46+
47+
To view the coverage report in your browser:
48+
```bash
49+
go tool cover -html=coverage.out
50+
```
51+
52+
## Git Hooks
53+
54+
The project uses Git hooks to ensure tests pass before commits are made. To set up Git hooks, run:
55+
56+
```bash
57+
./scripts/setup-git-hooks.sh
58+
```
59+
60+
This will install a pre-commit hook that runs tests before each commit. If tests fail, the commit will be aborted.
61+
62+
## Continuous Integration
63+
64+
The project uses GitHub Actions for continuous integration. The following workflows are configured:
65+
66+
1. **Test Workflow** - Runs tests on push to main branch and on pull requests
67+
- Runs standard tests
68+
- Runs tests with race detection
69+
- Performs linting
70+
71+
2. **Coverage Workflow** - Generates code coverage reports
72+
- Uploads coverage reports to Codecov
73+
74+
3. **Security Scanning** - Performs security checks
75+
- Runs gosec to detect security issues
76+
- Scans dependencies for vulnerabilities
77+
78+
## Adding Tests
79+
80+
When adding new features or fixing bugs, please include tests for the new functionality. Tests should be placed in the appropriate package directory with a `_test.go` suffix.
81+
82+
For example:
83+
- Core package tests go in `pkg/splitter/*_test.go`
84+
- Internal tests go in `internal/parser/*_test.go`
85+
- Test samples go in `test/samples/`
86+
87+
## Test Sample Files
88+
89+
SQL sample files for testing are maintained in the `test/samples` directory. If you're adding support for new SQL constructs, please add sample files to test the new functionality.
90+
91+
## Benchmarks
92+
93+
Performance benchmarks are included in the test suite. To run benchmarks:
94+
95+
```bash
96+
go test -bench=. ./...
97+
```
98+
99+
To compare benchmark results before and after changes, you can use the benchcmp tool:
100+
101+
```bash
102+
# Install benchcmp
103+
go install golang.org/x/tools/cmd/benchcmp@latest
104+
105+
# Run benchmarks before changes and save results
106+
go test -bench=. ./... > bench-before.txt
107+
108+
# Make changes, then run benchmarks again
109+
go test -bench=. ./... > bench-after.txt
110+
111+
# Compare results
112+
benchcmp bench-before.txt bench-after.txt
113+
```

scripts/setup-git-hooks.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# Creates git hooks for the project
4+
5+
HOOK_DIR=.git/hooks
6+
PRE_COMMIT=${HOOK_DIR}/pre-commit
7+
8+
# Create hooks directory if it doesn't exist
9+
mkdir -p ${HOOK_DIR}
10+
11+
# Create pre-commit hook
12+
cat > ${PRE_COMMIT} << 'EOF'
13+
#!/bin/bash
14+
15+
# Pre-commit hook for go-plsql-statement-splitter
16+
# This hook runs tests before allowing a commit
17+
18+
echo "Running pre-commit tests..."
19+
20+
# Stash any changes not being committed
21+
git stash -q --keep-index
22+
23+
# Run tests
24+
./scripts/test.sh
25+
26+
# Store the result
27+
RESULT=$?
28+
29+
# Restore stashed changes
30+
git stash pop -q
31+
32+
# Return the test result
33+
if [ $RESULT -ne 0 ]; then
34+
echo "Tests failed. Commit aborted."
35+
exit 1
36+
fi
37+
38+
exit 0
39+
EOF
40+
41+
# Make the pre-commit hook executable
42+
chmod +x ${PRE_COMMIT}
43+
44+
echo "Git hooks installed successfully. Pre-commit hook will run tests before each commit."

0 commit comments

Comments
 (0)