Beispiel #1
0
        public void Save_AppliesDeletionReorderingDisabling()
        {
            StringResource resource = new StringResource(Resources.ComplexHostsFile_Before);

            HostsFile file = new HostsFile(resource);

            var entry1 = file.Entries.Where(c => c.Hostname == "host1.localhost").First();
            var entry2 = file.Entries.Where(c => c.Hostname == "host2.localhost").First();
            var entry3 = file.Entries.Where(c => c.Hostname == "host3.localhost").First();
            var entry4 = file.Entries.Where(c => c.Hostname == "host4.localhost").First();
            var entry5 = file.Entries.Where(c => c.Hostname == "host5.localhost").First();
            var entry6 = new HostEntry("host6.localhost", "127.0.0.1", "comment 6");

            entry1.Enabled = false;
            entry2.Enabled = true;
            entry3.Enabled = false;

            entry3.SwapLine(entry5); // swap two with a deleted in between
            entry6.SwapLine(entry2); // new swapped with existing

            file.DeleteEntry(entry4);
            file.AddEntry(entry6);

            file.Save();

            string actualOutput = resource.ToString();

            Assert.AreEqual(Resources.ComplexHostsFile_Expected, actualOutput);
        }
        protected override void ProcessRecord()
        {
            ValidateAddress(Address);

            var newEntry = new HostEntry(Name, Address, Comment)
            {
                Enabled = Enabled
            };

            if (ShouldProcess(newEntry.ToShortString(), "Add host entry"))
            {
                if (!Force && HostEntryExists(HostsFile.Entries, newEntry.Name))
                {
                    if (!ShouldContinue(String.Format("The host entry '{0}' already exists and the Force paremeter was not specified. If you continue, a duplicate host entry will be created", Name),
                                        "Confirm", ref yesToAll, ref noToAll))
                    {
                        return;
                    }
                }

                HostsFile.AddEntry(newEntry);

                WriteObject(newEntry);
            }
        }
Beispiel #3
0
        public void AddEntry_NullEntry_ThrowsArgumentNullException()
        {
            StringResource resource = new StringResource();

            HostsFile file = new HostsFile(resource);

            file.AddEntry(null);
        }
Beispiel #4
0
        public void AddEntry_HostnameIsIgnored_ThrowsArgumentError()
        {
            StringResource resource = new StringResource();

            HostsFile file = new HostsFile(resource);

            HostEntry entry = new HostEntry("localhost", "1.0.0.0", null);

            file.AddEntry(entry);
        }
Beispiel #5
0
        public void AddEntry_AddsToEntries()
        {
            StringResource resource = new StringResource();

            HostsFile file = new HostsFile(resource);

            HostEntry entry = new HostEntry("host.localhost", "1.0.0.0", null);

            file.AddEntry(entry);

            Assert.IsTrue(file.Entries.Contains(entry));
        }
Beispiel #6
0
        public void IsDirty_ItemAdded_ReturnsTrue()
        {
            string inputValue = "127.0.0.1    host1 # comment 1\n192.168.0.1    host2";

            StringResource resource = new StringResource(inputValue);

            HostsFile file = new HostsFile(resource);

            HostEntry entry = new HostEntry("host.localhost", "1.0.0.0", null);

            file.AddEntry(entry);

            Assert.IsTrue(file.IsDirty);
        }
Beispiel #7
0
        public PropertyBag AddEntries(PropertyBag bag)
        {
            return(CatchCommonExceptions(() =>
            {
                AddEntriesRequest request = new AddEntriesRequest(bag);

                IList <HostEntry> hostEntries = request.Entries;

                HostsFile hostsFile = GetHostsFile();

                foreach (HostEntry hostEntry in hostEntries)
                {
                    hostsFile.AddEntry(hostEntry);
                }

                hostsFile.Save();

                return new AddEntriesResponse().ToPropertyBag();
            }));
        }