This repository has been archived on 2026-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
w10k/main_test.go
T
2026-11-08 21:58:45 +11:00

71 lines
2.0 KiB
Go

package main
import (
"bytes"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
)
func TestTimingsFlag(t *testing.T) {
root := t.TempDir()
cfg := filepath.Join(root, "r10k.yaml")
body := fmt.Sprintf("cachedir: %s\nsources:\n s:\n remote: %s\n basedir: %s\n", filepath.Join(root, "cache"), filepath.Join(root, "control"), filepath.Join(root, "envs"))
if err := os.WriteFile(cfg, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
prev := slog.Default()
defer slog.SetDefault(prev)
for _, on := range []bool{false, true} {
var buf bytes.Buffer
slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
args := []string{"gc", "-c", cfg}
if on {
args = append(args, "--timings")
}
cmd := rootCmd()
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
if got := strings.Contains(buf.String(), "phase=gc.mark"); got != on {
t.Errorf("--timings=%v: gc.mark emitted=%v\n%s", on, got, buf.String())
}
}
}
func TestListShowsAliasRows(t *testing.T) {
root := t.TempDir()
cache, base := filepath.Join(root, "cache"), filepath.Join(root, "code")
wt := filepath.Join(cache, "worktrees", "unkin", "abc123-0123456789ab")
for _, d := range []string{wt, base} {
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
}
for _, e := range []string{"develop", "production"} {
if err := os.Symlink(wt, filepath.Join(base, e)); err != nil {
t.Fatal(err)
}
}
cfg := filepath.Join(root, "r10k.yaml")
y := "cachedir: " + cache + "\nw10k: {aliases: {production: develop}}\nsources: {unkin: {remote: r, basedir: " + base + "}}\n"
if err := os.WriteFile(cfg, []byte(y), 0o644); err != nil {
t.Fatal(err)
}
var out bytes.Buffer
cmd := rootCmd()
cmd.SetOut(&out)
cmd.SetArgs([]string{"-c", cfg, "list"})
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
want := "unkin\tdevelop\tabc123\nunkin\tproduction\tabc123\talias of develop\n"
if out.String() != want {
t.Fatalf("list:\n%q\nwant\n%q", out.String(), want)
}
}