Skip to content

Commit

Permalink
chore(regression): improve coverage with testing tag matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso committed Nov 13, 2024
1 parent 45fd933 commit ba54e8e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
24 changes: 23 additions & 1 deletion .github/workflows/regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,28 @@ on:
- "LICENSE"

jobs:
# Generate matrix of tags for all permutations of the tests
generate-matrix:
runs-on: ubuntu-latest
outputs:
tags: ${{ steps.generate.outputs.tags }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Generate tag combinations
id: generate
run: |
go run mage.go tags-matrix > tags.json
echo "::set-output name=tags::$(cat tags.json)"
shell: bash
test:
needs: generate-matrix
strategy:
matrix:
go-version: [1.22.x, 1.23.x]
os: [ubuntu-latest]
build-flag: ${{ fromJson(needs.generate-matrix.outputs.tags) }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
Expand All @@ -28,7 +45,12 @@ jobs:
go-version: ${{ matrix.go-version }}
cache: true
- name: Tests and coverage
run: go run mage.go coverage
run: |
mkdir build
go test -race -coverprofile=build/${{ matrix.build-flag }}.txt -covermode=atomic -coverpkg=./... ${{ matrix.build-flag }} ./...
go test -race -coverprofile=build/${{ matrix.build-flag }}-examples.txt -covermode=atomic -coverpkg=./... ${{ matrix.build-flag }} ./examples/http-server
go test -coverprofile=build/${{ matrix.build-flag }}-ftw.txt -covermode=atomic -coverpkg=./... ${{ matrix.build-flag }} ./testing/coreruleset
go tool cover -html=build/coverage.txt -o build/coverage.html"
- name: "Codecov: General"
uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4
if: ${{ matrix.go-version == '1.22.x' }}
Expand Down
41 changes: 41 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -228,3 +230,42 @@ func Precommit() error {
func Check() {
mg.SerialDeps(Lint, Test)
}

// combinations generates all possible combinations of build tags
func combinations(tags []string) []string {
var result []string
n := len(tags)
for i := 0; i < (1 << n); i++ {
var combo []string
for j := 0; j < n; j++ {
if i&(1<<j) != 0 {
combo = append(combo, tags[j])
}
}
if len(combo) > 0 {
result = append(result, "-tags="+strings.Join(combo, ","))
} else {
result = append(result, "")
}
}
return result
}

// Generates a JSON output to stdout which contains all permutations of build tags for the project.
func TagsMatrix() error {
tags := []string{
"coraza.rule.case_sensitive_args_keys",
"memoize_builders",
"coraza.rule.multiphase_valuation",
}
combos := combinations(tags)

jsonData, err := json.Marshal(combos)
if err != nil {
fmt.Println("Error generating JSON:", err)
return nil
}

fmt.Println(string(jsonData))
return nil
}

0 comments on commit ba54e8e

Please sign in to comment.