2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-03-19 21:29:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-27 14:02:58 +00:00
|
|
|
"encoding/json"
|
2022-03-19 21:29:32 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
"github.com/hashicorp/go-set"
|
2022-03-19 21:29:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(os.Args[1:]); err != nil {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "FAIL: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
// Manifest represents groupings of packages for testing
|
|
|
|
// see: ci/test-core.json
|
|
|
|
type Manifest map[string][]string
|
|
|
|
|
|
|
|
func (m Manifest) covers(pkg string) bool {
|
|
|
|
for _, list := range m {
|
|
|
|
if isCovered(list, pkg) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2022-03-19 21:29:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
const (
|
|
|
|
verify = 1
|
|
|
|
group = 2
|
|
|
|
)
|
|
|
|
|
2022-03-19 21:29:32 +00:00
|
|
|
func run(args []string) error {
|
2022-10-27 14:02:58 +00:00
|
|
|
mode := len(args)
|
|
|
|
if !(mode == verify || mode == group) {
|
|
|
|
return errors.New("usage: [filename] <group>")
|
2022-03-19 21:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 13:49:50 +00:00
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
2022-03-19 21:29:32 +00:00
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
manifest, err := getManifest(f)
|
2022-03-19 21:29:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
switch mode {
|
|
|
|
case verify:
|
|
|
|
return runVerify(manifest)
|
|
|
|
case group:
|
|
|
|
return runGroups(manifest, args[1])
|
|
|
|
default:
|
|
|
|
panic("oops")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runVerify(manifest Manifest) error {
|
2022-03-19 21:29:32 +00:00
|
|
|
packages, err := inCode(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isMissing []string
|
|
|
|
for _, pkg := range packages {
|
2022-10-27 14:02:58 +00:00
|
|
|
if !manifest.covers(pkg) {
|
2022-03-19 21:29:32 +00:00
|
|
|
isMissing = append(isMissing, pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(isMissing)
|
|
|
|
for _, pkg := range isMissing {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "missing: %s\n", pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(isMissing) > 0 {
|
|
|
|
return fmt.Errorf("detected %d packages not tested", len(isMissing))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
func runGroups(manifest Manifest, group string) error {
|
|
|
|
list := manifest[group]
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
list[i] = "./" + list[i]
|
|
|
|
}
|
|
|
|
s := strings.Join(list, " ")
|
|
|
|
fmt.Print(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCovered returns true if pkg is tested by directory in manifest.
|
2022-03-19 21:29:32 +00:00
|
|
|
func isCovered(coverage []string, pkg string) bool {
|
|
|
|
for _, p := range coverage {
|
|
|
|
if isCoveredOne(p, pkg) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCoveredOne returns true if p covers pkg.
|
|
|
|
//
|
|
|
|
// p may be a complete path, or a prefix ending with recursive '...'
|
|
|
|
func isCoveredOne(p string, pkg string) bool {
|
|
|
|
if p == pkg {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(p, "/...") {
|
|
|
|
prefix := strings.TrimSuffix(p, "/...")
|
|
|
|
if strings.HasPrefix(pkg, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
func getManifest(r io.Reader) (Manifest, error) {
|
|
|
|
m := make(Manifest)
|
|
|
|
if err := json.NewDecoder(r).Decode(&m); err != nil {
|
2022-03-19 21:29:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-27 14:02:58 +00:00
|
|
|
return m, nil
|
2022-03-19 21:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uninteresting lists remaining packages that contain Go code but still
|
|
|
|
// do not need to be covered by test cases.
|
|
|
|
var uninteresting = []string{
|
|
|
|
// module
|
|
|
|
"api",
|
|
|
|
|
|
|
|
// main
|
|
|
|
".",
|
|
|
|
|
2023-02-10 17:02:29 +00:00
|
|
|
// go embed assets
|
|
|
|
"command/asset",
|
|
|
|
|
2022-03-19 21:29:32 +00:00
|
|
|
// testing helpers
|
|
|
|
"ci",
|
|
|
|
"client/testutil",
|
|
|
|
"client/vaultclient",
|
|
|
|
"e2e",
|
|
|
|
"nomad/mock",
|
|
|
|
"plugins/csi/fake",
|
|
|
|
|
|
|
|
// not core code
|
|
|
|
"demo",
|
|
|
|
"tools",
|
|
|
|
"version",
|
|
|
|
}
|
|
|
|
|
|
|
|
func skip(p string) bool {
|
|
|
|
for _, prefix := range uninteresting {
|
|
|
|
if strings.HasPrefix(p, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func inCode(root string) ([]string, error) {
|
2023-04-25 12:47:19 +00:00
|
|
|
pkgs := set.NewTreeSet[string, set.Compare[string]](set.Cmp[string])
|
2022-03-19 21:29:32 +00:00
|
|
|
|
|
|
|
err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if skip(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ext := filepath.Ext(path); ext == ".go" {
|
2022-10-27 14:02:58 +00:00
|
|
|
pkgs.Insert(filepath.Dir(path))
|
2022-03-19 21:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:58 +00:00
|
|
|
pkgs.Remove(".") // main
|
2022-03-19 21:29:32 +00:00
|
|
|
|
2023-04-25 12:47:19 +00:00
|
|
|
return pkgs.Slice(), nil
|
2022-03-19 21:29:32 +00:00
|
|
|
}
|