-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
41 lines (36 loc) · 1.12 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestGetCredential(t *testing.T) {
// create a temporary credential file
credFile, err := ioutil.TempFile("", "test-cred")
if err != nil {
t.Fatal(err)
}
defer os.Remove(credFile.Name())
// write some credentials to the file
creds := []string{
"https://john:password@github.com/foo/bar",
"https://jane:password@bitbucket.org/foo/bar.git",
}
for _, cred := range creds {
fmt.Fprintln(credFile, cred)
}
// test getting a credential that exists in the file
c := getCredential(&credential{username: "john", protocol: "https", host: "github.com", path: "foo/bar"}, credFile.Name())
if c == nil {
t.Errorf("expected to find a credential for github.com/foo/bar")
}
if c.username != "john" || c.password != "password" {
t.Errorf("unexpected credential found: %+v", c)
}
// test getting a credential that does not exist in the file
c = getCredential(&credential{username: "john", protocol: "https", host: "bitbucket.org", path: "foo/bar"}, credFile.Name())
if c != nil {
t.Errorf("expected to not find a credential for bitbucket.org/foo/bar")
}
}