Skip to content

Commit

Permalink
Merge pull request #5 from le-yams/feat/ctrf_for_custom_reporter
Browse files Browse the repository at this point in the history
feat: enable ctrf for custom reporter (for any tool)
  • Loading branch information
Ma11hewThomas authored May 29, 2024
2 parents 8870365 + f03a890 commit 915eaa5
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 131 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Build

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:


jobs:
test:
strategy:
matrix:
go-version: [ 1.19.x, 1.20.x, 1.21.x ]
lint-and-coverage: [ false ]
include:
- go-version: 1.22.x
lint-and-coverage: true

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Install dependencies
run: go get ./...

- name: Build
run: go build -v ./...

- name: Test with the Go CLI
run: |
go version
if [ ${{ matrix.lint-and-coverage }} = true ]; then
GO_TEST_OPTS="-covermode=atomic -coverprofile=coverage.out"
fi
export GORACE="halt_on_error=1"
go test -race $GO_TEST_OPTS ./...
- name: Reporting coverage
if: matrix.lint-and-coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

.idea
*.iml
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Go JSON Reporter
# Go CTRF JSON format support

## Go JSON Reporter


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

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

If you find this project useful, consider giving it a GitHub star ⭐ It means a lot to us.


## Generate a CTRF JSON report in your own testing tool written in go

If you are writting your own testing tool and wish to generate a CTRF JSON report, you can use the `ctrf` package.

```go
import (
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
)

func runTests(destinationReportFile string) error {
env := ctrf.Environment{
// add your environment details here
}
report := ctrf.NewReport("my-awesome-testing-tool", &env)

// run your tests and populate the report object here

return report.WriteFile(destinationReportFile)
}

```
111 changes: 56 additions & 55 deletions cmd/go-ctrf-json-reporter/main.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
package main

import (
"flag"
"fmt"
"github.com/ctrf-io/go-ctrf-json-reporter/reporter"
"os"
"flag"
"fmt"
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
"github.com/ctrf-io/go-ctrf-json-reporter/reporter"
"os"
)

func main() {
var outputFile string
var verbose bool
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
flag.BoolVar(&verbose, "v", false, "Enable verbose output (shorthand)")
flag.StringVar(&outputFile, "output", "ctrf-report.json", "The output file for the test results")
flag.StringVar(&outputFile, "o", "ctrf-report.json", "The output file for the test results (shorthand)")
var outputFile string
var verbose bool
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
flag.BoolVar(&verbose, "v", false, "Enable verbose output (shorthand)")
flag.StringVar(&outputFile, "output", "ctrf-report.json", "The output file for the test results")
flag.StringVar(&outputFile, "o", "ctrf-report.json", "The output file for the test results (shorthand)")

var tempAppName, tempAppVersion, tempOSPlatform, tempOSRelease, tempOSVersion, tempBuildName, tempBuildNumber string
var tempAppName, tempAppVersion, tempOSPlatform, tempOSRelease, tempOSVersion, tempBuildName, tempBuildNumber string

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

flag.Parse()
flag.Parse()

var env *reporter.Environment
var env *ctrf.Environment

if tempAppName != "" || tempAppVersion != "" || tempOSPlatform != "" ||
tempOSRelease != "" || tempOSVersion != "" || tempBuildName != "" || tempBuildNumber != "" {
env = &reporter.Environment{}
if tempAppName != "" || tempAppVersion != "" || tempOSPlatform != "" ||
tempOSRelease != "" || tempOSVersion != "" || tempBuildName != "" || tempBuildNumber != "" {
env = &ctrf.Environment{}

if tempAppName != "" {
env.AppName = &tempAppName
}
if tempAppVersion != "" {
env.AppVersion = &tempAppVersion
}
if tempOSPlatform != "" {
env.OSPlatform = &tempOSPlatform
}
if tempOSRelease != "" {
env.OSRelease = &tempOSRelease
}
if tempOSVersion != "" {
env.OSVersion = &tempOSVersion
}
if tempBuildName != "" {
env.BuildName = &tempBuildName
}
if tempBuildNumber != "" {
env.BuildNumber = &tempBuildNumber
}
}
if tempAppName != "" {
env.AppName = tempAppName
}
if tempAppVersion != "" {
env.AppVersion = tempAppVersion
}
if tempOSPlatform != "" {
env.OSPlatform = tempOSPlatform
}
if tempOSRelease != "" {
env.OSRelease = tempOSRelease
}
if tempOSVersion != "" {
env.OSVersion = tempOSVersion
}
if tempBuildName != "" {
env.BuildName = tempBuildName
}
if tempBuildNumber != "" {
env.BuildNumber = tempBuildNumber
}
}

report, err := reporter.ParseTestResults(os.Stdin, verbose, env)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
os.Exit(1)
}
report, err := reporter.ParseTestResults(os.Stdin, verbose, env)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error parsing test results: %v\n", err)
os.Exit(1)
}

err = reporter.WriteReportToFile(outputFile, report)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
os.Exit(1)
}
err = reporter.WriteReportToFile(outputFile, report)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
os.Exit(1)
}
}
Loading

0 comments on commit 915eaa5

Please sign in to comment.