code.oscarkilo.com/okg/okg_test.go

.gitignore
README.md
auth.go
client.go
config.go
go.mod
main.go
okg_test.go
pr.go
repo.go
package main

import "os"
import "testing"
import "time"

func TestRepoRegex(t *testing.T) {
  check := func(url, want string) {
    t.Helper()
    m := repoRegex.FindStringSubmatch(url)
    if want == "" {
      if m != nil {
        t.Errorf("%q: want no match, got %q", url, m[1])
      }
      return
    }
    if m == nil {
      t.Errorf("%q: want %q, got no match", url, want)
      return
    }
    if m[1] != want {
      t.Errorf("%q: want %q, got %q", url, want, m[1])
    }
  }
  check("https://code.oscarkilo.com/widget.git", "widget")
  check("https://code.oscarkilo.com/klee.git", "klee")
  check("https://code.oscarkilo.com/my-repo.git", "my-repo")
  check("https://code.oscarkilo.com/a123.git", "a123")
  check("https://github.com/foo/bar.git", "")
  check("not-a-url", "")
}

func TestResolveRepo(t *testing.T) {
  // Flag takes priority.
  repo, err := resolveRepo("from-flag")
  if err != nil {
    t.Fatal(err)
  }
  if repo != "from-flag" {
    t.Errorf("want from-flag, got %q", repo)
  }

  // Env var takes priority over detection.
  os.Setenv("OKG_REPO", "from-env")
  defer os.Unsetenv("OKG_REPO")
  repo, err = resolveRepo("")
  if err != nil {
    t.Fatal(err)
  }
  if repo != "from-env" {
    t.Errorf("want from-env, got %q", repo)
  }
}

func TestParsePRFlags(t *testing.T) {
  f, rest, err := parsePRFlags([]string{
    "--repo", "widget", "--json", "42",
  })
  if err != nil {
    t.Fatal(err)
  }
  if f.repo != "widget" {
    t.Errorf("repo: want widget, got %q", f.repo)
  }
  if !f.asJSON {
    t.Error("asJSON: want true")
  }
  if len(rest) != 1 || rest[0] != "42" {
    t.Errorf("rest: want [42], got %v", rest)
  }
}

func TestParsePRFlagsEmpty(t *testing.T) {
  f, rest, err := parsePRFlags(nil)
  if err != nil {
    t.Fatal(err)
  }
  if f.repo != "" {
    t.Errorf("repo: want empty, got %q", f.repo)
  }
  if f.asJSON {
    t.Error("asJSON: want false")
  }
  if len(rest) != 0 {
    t.Errorf("rest: want empty, got %v", rest)
  }
}

func TestParsePRFlagsMissingValue(t *testing.T) {
  _, _, err := parsePRFlags([]string{"--repo"})
  if err == nil {
    t.Error("want error for --repo without value")
  }
}

func TestAge(t *testing.T) {
  check := func(d time.Duration, want string) {
    t.Helper()
    got := age(time.Now().Add(-d))
    if got != want {
      t.Errorf("age(-%v): want %q, got %q", d, want, got)
    }
  }
  check(30*time.Second, "just now")
  check(5*time.Minute, "5m")
  check(3*time.Hour, "3h")
  check(48*time.Hour, "2d")
}

func TestConfigEnvOverrides(t *testing.T) {
  os.Setenv("OKG_HOST", "http://test:1234")
  os.Setenv("KLEX_API_KEY", "env-key")
  defer os.Unsetenv("OKG_HOST")
  defer os.Unsetenv("KLEX_API_KEY")

  cfg, err := loadConfig()
  if err != nil {
    t.Fatal(err)
  }
  if cfg.Host != "http://test:1234" {
    t.Errorf("Host: want http://test:1234, got %q", cfg.Host)
  }
  if cfg.ApiKey != "env-key" {
    t.Errorf("ApiKey: want env-key, got %q", cfg.ApiKey)
  }
}

func TestConfigDefaultHost(t *testing.T) {
  os.Unsetenv("OKG_HOST")
  os.Unsetenv("KLEX_API_KEY")
  cfg, err := loadConfig()
  if err != nil {
    t.Fatal(err)
  }
  if cfg.Host != "http://localhost:42069" {
    t.Errorf(
      "Host: want http://localhost:42069, got %q",
      cfg.Host)
  }
}