pkg/cli/licenses_test.go

package cli

import (
	"bytes"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"git.cafe/ezterry/ez-agent/pkg/buildinfo"
)

func TestThirdPartyLicensesOfflineBeforeDaemonOrConfig(t *testing.T) {
	root := t.TempDir()
	config := filepath.Join(root, "config-must-not-be-created")
	state := filepath.Join(root, "state-must-not-be-created")
	runtime := filepath.Join(root, "runtime-must-not-be-created")
	t.Setenv("XDG_CONFIG_HOME", config)
	t.Setenv("XDG_STATE_HOME", state)
	t.Setenv("XDG_RUNTIME_DIR", runtime)

	var out bytes.Buffer
	if err := RunWithWriter([]string{"--third-party-licenses"}, true, &out); err != nil {
		t.Fatal(err)
	}
	if got, want := out.String(), buildinfo.ThirdPartyLicenseReport(); got != want {
		t.Fatalf("output differs from embedded report: got %d bytes, want %d", len(got), len(want))
	}
	for _, path := range []string{config, state, runtime} {
		if _, err := os.Stat(path); !os.IsNotExist(err) {
			t.Fatalf("offline option accessed or created %s (stat error %v)", path, err)
		}
	}
}

func TestThirdPartyLicensesParsingAndHelp(t *testing.T) {
	var out bytes.Buffer
	if err := RunWithWriter([]string{"--no-autolaunch", "--third-party-licenses"}, false, &out); err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(out.String(), "github.com/coder/websocket v1.8.15 — ISC") {
		t.Fatal("summary lacks Coder ISC entry")
	}

	out.Reset()
	if err := RunWithWriter([]string{"--help"}, false, &out); err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(out.String(), "--third-party-licenses") {
		t.Fatal("help omits --third-party-licenses")
	}
}