contract_test.go

  1package cmd
  2
  3import (
  4	"testing"
  5
  6	"git.secluded.site/keld/internal/restic"
  7)
  8
  9func TestWrappedCommandsExist(t *testing.T) {
 10	t.Parallel()
 11
 12	for _, wc := range wrappedCommands {
 13		wc := wc
 14		t.Run(wc.Name, func(t *testing.T) {
 15			t.Parallel()
 16
 17			if _, ok := restic.Commands[wc.Name]; !ok {
 18				t.Fatalf("wrapped command %q not found in restic.Commands", wc.Name)
 19			}
 20		})
 21	}
 22}
 23
 24func TestAllResticCommandsClassified(t *testing.T) {
 25	t.Parallel()
 26
 27	wrapped := make(map[string]struct{}, len(wrappedCommands))
 28	for _, wc := range wrappedCommands {
 29		wrapped[wc.Name] = struct{}{}
 30	}
 31
 32	for name := range restic.Commands {
 33		name := name
 34		if name == "global" {
 35			continue
 36		}
 37
 38		t.Run(name, func(t *testing.T) {
 39			t.Parallel()
 40
 41			if _, ok := wrapped[name]; ok {
 42				return
 43			}
 44			if _, ok := passthroughCommands[name]; ok {
 45				return
 46			}
 47			t.Fatalf("restic command %q is neither wrapped nor listed in passthroughCommands", name)
 48		})
 49	}
 50}
 51
 52func TestFlagContracts(t *testing.T) {
 53	t.Parallel()
 54
 55	for _, fc := range flagContracts {
 56		fc := fc
 57		t.Run(fc.Command+"/"+fc.Name, func(t *testing.T) {
 58			t.Parallel()
 59
 60			cmd, ok := restic.Commands[fc.Command]
 61			if !ok {
 62				t.Fatalf("command %q not found in restic.Commands", fc.Command)
 63			}
 64
 65			var found bool
 66			for _, opt := range cmd.Options {
 67				if opt.Name == fc.Name {
 68					found = true
 69					if opt.IsBool != fc.IsBool {
 70						t.Errorf("IsBool mismatch for %s/%s: got %v, want %v", fc.Command, fc.Name, opt.IsBool, fc.IsBool)
 71					}
 72					if opt.Repeatable != fc.Repeatable {
 73						t.Errorf("Repeatable mismatch for %s/%s: got %v, want %v", fc.Command, fc.Name, opt.Repeatable, fc.Repeatable)
 74					}
 75					break
 76				}
 77			}
 78			if !found {
 79				t.Fatalf("flag %q not found in restic command %q", fc.Name, fc.Command)
 80			}
 81		})
 82	}
 83}
 84
 85// TestKeyAliasesTargetsExist verifies that the config key aliases (which are
 86// unexported in the config package) target flags that actually exist in
 87// restic's global options. The aliases are hardcoded here to mirror the
 88// source of truth in internal/config/config.go.
 89func TestKeyAliasesTargetsExist(t *testing.T) {
 90	t.Parallel()
 91
 92	aliases := map[string]string{
 93		"repository": "repo",
 94	}
 95
 96	global, ok := restic.Commands["global"]
 97	if !ok {
 98		t.Fatal("restic.Commands missing 'global' entry")
 99	}
100
101	globalFlags := make(map[string]struct{}, len(global.Options))
102	for _, opt := range global.Options {
103		globalFlags[opt.Name] = struct{}{}
104	}
105
106	for key, target := range aliases {
107		key, target := key, target
108		t.Run(key+"->"+target, func(t *testing.T) {
109			t.Parallel()
110
111			if _, ok := globalFlags[target]; !ok {
112				t.Fatalf("keyAlias %q -> %q: target flag %q not found in global options", key, target, target)
113			}
114		})
115	}
116}