@@ -325,6 +325,57 @@ func TestNewMachineGoesBeforeDefault(t *testing.T) {
325325 }
326326}
327327
328+ func TestRemoveMachine (t * testing.T ) {
329+ n , err := ParseFile ("examples/good.netrc" )
330+ if err != nil {
331+ t .Fatal (err )
332+ }
333+
334+ tests := []string {"mail.google.com" , "weirdlogin" }
335+
336+ for _ , name := range tests {
337+ mcount := len (n .machines )
338+ // sanity check
339+ m := n .FindMachine (name )
340+ if m == nil {
341+ t .Fatalf ("machine %q not found" , name )
342+ }
343+ if m .IsDefault () {
344+ t .Fatalf ("expected machine %q, got default instead" , name )
345+ }
346+ n .RemoveMachine (name )
347+
348+ if len (n .machines ) != mcount - 1 {
349+ t .Errorf ("n.machines count expected %d, got %d" , mcount - 1 , len (n .machines ))
350+ }
351+
352+ // make sure Machine is no longer returned by FindMachine()
353+ if m2 := n .FindMachine (name ); m2 != nil && ! m2 .IsDefault () {
354+ t .Errorf ("Machine %q not removed from Machines list" , name )
355+ }
356+
357+ // make sure tokens are not present in tokens list
358+ for _ , token := range []* token {m .nametoken , m .logintoken , m .passtoken , m .accounttoken } {
359+ if token != nil {
360+ for _ , tok2 := range n .tokens {
361+ if tok2 == token {
362+ t .Errorf ("token not removed from tokens list: %v" , token )
363+ break
364+ }
365+ }
366+ }
367+ }
368+
369+ bodyb , _ := n .MarshalText ()
370+ body := string (bodyb )
371+ for _ , value := range []string {m .Name , m .Login , m .Password , m .Account } {
372+ if value != "" && strings .Contains (body , value ) {
373+ t .Errorf ("MarshalText() after RemoveMachine() contained unexpected %q" , value )
374+ }
375+ }
376+ }
377+ }
378+
328379func TestUpdateLogin (t * testing.T ) {
329380 n , err := ParseFile ("examples/good.netrc" )
330381 if err != nil {
0 commit comments