Skip to content

Commit 915eaa5

Browse files
Merge pull request #5 from le-yams/feat/ctrf_for_custom_reporter
feat: enable ctrf for custom reporter (for any tool)
2 parents 8870365 + f03a890 commit 915eaa5

File tree

11 files changed

+707
-131
lines changed

11 files changed

+707
-131
lines changed

.github/workflows/build.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
11+
jobs:
12+
test:
13+
strategy:
14+
matrix:
15+
go-version: [ 1.19.x, 1.20.x, 1.21.x ]
16+
lint-and-coverage: [ false ]
17+
include:
18+
- go-version: 1.22.x
19+
lint-and-coverage: true
20+
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Setup Go ${{ matrix.go-version }}
28+
uses: actions/setup-go@v5
29+
with:
30+
go-version: ${{ matrix.go-version }}
31+
32+
- name: Install dependencies
33+
run: go get ./...
34+
35+
- name: Build
36+
run: go build -v ./...
37+
38+
- name: Test with the Go CLI
39+
run: |
40+
go version
41+
if [ ${{ matrix.lint-and-coverage }} = true ]; then
42+
GO_TEST_OPTS="-covermode=atomic -coverprofile=coverage.out"
43+
fi
44+
export GORACE="halt_on_error=1"
45+
go test -race $GO_TEST_OPTS ./...
46+
47+
- name: Reporting coverage
48+
if: matrix.lint-and-coverage
49+
uses: shogo82148/actions-goveralls@v1
50+
with:
51+
path-to-profile: coverage.out

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
.idea
3+
*.iml

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Go JSON Reporter
1+
# Go CTRF JSON format support
2+
3+
## Go JSON Reporter
4+
25

36
A Go JSON test reporter to create test reports that follow the CTRF standard.
47

@@ -115,3 +118,26 @@ When running go-ctrf-json-reporter results in a "command not found" error this u
115118
## Support Us
116119

117120
If you find this project useful, consider giving it a GitHub star ⭐ It means a lot to us.
121+
122+
123+
## Generate a CTRF JSON report in your own testing tool written in go
124+
125+
If you are writting your own testing tool and wish to generate a CTRF JSON report, you can use the `ctrf` package.
126+
127+
```go
128+
import (
129+
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
130+
)
131+
132+
func runTests(destinationReportFile string) error {
133+
env := ctrf.Environment{
134+
// add your environment details here
135+
}
136+
report := ctrf.NewReport("my-awesome-testing-tool", &env)
137+
138+
// run your tests and populate the report object here
139+
140+
return report.WriteFile(destinationReportFile)
141+
}
142+
143+
```

cmd/go-ctrf-json-reporter/main.go

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,71 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
6-
"github.com/ctrf-io/go-ctrf-json-reporter/reporter"
7-
"os"
4+
"flag"
5+
"fmt"
6+
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
7+
"github.com/ctrf-io/go-ctrf-json-reporter/reporter"
8+
"os"
89
)
910

