git-codereview: add test for loadAuth
Change-Id: Ifdbfc63caa7c1afa42d05c2f91a6168a867e8056
Reviewed-on: https://go-review.googlesource.com/2113
Reviewed-by: Austin Clements <austin@google.com>
diff --git a/git-codereview/api_test.go b/git-codereview/api_test.go
new file mode 100644
index 0000000..410be5a
--- /dev/null
+++ b/git-codereview/api_test.go
@@ -0,0 +1,116 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "os"
+ "testing"
+)
+
+var authTests = []struct {
+ netrc string
+ cookiefile string
+ user string
+ password string
+ cookieName string
+ cookieValue string
+ died bool
+}{
+ {
+ died: true,
+ },
+ {
+ netrc: "machine go.googlesource.com login u1 password pw\n",
+ user: "u1",
+ password: "pw",
+ },
+ {
+ cookiefile: "go.googlesource.com TRUE / TRUE 2147483647 o2 git-u2=pw\n",
+ cookieName: "o2",
+ cookieValue: "git-u2=pw",
+ },
+ {
+ cookiefile: ".googlesource.com TRUE / TRUE 2147483647 o3 git-u3=pw\n",
+ cookieName: "o3",
+ cookieValue: "git-u3=pw",
+ },
+ {
+ cookiefile: ".googlesource.com TRUE / TRUE 2147483647 o4 WRONG\n" +
+ "go.googlesource.com TRUE / TRUE 2147483647 o4 git-u4=pw\n",
+ cookieName: "o4",
+ cookieValue: "git-u4=pw",
+ },
+ {
+ cookiefile: "go.googlesource.com TRUE / TRUE 2147483647 o5 git-u5=pw\n" +
+ ".googlesource.com TRUE / TRUE 2147483647 o5 WRONG\n",
+ cookieName: "o5",
+ cookieValue: "git-u5=pw",
+ },
+ {
+ netrc: "machine go.googlesource.com login u6 password pw\n",
+ cookiefile: "BOGUS",
+ user: "u6",
+ password: "pw",
+ },
+ {
+ netrc: "BOGUS",
+ cookiefile: "go.googlesource.com TRUE / TRUE 2147483647 o7 git-u7=pw\n",
+ cookieName: "o7",
+ cookieValue: "git-u7=pw",
+ },
+ {
+ netrc: "machine go.googlesource.com login u8 password pw\n",
+ cookiefile: "MISSING",
+ user: "u8",
+ password: "pw",
+ },
+}
+
+func TestLoadAuth(t *testing.T) {
+ gt := newGitTest(t)
+ defer gt.done()
+ gt.work(t)
+
+ defer os.Setenv("HOME", os.Getenv("HOME"))
+ os.Setenv("HOME", gt.client)
+ trun(t, gt.client, "git", "config", "remote.origin.url", "https://go.googlesource.com/go")
+
+ for i, tt := range authTests {
+ t.Logf("#%d", i)
+ auth.user = ""
+ auth.password = ""
+ auth.cookieName = ""
+ auth.cookieValue = ""
+ trun(t, gt.client, "git", "config", "http.cookiefile", "XXX")
+ trun(t, gt.client, "git", "config", "--unset", "http.cookiefile")
+
+ remove(t, gt.client+"/.netrc")
+ remove(t, gt.client+"/.cookies")
+ if tt.netrc != "" {
+ write(t, gt.client+"/.netrc", tt.netrc)
+ }
+ if tt.cookiefile != "" {
+ if tt.cookiefile != "MISSING" {
+ write(t, gt.client+"/.cookies", tt.cookiefile)
+ }
+ trun(t, gt.client, "git", "config", "http.cookiefile", gt.client+"/.cookies")
+ }
+
+ // Run command via testMain to trap stdout, stderr, death.
+ // mail -n will load auth info for us.
+ if tt.died {
+ testMainDied(t, "test-loadAuth")
+ } else {
+ testMain(t, "test-loadAuth")
+ }
+
+ if auth.user != tt.user || auth.password != tt.password {
+ t.Errorf("#%d: have user, password = %q, %q, want %q, %q", i, auth.user, auth.password, tt.user, tt.password)
+ }
+ if auth.cookieName != tt.cookieName || auth.cookieValue != tt.cookieValue {
+ t.Errorf("#%d: have cookie name, value = %q, %q, want %q, %q", i, auth.cookieName, auth.cookieValue, tt.cookieName, tt.cookieValue)
+ }
+ }
+}
diff --git a/git-codereview/review.go b/git-codereview/review.go
index c8ed27a..98fa241 100644
--- a/git-codereview/review.go
+++ b/git-codereview/review.go
@@ -140,6 +140,8 @@
submit(args)
case "sync":
doSync(args)
+ case "test-loadAuth": // for testing only
+ loadAuth()
default:
flags.Usage()
}