Skip to content

Commit 7909c30

Browse files
authored
Merge pull request #67 from grafana/66-adds-the-capability-to-test-generated-files
feat: added testability of generated files
2 parents 9c17379 + e926706 commit 7909c30

File tree

9 files changed

+123
-16
lines changed

9 files changed

+123
-16
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ name | reqired | default | description
472472
in | yes | | input file name
473473
out | no | stdout | output file name
474474
api | no | | output directory name
475+
test | no | | api path(s) to test after generation
475476
quiet | no | `false` | no output, only validation
476477
loose | no | `false` | skip JSON schema validation
477478
lint | no | `false` | enable built-in linter
@@ -483,6 +484,8 @@ In GitHub action mode, the change can be indicated by comparing the output to a
483484

484485
The `api` parameter can be used to specify a directory into which the outputs are written. The `registry.json` file is placed in the root directory. The `extension.json` file and the `badge.svg` file are placed in a directory with the same name as the go module path of the extension (if the `lint` parameter is `true`).
485486

487+
The `test` parameter can be used to test registry and catalog files generated with the `api` parameter. The test is successful if the file is not empty, contains `k6` and at least one extension, and if all extensions meet the minimum requirements (e.g. it has versions).
488+
486489
**Outputs**
487490

488491
name | description
@@ -521,6 +524,8 @@ The output of the generation will be written to the standard output by default.
521524

522525
The `--api` flag can be used to specify a directory to which the outputs will be written. The `registry.json` file is placed in the root directory. The `extension.json` file and the `badge.svg` file (if the `--lint` flag is used) are placed in a directory with the same name as the extension's go module path.
523526

527+
The `--test` flag can be used to test registry and catalog files generated with the `--api` flag. The test is successful if the file is not empty, contains `k6` and at least one extension, and if all extensions meet the minimum requirements (e.g. it has versions).
528+
524529

