-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
100 lines (91 loc) · 2.63 KB
/
main_test.go
File metadata and controls
100 lines (91 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2026 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestCLI_EndToEnd builds the binary and runs it against a fixture, asserting
// that the output matches the install+keep+escape golden file.
func TestCLI_EndToEnd(t *testing.T) {
if testing.Short() {
t.Skip("skipping CLI integration test in short mode")
}
dir := t.TempDir()
bin := filepath.Join(dir, "helm-crd-wrapper")
cmd := exec.Command("go", "build", "-o", bin, ".")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("go build: %v", err)
}
src := filepath.Join(dir, "src")
tgt := filepath.Join(dir, "tgt")
if err := os.MkdirAll(src, 0o755); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join("internal", "testdata", "input", "with_template_chars.yaml"))
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(src, "with_template_chars.yaml"), data, 0o600); err != nil {
t.Fatal(err)
}
out := &bytes.Buffer{}
run := exec.Command(bin,
"-source", src,
"-target", tgt,
"-install",
"-keep",
"-escape",
)
run.Stdout = out
run.Stderr = out
if err := run.Run(); err != nil {
t.Fatalf("run binary failed: %v\nlog:\n%s", err, out.String())
}
produced, err := os.ReadFile(filepath.Join(tgt, "with_template_chars.yaml"))
if err != nil {
t.Fatalf("read produced output: %v", err)
}
golden, err := os.ReadFile(filepath.Join("internal", "testdata", "golden", "install-keep-escape.golden.yaml"))
if err != nil {
t.Fatalf("read golden: %v", err)
}
if !bytes.Equal(produced, golden) {
t.Errorf("end-to-end output diverges from golden\n--- want ---\n%s\n--- got ---\n%s",
string(golden), string(produced))
}
}
// TestCLI_MissingFlagsExitsNonZero confirms the binary returns exit code 2 when
// required flags are missing.
func TestCLI_MissingFlagsExitsNonZero(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
dir := t.TempDir()
bin := filepath.Join(dir, "helm-crd-wrapper")
cmd := exec.Command("go", "build", "-o", bin, ".")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("go build: %v", err)
}
run := exec.Command(bin)
stderr := &bytes.Buffer{}
run.Stderr = stderr
err := run.Run()
if err == nil {
t.Fatal("expected non-zero exit with no flags")
}
if !strings.Contains(stderr.String(), "Usage:") {
t.Errorf("expected usage in stderr, got %q", stderr.String())
}
}