Skip to content

Commit

Permalink
Don't serialize tokens with no values.
Browse files Browse the repository at this point in the history
Fixes #3.
  • Loading branch information
bgentry committed Jan 31, 2014
1 parent 6996b5c commit 13bba5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 8 additions & 2 deletions netrc/netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
15 changes: 14 additions & 1 deletion netrc/netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package netrc

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
)

var expectedMachines = []*Machine{
&Machine{Name: "mail.google.com", Login: "[email protected]", Password: "somethingSecret", Account: "gmail"},
&Machine{Name: "mail.google.com", Login: "[email protected]", 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: "[email protected]", Account: ""},
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 13bba5c

Please sign in to comment.