525530
```
526531
k6registry [flags] [source-file]
@@ -529,15 +534,17 @@ k6registry [flags] [source-file]
529534
### Flags
530535
531536
```
532-
-o, --out string write output to file instead of stdout
533-
--api string write outputs to directory instead of stdout
534-
-q, --quiet no output, only validation
535-
--loose skip JSON schema validation
536-
--lint enable built-in linter
537-
-c, --compact compact instead of pretty-printed output
538-
--catalog generate catalog instead of registry
539-
-V, --version print version
540-
-h, --help help for k6registry
537+
-o, --out string write output to file instead of stdout
538+
--api string write outputs to directory instead of stdout
539+
--test strings test api path(s) (example: /registry.json,/catalog.json)
540+
-q, --quiet no output, only validation
541+
--loose skip JSON schema validation
542+
--lint enable built-in linter
543+
-c, --compact compact instead of pretty-printed output
544+
--catalog generate catalog instead of registry
545+
-v, --verbose verbose logging
546+
-V, --version print version
547+
-h, --help help for k6registry
541548
```
542549
543550
<!-- #endregion cli -->

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ inputs:
4747
description: reference output URL for change detection
4848
required: false
4949

50+
test:
51+
description: api path(s) to test after generation
52+
required: false
53+
5054
outputs:
5155
changed:
5256
description: "true if the output has changed compared to ref"

cmd/api.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
6+
"fmt"
57
"os"
68
"path/filepath"
9+
"strings"
710

811
"github.com/grafana/k6registry"
912
"github.com/narqo/go-badge"
@@ -338,3 +341,65 @@ func badgecolor(grade k6registry.Grade) badge.Color {
338341
return "blue"
339342
}
340343
}
344+
345+
func isCatalog(filename string) bool {
346+
basename := strings.TrimSuffix(filename, filepath.Ext(filename))
347+
348+
return strings.HasSuffix(basename, "catalog")
349+
}
350+
351+
func testAPI(paths []string, dir string) error {
352+
for _, name := range paths {
353+
name = filepath.FromSlash(strings.TrimPrefix(name, "/"))
354+
name = filepath.Join(dir, name)
355+
356+
if err := testFile(name); err != nil {
357+
return err
358+
}
359+
}
360+
361+
return nil
362+
}
363+
364+
//nolint:forbidigo
365+
func testFile(filename string) error {
366+
data, err := os.ReadFile(filepath.Clean(filename))
367+
if err != nil {
368+
return err
369+
}
370+
371+
var catalog k6registry.Catalog
372+
373+
if isCatalog(filename) {
374+
if err := json.Unmarshal(data, &catalog); err != nil {
375+
return err
376+
}
377+
} else {
378+
var registry k6registry.Registry
379+
380+
if err := json.Unmarshal(data, &registry); err != nil {
381+
return err
382+
}
383+
384+
catalog = k6registry.RegistryToCatalog(registry)
385+
}
386+
387+
_, found := catalog[k6ImportPath]
388+
if !found {
389+
return fmt.Errorf("%w: %s: missing %s module", errTestFailed, filename, k6Module)
390+
}
391+
392+
if len(catalog) == 1 {
393+
return fmt.Errorf("%w: %s: missing extensions", errTestFailed, filename)
394+
}
395+
396+
for _, ext := range catalog {
397+
if ok, msgs := lintExtension(ext); !ok && len(msgs) != 0 {
398+
return fmt.Errorf("%w: %s: %s", errTestFailed, filename, msgs[0])
399+
}
400+
}
401+
402+
return nil
403+
}
404+
405+
var errTestFailed = errors.New("test failed")

cmd/cmd.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
_ "embed"
77
"encoding/json"
8+
"io"
89
"io/fs"
910
"log/slog"
1011
"os"
@@ -25,6 +26,7 @@ type options struct {
2526
loose bool
2627
lint bool
2728
api string
29+
test []string
2830
}
2931

3032
// New creates new cobra command for exec command.
@@ -68,6 +70,7 @@ func New(levelVar *slog.LevelVar) (*cobra.Command, error) {
6870

6971
flags.StringVarP(&opts.out, "out", "o", "", "write output to file instead of stdout")
7072
flags.StringVar(&opts.api, "api", "", "write outputs to directory instead of stdout")
73+
flags.StringSliceVar(&opts.test, "test", []string{}, "test api path(s) (example: /registry.json,/catalog.json)")
7174
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "no output, only validation")
7275
flags.BoolVar(&opts.loose, "loose", false, "skip JSON schema validation")
7376
flags.BoolVar(&opts.lint, "lint", false, "enable built-in linter")
@@ -139,13 +142,21 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
139142
}
140143

141144
if len(opts.api) != 0 {
142-
return writeAPI(registry, opts.api)
145+
if err = writeAPI(registry, opts.api); err != nil {
146+
return err
147+
}
148+
149+
return testAPI(opts.test, opts.api)
143150
}
144151

145152
if opts.quiet {
146153
return nil
147154
}
148155

156+
return writeOutput(registry, output, opts)
157+
}
158+
159+
func writeOutput(registry k6registry.Registry, output io.Writer, opts *options) error {
149160
encoder := json.NewEncoder(output)
150161

151162
if !opts.compact {
@@ -158,12 +169,7 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
158169
source = k6registry.RegistryToCatalog(registry)
159170
}
160171

161-
err = encoder.Encode(source)
162-
if err != nil {
163-
return err
164-
}
165-
166-
return nil
172+
return encoder.Encode(source)
167173
}
168174

169175
const (

cmd/help.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ The source is read from file specified as command line argument. If it is missin
99
The output of the generation will be written to the standard output by default. The output can be saved to a file using the `-o/--out` flag.
1010

1111
The `--api` flag can be used to specify a directory to which the outputs will be written. The `registry.json` file is placed in the root directory. The `extension.json` file and the `badge.svg` file (if the `--lint` flag is used) are placed in a directory with the same name as the extension's go module path.
12+
13+
The `--test` flag can be used to test registry and catalog files generated with the `--api` flag. The test is successful if the file is not empty, contains `k6` and at least one extension, and if all extensions meet the minimum requirements (e.g. it has versions).

cmd/k6registry/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"log"
66
"log/slog"
77
"os"
8+
"strings"
89

10+
"github.com/google/shlex"
911
"github.com/grafana/k6registry/cmd"
1012
sloglogrus "github.com/samber/slog-logrus/v2"
1113
"github.com/sirupsen/logrus"
@@ -102,6 +104,15 @@ func getArgs() []string {
102104
args = append(args, "--out", out)
103105
}
104106

107+
if paths := getenv("INPUT_TEST", ""); len(paths) != 0 {
108+
parts, err := shlex.Split(paths)
109+
if err != nil {
110+
paths = strings.Join(parts, ",")
111+
}
112+
113+
args = append(args, "--test", paths)
114+
}
115+
105116
args = append(args, getenv("INPUT_IN", ""))
106117

107118
return args

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/cli/go-gh/v2 v2.9.0
99
github.com/go-git/go-git/v5 v5.12.0
1010
github.com/google/go-github/v62 v62.0.0
11+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1112
github.com/grafana/clireadme v0.1.0
1213
github.com/grafana/k6lint v0.1.0
1314
github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwM
7575
github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4=
7676
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
7777
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
78+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
79+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
7880
github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM=
7981
github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc=
8082
github.com/grafana/k6lint v0.1.0 h1:egUuy8Dmc1Wi5eXBnbGC2QkZIt71KTrOnMr/B5bgkzk=

releases/v0.1.24.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
k6registry `v0.1.24` is here 🎉!
2+
3+
This is an internal maintenance release.
4+
5+
**Added testability of generated files**
6+
7+
The generated registry and catalog will play an important role in the k6 binary provisioning subsystem, so it is important that there is no loss of service due to generated files. It is advisable to test the generated files to see if they meet the minimum requirements.
8+
9+
The `--test` flag can be used to test registry and catalog files generated with the `--api` flag. The test is successful if the file is not empty, contains k6 and at least one extension, and if all extensions meet the minimum requirements (e.g. it has versions).

0 commit comments

Comments
 (0)