check / check (pull_request) Failing after 1s
Remove the unused internal/models package (and its coverage-only test), internal/cli/vaultik_snapshot_types.go (a second, dead SnapshotInfo that shadowed the live type in internal/vaultik), and two fixtures (test-config.yml, test/integration-config.yml) that use source_dirs and other keys config.Config no longer has. All confirmed by grep against origin/next; none has a production consumer. config set now writes via yaml.NewEncoder with 2-space indent, matching defaultConfigTemplate, so the first set no longer reindents the file it promises to preserve. A test covers the round-trip. Single commit rather than one commit per deletion, per task instruction. test/integration-config.yml removed per the manager note on the issue. Model: opus-4-8
239 lines
5.4 KiB
Go
239 lines
5.4 KiB
Go
package cli //nolint:testpackage // exercises unexported yamlPathGet/yamlPathSet
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
"sneak.berlin/go/vaultik/internal/config"
|
|
)
|
|
|
|
// TestDefaultConfigTemplateParses ensures the init template is valid YAML
|
|
// that unmarshals into the Config struct with the expected snapshots.
|
|
func TestDefaultConfigTemplateParses(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var cfg config.Config
|
|
|
|
err := yaml.Unmarshal([]byte(defaultConfigTemplate), &cfg)
|
|
if err != nil {
|
|
t.Fatalf("default config template is not valid YAML: %v", err)
|
|
}
|
|
|
|
if len(cfg.AgeRecipients) != 1 {
|
|
t.Errorf("expected 1 placeholder age recipient, got %d", len(cfg.AgeRecipients))
|
|
}
|
|
|
|
home, ok := cfg.Snapshots["home"]
|
|
if !ok {
|
|
t.Fatal("expected 'home' snapshot in default config")
|
|
}
|
|
|
|
if len(home.Paths) == 0 {
|
|
t.Error("home snapshot should have at least one path")
|
|
}
|
|
|
|
if len(home.Exclude) == 0 {
|
|
t.Error("home snapshot should have exclude patterns")
|
|
}
|
|
|
|
apps, ok := cfg.Snapshots["apps"]
|
|
if !ok {
|
|
t.Fatal("expected 'apps' snapshot in default config")
|
|
}
|
|
|
|
if len(apps.Paths) != 1 || apps.Paths[0] != "/Applications" {
|
|
t.Errorf("apps snapshot should back up /Applications, got %v", apps.Paths)
|
|
}
|
|
|
|
if len(apps.Exclude) == 0 {
|
|
t.Error("apps snapshot should have exclude patterns")
|
|
}
|
|
}
|
|
|
|
const testYAML = `# top comment
|
|
compression_level: 3
|
|
age_recipients:
|
|
- age1aaa
|
|
s3:
|
|
bucket: oldbucket # inline comment
|
|
region: us-east-1
|
|
snapshots:
|
|
home:
|
|
paths:
|
|
- "~"
|
|
`
|
|
|
|
func parseTestYAML(t *testing.T) *yaml.Node {
|
|
t.Helper()
|
|
|
|
var root yaml.Node
|
|
|
|
err := yaml.Unmarshal([]byte(testYAML), &root)
|
|
if err != nil {
|
|
t.Fatalf("parsing test yaml: %v", err)
|
|
}
|
|
|
|
return &root
|
|
}
|
|
|
|
func TestYAMLPathGet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
root := parseTestYAML(t)
|
|
|
|
tests := []struct {
|
|
path string
|
|
want string
|
|
err bool
|
|
}{
|
|
{"compression_level", "3", false},
|
|
{"s3.bucket", "oldbucket", false},
|
|
{"s3.region", "us-east-1", false},
|
|
{"age_recipients.0", "age1aaa", false},
|
|
{"age_recipients.5", "", true},
|
|
{"age_recipients.notanumber", "", true},
|
|
{"s3.nonexistent", "", true},
|
|
{"nonexistent", "", true},
|
|
{"compression_level.sub", "", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.path, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
node, err := yamlPathGet(root, splitPath(tt.path))
|
|
if tt.err {
|
|
if err == nil {
|
|
t.Fatalf("expected error for %q", tt.path)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if node.Value != tt.want {
|
|
t.Errorf("get %q = %q, want %q", tt.path, node.Value, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestYAMLPathSet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
root := parseTestYAML(t)
|
|
|
|
// Overwrite existing nested value
|
|
err := yamlPathSet(root, splitPath("s3.bucket"), "newbucket")
|
|
if err != nil {
|
|
t.Fatalf("set s3.bucket: %v", err)
|
|
}
|
|
|
|
// Create new nested key with intermediate map
|
|
err = yamlPathSet(root, splitPath("s3.endpoint"), "s3.example.com")
|
|
if err != nil {
|
|
t.Fatalf("set s3.endpoint: %v", err)
|
|
}
|
|
|
|
err = yamlPathSet(root, splitPath("newmap.newkey"), "val")
|
|
if err != nil {
|
|
t.Fatalf("set newmap.newkey: %v", err)
|
|
}
|
|
|
|
// Overwrite a sequence element and append a new one
|
|
err = yamlPathSet(root, splitPath("age_recipients.0"), "age1bbb")
|
|
if err != nil {
|
|
t.Fatalf("set age_recipients.0: %v", err)
|
|
}
|
|
|
|
err = yamlPathSet(root, splitPath("age_recipients.1"), "age1ccc")
|
|
if err != nil {
|
|
t.Fatalf("append age_recipients.1: %v", err)
|
|
}
|
|
|
|
err = yamlPathSet(root, splitPath("age_recipients.5"), "age1ddd")
|
|
if err == nil {
|
|
t.Error("expected out-of-range append to fail")
|
|
}
|
|
|
|
// Round-trip and verify values + comment preservation
|
|
out, err := yaml.Marshal(root)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
text := string(out)
|
|
|
|
wants := []string{
|
|
"newbucket", "s3.example.com", "newkey: val",
|
|
"# top comment", "# inline comment", "age1bbb", "age1ccc",
|
|
}
|
|
for _, want := range wants {
|
|
if !contains(text, want) {
|
|
t.Errorf("round-tripped YAML missing %q:\n%s", want, text)
|
|
}
|
|
}
|
|
|
|
got, err := yamlPathGet(root, splitPath("s3.bucket"))
|
|
if err != nil {
|
|
t.Fatalf("get after set: %v", err)
|
|
}
|
|
|
|
if got.Value != "newbucket" {
|
|
t.Errorf("s3.bucket = %q after set, want newbucket", got.Value)
|
|
}
|
|
}
|
|
|
|
// TestConfigSetPreservesFormatting asserts the `config set` write path
|
|
// (marshalConfigYAML) round-trips a 2-space-indented file without reindenting
|
|
// it to yaml.Marshal's 4-space default, and keeps comments.
|
|
func TestConfigSetPreservesFormatting(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
root := parseTestYAML(t)
|
|
|
|
err := yamlPathSet(root, splitPath("s3.bucket"), "newbucket")
|
|
if err != nil {
|
|
t.Fatalf("set s3.bucket: %v", err)
|
|
}
|
|
|
|
out, err := marshalConfigYAML(root)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
text := string(out)
|
|
|
|
for _, want := range []string{"# top comment", "# inline comment"} {
|
|
if !contains(text, want) {
|
|
t.Errorf("round-tripped YAML dropped comment %q:\n%s", want, text)
|
|
}
|
|
}
|
|
|
|
// Nested map keys stay at 2-space indent; the bug reindented them to 4.
|
|
if !contains(text, "\n bucket: newbucket") {
|
|
t.Errorf("expected 2-space indent for s3.bucket, got:\n%s", text)
|
|
}
|
|
|
|
if contains(text, "\n bucket:") {
|
|
t.Errorf("s3.bucket reindented to 4 spaces:\n%s", text)
|
|
}
|
|
|
|
// Sequence items under a key also stay at 2 spaces.
|
|
if !contains(text, "\n - age1aaa") {
|
|
t.Errorf("expected 2-space indent for sequence item, got:\n%s", text)
|
|
}
|
|
}
|
|
|
|
func splitPath(s string) []string {
|
|
return strings.Split(s, ".")
|
|
}
|
|
|
|
func contains(haystack, needle string) bool {
|
|
return strings.Contains(haystack, needle)
|
|
}
|