From 13bba5cc3ac0873a3a87f68c4f50d8677adbb30b Mon Sep 17 00:00:00 2001 From: Blake Gentry Date: Fri, 31 Jan 2014 11:36:37 -0800 Subject: [PATCH] Don't serialize tokens with no values. Fixes #3. --- netrc/netrc.go | 10 ++++++++-- netrc/netrc_test.go | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/netrc/netrc.go b/netrc/netrc.go index 0471f17..c5ad269 100644 --- a/netrc/netrc.go +++ b/netrc/netrc.go @@ -69,9 +69,15 @@ func (n *Netrc) FindMachine(name string) (m *Machine) { func (n *Netrc) MarshalText() (text []byte, err error) { // TODO(bgentry): not safe for concurrency for i := range n.tokens { - text = append(text, n.tokens[i].rawkind...) switch n.tokens[i].kind { - case tkMacdef: + case tkComment, tkDefault, tkWhitespace: // always append these types + text = append(text, n.tokens[i].rawkind...) + default: + if n.tokens[i].value != "" { // skip empty-value tokens + text = append(text, n.tokens[i].rawkind...) + } + } + if n.tokens[i].kind == tkMacdef { text = append(text, ' ') text = append(text, n.tokens[i].macroName...) } diff --git a/netrc/netrc_test.go b/netrc/netrc_test.go index fe077d1..7137d0b 100644 --- a/netrc/netrc_test.go +++ b/netrc/netrc_test.go @@ -6,6 +6,7 @@ package netrc import ( "bytes" + "fmt" "io" "io/ioutil" "strings" @@ -13,7 +14,7 @@ import ( ) var expectedMachines = []*Machine{ - &Machine{Name: "mail.google.com", Login: "joe@gmail.com", Password: "somethingSecret", Account: "gmail"}, + &Machine{Name: "mail.google.com", Login: "joe@gmail.com", Password: "somethingSecret", Account: "justagmail"}, &Machine{Name: "ray", Login: "demo", Password: "mypassword", Account: ""}, &Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""}, &Machine{Name: "", Login: "anonymous", Password: "joe@example.com", Account: ""}, @@ -185,6 +186,18 @@ func TestMarshalText(t *testing.T) { if string(result) != string(expected) { t.Errorf("expected:\n%q\ngot:\n%q", string(expected), string(result)) } + + // make sure tokens w/ no value are not serialized + m := n.FindMachine("mail.google.com") + m.UpdatePassword("") + result, err = n.MarshalText() + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(result), "\tpassword \n") { + fmt.Println(string(result)) + t.Errorf("expected zero-value password token to not be serialzed") + } } var newMachineTests = []struct {