Skip to content

Commit 7160553

Browse files
committed
make sure undesired tokens are omitted
1 parent a9baa4b commit 7160553

File tree

1 file changed

+90
-46
lines changed

1 file changed

+90
-46
lines changed

netrc/netrc_test.go

Lines changed: 90 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -184,53 +184,105 @@ func TestMarshalText(t *testing.T) {
184184
}
185185
}
186186

187+
var newMachineTests = []struct {
188+
name string
189+
login string
190+
password string
191+
account string
192+
}{
193+
{"heroku.com", "dodging-samurai-42@heroku.com", "octocatdodgeballchampions", "2011+2013"},
194+
{"bgentry.io", "special@test.com", "noacct", ""},
195+
{"github.io", "2@test.com", "", "acctwithnopass"},
196+
{"someotherapi.com", "", "passonly", ""},
197+
}
198+
187199
func TestNewMachine(t *testing.T) {
188200
n, err := ParseFile("examples/good.netrc")
189201
if err != nil {
190202
t.Fatal(err)
191203
}
192-
nameVal := "heroku.com"
193-
loginVal := "dodging-samurai-42@heroku.com"
194-
passwordVal := "octocatdodgeballchampions"
195-
accountVal := "someacct"
204+
testNewMachine(t, n)
205+
n = &Netrc{}
206+
testNewMachine(t, n)
196207

197-
// sanity check
198-
bodyb, _ := n.MarshalText()
199-
body := string(bodyb)
200-
for _, value := range []string{nameVal, loginVal, passwordVal, accountVal} {
201-
if strings.Contains(body, value) {
202-
t.Errorf("MarshalText() before NewMachine() contained unexpected %q", value)
208+
// make sure that tokens without a value are not serialized at all
209+
for _, test := range newMachineTests {
210+
n = &Netrc{}
211+
_ = n.NewMachine(test.name, test.login, test.password, test.account)
212+
213+
bodyb, _ := n.MarshalText()
214+
body := string(bodyb)
215+
216+
// ensure desired values are present when they should be
217+
if !strings.Contains(body, "machine") {
218+
t.Errorf("NewMachine() %s missing keyword 'machine'", test.name)
219+
}
220+
if !strings.Contains(body, test.name) {
221+
t.Errorf("NewMachine() %s missing value %q", test.name, test.name)
222+
}
223+
if test.login != "" && !strings.Contains(body, "login "+test.login) {
224+
t.Errorf("NewMachine() %s missing value %q", test.name, "login "+test.login)
225+
}
226+
if test.password != "" && !strings.Contains(body, "password "+test.password) {
227+
t.Errorf("NewMachine() %s missing value %q", test.name, "password "+test.password)
228+
}
229+
if test.account != "" && !strings.Contains(body, "account "+test.account) {
230+
t.Errorf("NewMachine() %s missing value %q", test.name, "account "+test.account)
203231
}
204-
}
205232

206-
m := n.NewMachine(nameVal, loginVal, passwordVal, accountVal)
207-
if m == nil {
208-
t.Fatalf("NewMachine() returned nil")
209-
}
210-
// check values
211-
if m.Name != nameVal {
212-
t.Errorf("m.Name expected %q, got %q", nameVal, m.Name)
213-
}
214-
if m.Login != loginVal {
215-
t.Errorf("m.Login expected %q, got %q", loginVal, m.Login)
216-
}
217-
if m.Password != passwordVal {
218-
t.Errorf("m.Password expected %q, got %q", passwordVal, m.Password)
219-
}
220-
if m.Account != accountVal {
221-
t.Errorf("m.Account expected %q, got %q", accountVal, m.Account)
233+
// ensure undesired values are not present when they shouldn't be
234+
if test.login == "" && strings.Contains(body, "login") {
235+
t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "login")
236+
}
237+
if test.password == "" && strings.Contains(body, "password") {
238+
t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "password")
239+
}
240+
if test.account == "" && strings.Contains(body, "account") {
241+
t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "account")
242+
}
222243
}
223-
// check tokens
224-
checkToken(t, "nametoken", m.nametoken, tkMachine, "\nmachine", nameVal)
225-
checkToken(t, "logintoken", m.logintoken, tkLogin, "\n\tlogin", loginVal)
226-
checkToken(t, "passtoken", m.passtoken, tkPassword, "\n\tpassword", passwordVal)
227-
checkToken(t, "accounttoken", m.accounttoken, tkAccount, "\n\taccount", accountVal)
228-
// check marshal output
229-
bodyb, _ = n.MarshalText()
230-
body = string(bodyb)
231-
for _, value := range []string{nameVal, loginVal, passwordVal, accountVal} {
232-
if !strings.Contains(body, value) {
233-
t.Errorf("MarshalText() after NewMachine() did not include %q as expected", value)
244+
}
245+
246+
func testNewMachine(t *testing.T, n *Netrc) {
247+
for _, test := range newMachineTests {
248+
// sanity check
249+
bodyb, _ := n.MarshalText()
250+
body := string(bodyb)
251+
for _, value := range []string{test.name, test.login, test.password, test.account} {
252+
if value != "" && strings.Contains(body, value) {
253+
t.Errorf("MarshalText() before NewMachine() contained unexpected %q", value)
254+
}
255+
}
256+
257+
m := n.NewMachine(test.name, test.login, test.password, test.account)
258+
if m == nil {
259+
t.Fatalf("NewMachine() returned nil")
260+
}
261+
// check values
262+
if m.Name != test.name {
263+
t.Errorf("m.Name expected %q, got %q", test.name, m.Name)
264+
}
265+
if m.Login != test.login {
266+
t.Errorf("m.Login expected %q, got %q", test.login, m.Login)
267+
}
268+
if m.Password != test.password {
269+
t.Errorf("m.Password expected %q, got %q", test.password, m.Password)
270+
}
271+
if m.Account != test.account {
272+
t.Errorf("m.Account expected %q, got %q", test.account, m.Account)
273+
}
274+
// check tokens
275+
checkToken(t, "nametoken", m.nametoken, tkMachine, "\nmachine", test.name)
276+
checkToken(t, "logintoken", m.logintoken, tkLogin, "\n\tlogin", test.login)
277+
checkToken(t, "passtoken", m.passtoken, tkPassword, "\n\tpassword", test.password)
278+
checkToken(t, "accounttoken", m.accounttoken, tkAccount, "\n\taccount", test.account)
279+
// check marshal output
280+
bodyb, _ = n.MarshalText()
281+
body = string(bodyb)
282+
for _, value := range []string{test.name, test.login, test.password, test.account} {
283+
if !strings.Contains(body, value) {
284+
t.Errorf("MarshalText() after NewMachine() did not include %q as expected", value)
285+
}
234286
}
235287
}
236288
}
@@ -254,14 +306,6 @@ func checkToken(t *testing.T, name string, tok *token, kind tkType, rawkind, val
254306
}
255307
}
256308

257-
type tokenss struct {
258-
kind tkType
259-
macroName string
260-
value string
261-
rawkind []byte
262-
rawvalue []byte
263-
}
264-
265309
func TestUpdateLogin(t *testing.T) {
266310
n, err := ParseFile("examples/good.netrc")
267311
if err != nil {

0 commit comments

Comments
 (0)