public void AddHostsFileEntry(IList <HostsFileEntryBase> hostsFileMapEntries) { var hostsFile = new HostsFile(); hostsFile.Load(HostsFile.GetDefaultHostsFilePath()); var addedHosts = false; foreach (var commandHostsFileMapEntry in hostsFileMapEntries) { if (hostsFile.Entries.Any(e => e.RawLine == commandHostsFileMapEntry.ToString())) { continue; } addedHosts = true; hostsFile.Add(commandHostsFileMapEntry); } if (addedHosts) { hostsFileMapEntries.Insert(0, new HostsFileComment("Added by DIMMY")); hostsFileMapEntries.Add(new HostsFileComment("End of DIMMY section ")); } hostsFile.Save(HostsFile.GetDefaultHostsFilePath()); }
public void Load_ShouldSetEntries() { _mockHandler.Setup(mock => mock.Read()).Returns(GetSampleFileData()); var hosts = new HostsFile(_mockHandler.Object); hosts.Load(); Assert.NotNull(hosts.Entries); Assert.Equal(23, hosts.Entries.Count); }
public void Load_ShouldClearHasUnsavedChanges() { _mockHandler.Setup(mock => mock.Read()).Returns(GetSampleFileData()); var hosts = new HostsFile(_mockHandler.Object) { HasUnsavedChanges = true }; hosts.Load(); Assert.False(hosts.HasUnsavedChanges, "Expected HasUnsavedChanges to be false, but it was true."); }
public void can_load_both_comments_and_entries() { var hosts = "# comment line 1" + Environment.NewLine + "127.0.0.1 entry1.com" + Environment.NewLine + "127.0.0.1 entry2.com" + Environment.NewLine + "216.92.92.92 entry3.com" + Environment.NewLine + "# comment line 2 " + Environment.NewLine + "127.0.0.1 entry4.com"; var reader = new StringReader(hosts); var hostFile = HostsFile.Load(reader); Assert.Equal(6, hostFile.Lines.Count); Assert.Equal(4, hostFile.AllEntries().Count); }
public void will_keep_original_comments_and_empty_lines_not_changed() { var hosts = "# comment line 1" + Environment.NewLine + "127.0.0.1 entry1.com" + Environment.NewLine + Environment.NewLine + "# comment line 2" + Environment.NewLine + "127.0.0.1 entry4.com"; var reader = new StringReader(hosts); var hostFile = HostsFile.Load(reader); hostFile["entry5.com"] = "127.0.0.1"; var writer = new StringWriter(); hostFile.SaveAs(writer); Assert.Equal(hosts + Environment.NewLine + "127.0.0.1 entry5.com" + Environment.NewLine, writer.ToString()); }