Skip to content

Commit 13bba5c

Browse files
committed
Don't serialize tokens with no values.
Fixes #3.
1 parent 6996b5c commit 13bba5c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

netrc/netrc.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ func (n *Netrc) FindMachine(name string) (m *Machine) {
6969
func (n *Netrc) MarshalText() (text []byte, err error) {
7070
// TODO(bgentry): not safe for concurrency
7171
for i := range n.tokens {
72-
text = append(text, n.tokens[i].rawkind...)
7372
switch n.tokens[i].kind {
74-
case tkMacdef:
73+
case tkComment, tkDefault, tkWhitespace: // always append these types
74+
text = append(text, n.tokens[i].rawkind...)
75+
default:
76+
if n.tokens[i].value != "" { // skip empty-value tokens
77+
text = append(text, n.tokens[i].rawkind...)
78+
}
79+
}
80+
if n.tokens[i].kind == tkMacdef {
7581
text = append(text, ' ')
7682
text = append(text, n.tokens[i].macroName...)
7783
}

netrc/netrc_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ package netrc
66

77
import (
88
"bytes"
9+
"fmt"
910
"io"
1011
"io/ioutil"
1112
"strings"
1213
"testing"
1314
)
1415

1516
var expectedMachines = []*Machine{
16-
&Machine{Name: "mail.google.com", Login: "[email protected]", Password: "somethingSecret", Account: "gmail"},
17+
&Machine{Name: "mail.google.com", Login: "[email protected]", Password: "somethingSecret", Account: "justagmail"},
1718
&Machine{Name: "ray", Login: "demo", Password: "mypassword", Account: ""},
1819
&Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""},
1920
&Machine{Name: "", Login: "anonymous", Password: "[email protected]", Account: ""},
@@ -185,6 +186,18 @@ func TestMarshalText(t *testing.T) {
185186
if string(result) != string(expected) {
186187
t.Errorf("expected:\n%q\ngot:\n%q", string(expected), string(result))
187188
}
189+
190+
// make sure tokens w/ no value are not serialized
191+
m := n.FindMachine("mail.google.com")
192+
m.UpdatePassword("")
193+
result, err = n.MarshalText()
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
if strings.Contains(string(result), "\tpassword \n") {
198+
fmt.Println(string(result))
199+
t.Errorf("expected zero-value password token to not be serialzed")
200+
}
188201
}
189202

190203
var newMachineTests = []struct {

0 commit comments

Comments
 (0)