Skip to content

Commit 205e3b3

Browse files
committed
cleaned up some of the osx code, added a better test for list
Signed-off-by: avaid96 <[email protected]>
1 parent 7566a1e commit 205e3b3

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

credentials/credentials.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ type Credentials struct {
1717
Secret string
1818
}
1919

20-
type KeyData struct{
21-
Path string
22-
Username string
20+
type KeyData struct {
21+
Path string
22+
Username string
2323
}
2424

2525
// Serve initializes the credentials helper and parses the action argument.
@@ -133,18 +133,18 @@ func Erase(helper Helper, reader io.Reader) error {
133133
return helper.Delete(serverURL)
134134
}
135135

136-
//List returns all the serverURLs of keys in
137-
//the OS store as a list of strings
136+
//List returns all the serverURLs of keys in
137+
//the OS store as a list of strings
138138
func List(helper Helper, writer io.Writer) error {
139-
x, y, err := helper.List()
139+
paths, accts, err := helper.List()
140140
if err != nil {
141141
return err
142142
}
143143
keyDataList := []KeyData{}
144-
for index, _ := range(x) {
144+
for index := 0; index < len(paths); index++ {
145145
keyDataObj := KeyData{
146-
Path:x[index],
147-
Username:y[index],
146+
Path: paths[index],
147+
Username: accts[index],
148148
}
149149
keyDataList = append([]KeyData{keyDataObj}, keyDataList...)
150150
}
@@ -154,4 +154,4 @@ func List(helper Helper, writer io.Writer) error {
154154
}
155155
fmt.Fprint(writer, buffer.String())
156156
return nil
157-
}
157+
}

credentials/credentials_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func TestList(t *testing.T) {
152152
if err := List(h, out); err != nil {
153153
t.Fatal(err)
154154
}
155+
//testing that there is an output
155156
if out.Len() == 0 {
156157
t.Fatalf("expected output in the writer, got %d", 0)
157158
}

credentials/helper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ type Helper interface {
99
// Get retrieves credentials from the store.
1010
// It returns username and secret as strings.
1111
Get(serverURL string) (string, string, error)
12-
// List returns all the serverURLs of keys in
13-
// the OS store as a list of strings
12+
// List returns the serverURLs of keys and their
13+
// associated usernames from the OS store as a
14+
// list of strings
1415
List() ([]string, []string, error)
1516
}

osxkeychain/osxkeychain_darwin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
145145
char * path = (char *) malloc(CFStringGetLength(pathTmp)+1);
146146
path = CFStringToCharArr(pathTmp);
147147
path[strlen(path)] = '\0';
148-
char * acct = (char *) malloc(CFStringGetLength(acctTmp)+1); //<- problem line in 38th iteration
148+
char * acct = (char *) malloc(CFStringGetLength(acctTmp)+1);
149149
acct = CFStringToCharArr(acctTmp);
150150
acct[strlen(acct)] = '\0';
151151
//We now have all we need, username and servername. Now export this to .go

osxkeychain/osxkeychain_darwin_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package osxkeychain
22

33
import (
4-
"testing"
54
"github.com/docker/docker-credential-helpers/credentials"
5+
"testing"
66
)
77

88
func TestOSXKeychainHelper(t *testing.T) {
@@ -34,10 +34,16 @@ func TestOSXKeychainHelper(t *testing.T) {
3434
t.Fatal(err)
3535
}
3636

37-
_, _, err = helper.List();
38-
if err != nil {
37+
paths, accts, err := helper.List()
38+
if err != nil || len(paths) == 0 || len(accts) == 0 {
3939
t.Fatal(err)
4040
}
41+
helper.Add(creds)
42+
newpaths, newaccts, err := helper.List()
43+
if len(newpaths)-len(paths) != 1 || len(newaccts)-len(accts) != 1 {
44+
t.Fatal()
45+
}
46+
helper.Delete(creds.ServerURL)
4147
}
4248

4349
func TestMissingCredentials(t *testing.T) {

0 commit comments

Comments
 (0)