Gitfed
bastien-mrq/gitfed / cmd / gitfed-install / scan_test.go
package main

import (
	"net"
	"testing"
)

func TestCommandExists(t *testing.T) {
	if !commandExists("go") {
		t.Error("expected to find \"go\" on PATH — this test itself runs via `go test`")
	}
	if commandExists("gitfed-install-definitely-not-a-real-binary") {
		t.Error("expected a made-up binary name to not exist")
	}
}

func TestPortBusyDetectsAnOpenListener(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("listen: %v", err)
	}
	defer l.Close()
	port := l.Addr().(*net.TCPAddr).Port

	if !portBusy(port) {
		t.Errorf("port %d has an active listener, portBusy should report true", port)
	}
}

func TestPortBusyFalseForAFreePort(t *testing.T) {
	// Grab an ephemeral port, then release it immediately — it's very
	// likely still free a moment later, which is all this needs.
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("listen: %v", err)
	}
	port := l.Addr().(*net.TCPAddr).Port
	l.Close()

	if portBusy(port) {
		t.Errorf("port %d was just released, portBusy should report false", port)
	}
}

func TestRunScanPopulatesOSAndArch(t *testing.T) {
	r := runScan()
	if r.OS == "" || r.Arch == "" {
		t.Errorf("runScan() left OS/Arch empty: %+v", r)
	}
}