File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,13 @@ func (h *Hosts) Load() error {
72
72
}
73
73
defer file .Close ()
74
74
75
+ // if you're reloading from disk confirm you refresh the hash maps and lines
76
+ if len (h .Lines ) != 0 {
77
+ h .ips = lookup {l : make (map [string ][]int )}
78
+ h .hosts = lookup {l : make (map [string ][]int )}
79
+ h .Lines = []HostsLine {}
80
+ }
81
+
75
82
scanner := bufio .NewScanner (utfbom .SkipOnly (file ))
76
83
for scanner .Scan () {
77
84
hl := NewHostsLine (scanner .Text ())
Original file line number Diff line number Diff line change @@ -263,3 +263,21 @@ func benchmarkHosts_Flush(c int, b *testing.B) {
263
263
assert .Nil (b , hosts .Flush ())
264
264
assert .Nil (b , os .Remove ("hostsfile" ))
265
265
}
266
+
267
+ func TestHosts_Flush (t * testing.T ) {
268
+ f , err := os .Create ("hostsfile" )
269
+ defer func () {
270
+ assert .Nil (t , f .Close ())
271
+ assert .Nil (t , os .Remove ("hostsfile" ))
272
+ }()
273
+
274
+ assert .Nil (t , err )
275
+ hosts , err := NewCustomHosts ("./hostsfile" )
276
+ assert .Nil (t , err )
277
+ assert .Nil (t , hosts .Add ("127.0.0.2" , "host1" ))
278
+ assert .Equal (t , 1 , len (hosts .Lines ))
279
+ assert .Equal (t , "127.0.0.2 host1" , hosts .Lines [0 ].Raw )
280
+ assert .Nil (t , hosts .Flush ())
281
+ assert .Equal (t , 1 , len (hosts .Lines ))
282
+ assert .Equal (t , "127.0.0.2 host1" , hosts .Lines [0 ].Raw )
283
+ }
You can’t perform that action at this time.
0 commit comments