Skip to content

Commit

Permalink
ci-matrix: output packages as a single string
Browse files Browse the repository at this point in the history
So that we don't need to join them in the github action
And so that when we add splitting of tests in a single package we can continue to
use this package field to set the package name.
  • Loading branch information
dnephin committed Sep 5, 2022
1 parent fd06265 commit 8cd52d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
19 changes: 10 additions & 9 deletions cmd/tool/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"time"

"github.com/dnephin/pflag"
Expand Down Expand Up @@ -253,10 +254,10 @@ type matrix struct {
}

type Partition struct {
ID int `json:"id"`
EstimatedRuntime string `json:"estimatedRuntime"`
Packages []string `json:"packages"`
Description string `json:"description"`
ID int `json:"id"`
EstimatedRuntime string `json:"estimatedRuntime"`
Packages string `json:"packages"`
Description string `json:"description"`
}

func writeMatrix(out io.Writer, buckets []bucket) error {
Expand All @@ -265,15 +266,15 @@ func writeMatrix(out io.Writer, buckets []bucket) error {
p := Partition{
ID: i,
EstimatedRuntime: bucket.Total.String(),
Packages: bucket.Packages,
Packages: strings.Join(bucket.Packages, " "),
}
if len(p.Packages) > 0 {
if len(bucket.Packages) > 0 {
var extra string
if len(p.Packages) > 1 {
extra = fmt.Sprintf(" and %d others", len(p.Packages)-1)
if len(bucket.Packages) > 1 {
extra = fmt.Sprintf(" and %d others", len(bucket.Packages)-1)
}
p.Description = fmt.Sprintf("partition %d - package %v%v",
p.ID, testjson.RelativePackagePath(p.Packages[0]), extra)
p.ID, testjson.RelativePackagePath(bucket.Packages[0]), extra)
}

m.Include[i] = p
Expand Down
40 changes: 37 additions & 3 deletions cmd/tool/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package matrix
import (
"bytes"
"encoding/json"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -237,11 +238,44 @@ func TestRun(t *testing.T) {
}
err := run(opts)
assert.NilError(t, err)
assert.Equal(t, strings.Count(stdout.String(), "\n"), 1,
"the output should be a single line")

assert.Equal(t, stdout.String(), expectedMatrix)
assert.Equal(t, formatJSON(t, stdout), expectedMatrix)
}

// expectedMatrix can be automatically updated by running tests with -update
// nolint:lll
var expectedMatrix = `{"include":[{"id":0,"estimatedRuntime":"6s","packages":["pkg2"],"description":"partition 0 - package pkg2"},{"id":1,"estimatedRuntime":"4s","packages":["pkg1"],"description":"partition 1 - package pkg1"},{"id":2,"estimatedRuntime":"2s","packages":["pkg0","other"],"description":"partition 2 - package pkg0 and 1 others"}]}
`
var expectedMatrix = `{
"include": [
{
"description": "partition 0 - package pkg2",
"estimatedRuntime": "6s",
"id": 0,
"packages": "pkg2"
},
{
"description": "partition 1 - package pkg1",
"estimatedRuntime": "4s",
"id": 1,
"packages": "pkg1"
},
{
"description": "partition 2 - package pkg0 and 1 others",
"estimatedRuntime": "2s",
"id": 2,
"packages": "pkg0 other"
}
]
}`

func formatJSON(t *testing.T, v io.Reader) string {
t.Helper()
raw := map[string]interface{}{}
err := json.NewDecoder(v).Decode(&raw)
assert.NilError(t, err)

formatted, err := json.MarshalIndent(raw, "", " ")
assert.NilError(t, err)
return string(formatted)
}

0 comments on commit 8cd52d7

Please sign in to comment.