114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"proxyctl/internal/profile"
|
|
"proxyctl/internal/target"
|
|
)
|
|
|
|
func TestVersionOutputIsHumanAndScriptFriendly(t *testing.T) {
|
|
previous := version
|
|
version = "v1.2.3"
|
|
t.Cleanup(func() { version = previous })
|
|
|
|
var output bytes.Buffer
|
|
if err := run([]string{"version"}, strings.NewReader(""), &output); err != nil {
|
|
t.Fatalf("run version: %v", err)
|
|
}
|
|
if got, want := output.String(), "proxyctl version v1.2.3\n"; got != want {
|
|
t.Fatalf("human version output = %q, want %q", got, want)
|
|
}
|
|
|
|
output.Reset()
|
|
if err := run([]string{"version", "--short"}, strings.NewReader(""), &output); err != nil {
|
|
t.Fatalf("run short version: %v", err)
|
|
}
|
|
if got, want := output.String(), "v1.2.3\n"; got != want {
|
|
t.Fatalf("script version output = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestVersionDoesNotRequireUserOrTargetResolution(t *testing.T) {
|
|
var output bytes.Buffer
|
|
if err := runWithEnvironmentAndTargets([]string{"version", "--short"}, strings.NewReader(""), &output, failingIdentityEnvironment{}, nil, "", nil); err != nil {
|
|
t.Fatalf("version depended on runtime environment: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestProductionCompositionRegistersAPTContainerGNOMEAndDaemonTargets(t *testing.T) {
|
|
home := t.TempDir()
|
|
environment := commandEnvironment{identity: profile.UserIdentity{Username: "tester", HomeDir: home, UID: os.Getuid(), GID: os.Getgid()}, env: map[string]string{"SHELL": "/bin/bash"}}
|
|
targets, err := productionTargets(environment)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ids []string
|
|
for _, adapter := range targets {
|
|
ids = append(ids, adapter.ID())
|
|
if _, ok := adapter.(target.Diagnosable); !ok {
|
|
t.Errorf("production Target %s does not contribute doctor checks", adapter.ID())
|
|
}
|
|
}
|
|
want := []string{"apt", "docker-containers", "gnome", "docker-daemon"}
|
|
if strings.Join(ids, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("production Target IDs = %#v, want %#v", ids, want)
|
|
}
|
|
}
|
|
|
|
func TestDefaultInvocationStartsTUIWithNoProfileState(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
var output bytes.Buffer
|
|
if err := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); err != nil {
|
|
t.Fatalf("run proxyctl: %v", err)
|
|
}
|
|
if !strings.Contains(output.String(), "No Active Profile") {
|
|
t.Fatalf("default TUI output missing no-Profile state: %q", output.String())
|
|
}
|
|
}
|
|
|
|
func TestDefaultInvocationUsesAlternateScreenAndRestoresItOnExit(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
var output bytes.Buffer
|
|
if err := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); err != nil {
|
|
t.Fatalf("run proxyctl: %v", err)
|
|
}
|
|
frame := output.String()
|
|
if !strings.Contains(frame, "\x1b[?1049h") {
|
|
t.Fatalf("default TUI did not enter the alternate screen: %q", frame)
|
|
}
|
|
if !strings.Contains(frame, "\x1b[?1049l") {
|
|
t.Fatalf("default TUI did not restore the previous screen: %q", frame)
|
|
}
|
|
}
|
|
|
|
func TestDefaultInvocationLoadsPersistedActiveProfile(t *testing.T) {
|
|
xdg := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", xdg)
|
|
writeConfig(t, filepath.Join(xdg, "proxyctl", "profiles.json"), `{
|
|
"profiles": [{"name":"office","mode":"single","singleUrl":"http://proxy.example:8080","httpUrl":"http://proxy.example:8080","httpsUrl":"http://proxy.example:8080","allUrl":"http://proxy.example:8080"}],
|
|
"activeProfile": "office"
|
|
}`)
|
|
var output bytes.Buffer
|
|
if err := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); err != nil {
|
|
t.Fatalf("run proxyctl: %v", err)
|
|
}
|
|
if !strings.Contains(output.String(), "Profile: office") {
|
|
t.Fatalf("default TUI output missing Profile: %q", output.String())
|
|
}
|
|
}
|
|
|
|
func writeConfig(t *testing.T, path, contents string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|