1011
func main() {
11-
var outputFile string
12-
var verbose bool
13-
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
14-
flag.BoolVar(&verbose, "v", false, "Enable verbose output (shorthand)")
15-
flag.StringVar(&outputFile, "output", "ctrf-report.json", "The output file for the test results")
16-
flag.StringVar(&outputFile, "o", "ctrf-report.json", "The output file for the test results (shorthand)")
12+
var outputFile string
13+
var verbose bool
14+
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
15+
flag.BoolVar(&verbose, "v", false, "Enable verbose output (shorthand)")
16+
flag.StringVar(&outputFile, "output", "ctrf-report.json", "The output file for the test results")
17+
flag.StringVar(&outputFile, "o", "ctrf-report.json", "The output file for the test results (shorthand)")
1718

18-
var tempAppName, tempAppVersion, tempOSPlatform, tempOSRelease, tempOSVersion, tempBuildName, tempBuildNumber string
19+
var tempAppName, tempAppVersion, tempOSPlatform, tempOSRelease, tempOSVersion, tempBuildName, tempBuildNumber string
1920

20-
flag.StringVar(&tempAppName, "appName", "", "The name of the application being tested.")
21-
flag.StringVar(&tempAppVersion, "appVersion", "", "The version of the application being tested.")
22-
flag.StringVar(&tempOSPlatform, "osPlatform", "", "The operating system platform (e.g., Windows, Linux).")
23-
flag.StringVar(&tempOSRelease, "osRelease", "", "The release version of the operating system.")
24-
flag.StringVar(&tempOSVersion, "osVersion", "", "The version number of the operating system.")
25-
flag.StringVar(&tempBuildName, "buildName", "", "The name of the build (e.g., feature branch name).")
26-
flag.StringVar(&tempBuildNumber, "buildNumber", "", "The build number or identifier.")
21+
flag.StringVar(&tempAppName, "appName", "", "The name of the application being tested.")
22+
flag.StringVar(&tempAppVersion, "appVersion", "", "The version of the application being tested.")
23+
flag.StringVar(&tempOSPlatform, "osPlatform", "", "The operating system platform (e.g., Windows, Linux).")
24+
flag.StringVar(&tempOSRelease, "osRelease", "", "The release version of the operating system.")
25+
flag.StringVar(&tempOSVersion, "osVersion", "", "The version number of the operating system.")
26+
flag.StringVar(&tempBuildName, "buildName", "", "The name of the build (e.g., feature branch name).")
27+
flag.StringVar(&tempBuildNumber, "buildNumber", "", "The build number or identifier.")
2728

28-
flag.Parse()
29+
flag.Parse()
2930

30-
var env *reporter.Environment
31+
var env *ctrf.Environment
3132

32-
if tempAppName != "" || tempAppVersion != "" || tempOSPlatform != "" ||
33-
tempOSRelease != "" || tempOSVersion != "" || tempBuildName != "" || tempBuildNumber != "" {
34-
env = &reporter.Environment{}
33+
if tempAppName != "" || tempAppVersion != "" || tempOSPlatform != "" ||
34+
tempOSRelease != "" || tempOSVersion != "" || tempBuildName != "" || tempBuildNumber != "" {
35+
env = &ctrf.Environment{}
3536

36-
if tempAppName != "" {
37-
env.AppName = &tempAppName
38-
}
39-
if tempAppVersion != "" {
40-
env.AppVersion = &tempAppVersion
41-
}
42-
if tempOSPlatform != "" {
43-
env.OSPlatform = &tempOSPlatform
44-
}
45-
if tempOSRelease != "" {
46-
env.OSRelease = &tempOSRelease
47-
}
48-
if tempOSVersion != "" {
49-
env.OSVersion = &tempOSVersion
50-
}
51-
if tempBuildName != "" {
52-
env.BuildName = &tempBuildName
53-
}
54-
if tempBuildNumber != "" {
55-
env.BuildNumber = &tempBuildNumber
56-
}
57-
}
37+
if tempAppName != "" {
38+
env.AppName = tempAppName
39+
}
40+
if tempAppVersion != "" {
41+
env.AppVersion = tempAppVersion
42+
}
43+
if tempOSPlatform != "" {
44+
env.OSPlatform = tempOSPlatform
45+
}
46+
if tempOSRelease != "" {
47+
env.OSRelease = tempOSRelease
48+
}
49+
if tempOSVersion != "" {
50+
env.OSVersion = tempOSVersion
51+
}
52+
if tempBuildName != "" {
53+
env.BuildName = tempBuildName
54+
}
55+
if tempBuildNumber != "" {
56+
env.BuildNumber = tempBuildNumber
57+
}
58+
}
5859

59-
report, err := reporter.ParseTestResults(os.Stdin, verbose, env)
60-
if err != nil {
61-
fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
62-
os.Exit(1)
63-
}
60+
report, err := reporter.ParseTestResults(os.Stdin, verbose, env)
61+
if err != nil {
62+
_, _ = fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
63+
os.Exit(1)
64+
}
6465

65-
err = reporter.WriteReportToFile(outputFile, report)
66-
if err != nil {
67-
fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
68-
os.Exit(1)
69-
}
66+
err = reporter.WriteReportToFile(outputFile, report)
67+
if err != nil {
68+
_, _ = fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
69+
os.Exit(1)
70+
}
7071
}

0 commit comments

Comments
 (0)