Ejemplo n.º 1
0
        public void TestSearchAndFilteredReplaceXPathWithMissingXmlDeclaration(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "books_no_decl.xml"),
                                                          SearchType.XPath, "(//@currency)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);

            // mark 2nd and 4th matches for replace
            results[0].Matches[1].ReplaceMatch = true;
            results[0].Matches[3].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase4", "books_no_decl.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.XPath, "(//@currency)", "EUR", GrepSearchOption.None, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Equal(2, Regex.Matches(fileContent, "EUR").Count);
        }
Ejemplo n.º 2
0
        public void TestSearchAndReplaceXPathWithMissingXmlDeclaration(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "books_no_decl.xml"),
                                                          SearchType.XPath, "(//@category)[2]", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].Matches);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destinationFolder, "TestCase4", "books_no_decl.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.XPath, "(//@category)[2]", "general", GrepSearchOption.None, -1);

            var fileContent = File.ReadAllLines(testFile, Encoding.UTF8);

            Assert.Equal(37, fileContent.Length);
            Assert.Equal("<bookstore>", fileContent[0]);
            Assert.Equal("  <book category=\"general\">", fileContent[7]);
        }
Ejemplo n.º 3
0
        public void TestSearchAndFilteredReplace(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books.xml"),
                                                          type, searchFor, option, -1);

            Assert.Single(results);
            Assert.Equal(2, results[0].Matches.Count);

            // mark only the second match for replace
            results[0].Matches[1].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase15", "books.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Contains("<year>2003</year>", fileContent);
            Assert.Single(Regex.Matches(fileContent, "2002"));
            Assert.Single(Regex.Matches(fileContent, "2003"));
        }
Ejemplo n.º 4
0
        public void TestReplaceOnFileWith_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            // Test for Issue #227 dnGrep inserting extra BOM, and scrambling file
            Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"),
                                                          type, searchFor, option, -1);

            Assert.Equal(1, results.Count);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"),
                         type, searchFor, replaceWith, option, -1);

            using (StreamReader reader = new StreamReader(destinationFolder + "\\TestCase15\\books_bom.xml", true))
            {
                Assert.Equal(Encoding.UTF8, reader.CurrentEncoding);
                // check there is a BOM
                int bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xEF, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xBB, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xBF, bb);
                // check that there are not two BOMs
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xEF, bb);
                Assert.Equal('<', bb);
            }
            var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books_bom.xml", Encoding.UTF8);

            Assert.Equal(38, fileContent.Length);
            string line1 = fileContent[0].Replace("\ufeff", ""); // remove BOM

            Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", line1);
            Assert.Equal("  <book category=\"general\">", fileContent[8]);
        }
Ejemplo n.º 5
0
        public void TestReplaceAndUndoWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "test-file-code.cs"), SearchType.PlainText, "body", GrepSearchOption.None, -1);

            Assert.Equal(1, results.Count);
            Assert.Equal(2, results[0].SearchResults.Where(r => r.IsContext).Count());

            string testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".cs" }
            };

            core.Replace(files, SearchType.PlainText, "body", "text", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.False(content.Contains("body"));
            Assert.True(content.Contains("text"));

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.False(content.Contains("text"));
            Assert.True(content.Contains("body"));
        }
Ejemplo n.º 6
0
        public void TestReplaceWithPattern(SearchType type)
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All guides should be unique.");
                }
            }
        }
Ejemplo n.º 7
0
        public void TestGuidxReplaceWithPatternRegex()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, "h\\wre", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, destinationFolder + "\\TestCase9", "h\\wre", "$(guidx)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\guidx.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            Dictionary <string, int> uniqueGuids = new Dictionary <string, int>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.ContainsKey(match.Value))
                {
                    uniqueGuids[match.Value] = 1;
                }
                else
                {
                    uniqueGuids[match.Value]++;
                }
            }
            Assert.Equal(2, uniqueGuids.Keys.Count);
        }
Ejemplo n.º 8
0
        public void TestReplaceOnFileWithout_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            // Test for Issue #227
            Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books.xml"),
                                                          type, searchFor, option, -1);

            Assert.Equal(1, results.Count);
            string testFile = Path.Combine(destinationFolder, @"TestCase15\books.xml");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".xml" }
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            using (StreamReader reader = new StreamReader(testFile, true))
            {
                Assert.Equal(Encoding.UTF8, reader.CurrentEncoding);
                // check there is no BOM
                int bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xEF, bb);
                Assert.Equal('<', bb);
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xBB, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xBF, bb);
            }
            var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books.xml", Encoding.UTF8);

            Assert.Equal(38, fileContent.Length);
            Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", fileContent[0]);
            Assert.Equal("  <book category=\"general\">", fileContent[8]);
        }
Ejemplo n.º 9
0
        public void TestReplaceWithPattern(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase9", destFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);

            string testFile = Path.Combine(destFolder, @"TestCase9\test.txt");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".txt" }
            };

            core.Replace(files, type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All guides should be unique.");
                }
            }
        }
Ejemplo n.º 10
0
        public void TestRegexCaptureReplace(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase3"), Path.Combine(destinationFolder, "TestCase3"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase3"), "test-file-code.cs"),
                                                          SearchType.Regex, @"-(\d)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults.Where(r => !r.IsContext));

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, @"-(\d)", @"$1", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.DoesNotContain("= -1;", content);
            Assert.Contains("= 1;", content);

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.DoesNotContain("= 1;", content);
            Assert.Contains("= -1;", content);
        }
Ejemplo n.º 11
0
        public void TestReplaceWithNewLineWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase8"), Path.Combine(destFolder, "TestCase8"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase8"), "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase8\test.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, "here", "\\\\n", GrepSearchOption.None, -1);
            Assert.Equal(2, File.ReadAllText(testFile, Encoding.ASCII).Trim().Split('\n').Length);
        }
Ejemplo n.º 12
0
        public void TestReplaceWithNewLineWorks()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase8", destinationFolder + "\\TestCase8", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", "\\n", GrepSearchOption.None, -1);
            Assert.Equal(File.ReadAllText(destinationFolder + "\\TestCase8\\test.txt", Encoding.ASCII).Trim().Split('\n').Length, 2);
        }
Ejemplo n.º 13
0
        public void TestReplaceOnFileWith_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            // Test for Issue #227 dnGrep inserting extra BOM, and scrambling file
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books_bom.xml"),
                                                          type, searchFor, option, -1);

            Assert.Single(results);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destinationFolder, "TestCase15", "books_bom.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            using (FileStream stream = File.Open(testFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamReader reader = new StreamReader(stream, true))
                {
                    Assert.Equal(Encoding.UTF8, reader.CurrentEncoding);
                    // check there is a BOM
                    int bb = reader.BaseStream.ReadByte();
                    Assert.Equal(0xEF, bb);
                    bb = reader.BaseStream.ReadByte();
                    Assert.Equal(0xBB, bb);
                    bb = reader.BaseStream.ReadByte();
                    Assert.Equal(0xBF, bb);
                    // check that there are not two BOMs
                    bb = reader.BaseStream.ReadByte();
                    Assert.NotEqual(0xEF, bb);
                    Assert.Equal('<', bb);
                }
            var fileContent = File.ReadAllLines(Path.Combine(destinationFolder, "TestCase15", "books_bom.xml"), Encoding.UTF8);

            Assert.Equal(38, fileContent.Length);
            string line1 = fileContent[0].Replace("\ufeff", ""); // remove BOM

            Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", line1);
            Assert.Equal("  <book category=\"general\">", fileContent[8]);
        }
Ejemplo n.º 14
0
        public void TestMultilineSearchAndReplace(string fileName, string newLine, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase16"), Path.Combine(destinationFolder, "TestCase16"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase16"), fileName),
                                                          SearchType.Regex, @"\w*\.$.P\w*", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);

            Assert.Single(results);
            var hits = results[0].SearchResults.Where(r => !r.IsContext).ToList();

            Assert.Equal(2, hits.Count);
            Assert.Single(hits[0].Matches);
            Assert.Equal("hendrerit.", hits[0].LineText.Substring(hits[0].Matches[0].StartLocation, hits[0].Matches[0].Length));

            Assert.Single(hits[1].Matches);
            Assert.Equal("Phasellus", hits[1].LineText.Substring(hits[1].Matches[0].StartLocation, hits[1].Matches[0].Length));


            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase16", fileName);
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, @"\w*\.$.P\w*", $"end.{newLine}Start", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);

            results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase16"), fileName),
                                  SearchType.Regex, @"\w*\.$.S\w*", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);
            Assert.Single(results);
            hits = results[0].SearchResults.Where(r => !r.IsContext).ToList();
            Assert.Equal(2, hits.Count);
            Assert.Single(hits[0].Matches);
            Assert.Equal("end.", hits[0].LineText.Substring(hits[0].Matches[0].StartLocation, hits[0].Matches[0].Length));

            Assert.Single(hits[1].Matches);
            Assert.Equal("Start", hits[1].LineText.Substring(hits[1].Matches[0].StartLocation, hits[1].Matches[0].Length));
        }
Ejemplo n.º 15
0
        public void TestReplaceWithNewLineWorks()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase8", destinationFolder + "\\TestCase8", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);

            string testFile = Path.Combine(destinationFolder, @"TestCase8\test.txt");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".txt" }
            };

            core.Replace(files, SearchType.Regex, "here", "\\\\n", GrepSearchOption.None, -1);
            Assert.Equal(File.ReadAllText(testFile, Encoding.ASCII).Trim().Split('\n').Length, 2);
        }
Ejemplo n.º 16
0
        public void TestGuidxReplaceWithPatternRegex()
        {
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase9"), Path.Combine(destinationFolder, "TestCase9"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase9"), "guidx.txt"),
                                                          SearchType.Regex, "h\\wre", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(6, results[0].SearchResults.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destinationFolder, @"TestCase9\guidx.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            // all instances of the same string matched will get the same guid
            core.Replace(files, SearchType.Regex, "h\\wre", "$(guidx)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(testFile, Encoding.UTF8).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            Dictionary <string, int> uniqueGuids = new Dictionary <string, int>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.ContainsKey(match.Value))
                {
                    uniqueGuids[match.Value] = 1;
                }
                else
                {
                    uniqueGuids[match.Value]++;
                }
            }
            Assert.Equal(2, uniqueGuids.Keys.Count);
        }
Ejemplo n.º 17
0
        public void TestReplaceWithPattern(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase9"), Path.Combine(destFolder, "TestCase9"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase9"), "test.txt"),
                                                          type, "here", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(6, results[0].SearchResults.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase9\test.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(testFile, Encoding.UTF8).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All GUIDs should be unique.");
                }
            }
        }
Ejemplo n.º 18
0
        public void TestSearchAndReplaceXPath(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "app.config"), SearchType.XPath,
                                                          "//appSettings", GrepSearchOption.CaseSensitive, -1);

            Assert.Single(results);
            Assert.Single(results[0].Matches);

            // mark all matches for replace
            foreach (var result in results)
            {
                foreach (var match in result.Matches)
                {
                    match.ReplaceMatch = true;
                }
            }

            string            testFile = Path.Combine(destFolder, @"TestCase4\app.config");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            string replaceText = "<add key=\"test\" value=\"true\" />";

            core.Replace(files, SearchType.XPath, "//appSettings", replaceText, GrepSearchOption.None, -1);

            results = core.Search(new string[] { testFile }, SearchType.XPath, "//add", GrepSearchOption.None, -1);
            Assert.Single(results);
            Assert.Single(results[0].Matches);
            var hit = results[0].SearchResults.Where(r => !r.IsContext).ToArray();

            Assert.Single(hit);
            Assert.Contains("key=\"test\"", hit[0].LineText);
            Assert.Contains("value=\"true\"", hit[0].LineText);
        }
Ejemplo n.º 19
0
        public void TestSearchAndReplaceXPathAttribute(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase15"), "books.xml"), SearchType.XPath,
                                                          "/bookstore/book/title[1]/@lang", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase15\books.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            string replaceText = "dnGrep";

            core.Replace(files, SearchType.XPath, "/bookstore/book/title[1]/@lang", replaceText, GrepSearchOption.None, -1);

            results = core.Search(new string[] { testFile }, SearchType.XPath, "/bookstore/book/title[1]/@lang", GrepSearchOption.None, -1);
            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);
            var hits = results[0].SearchResults.Where(r => !r.IsContext).ToArray();

            Assert.Equal(5, hits.Length);
            foreach (var line in hits)
            {
                Assert.Contains("lang=\"dnGrep\"", line.LineText);
            }
        }
Ejemplo n.º 20
0
        private void doSearchReplace(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (!workerSearchReplace.CancellationPending)
                {
                    timer = DateTime.Now;
                    if (e.Argument == SEARCH_KEY)
                    {
                        int sizeFrom = 0;
                        int sizeTo   = 0;
                        if (!IsAllSizes)
                        {
                            sizeFrom = Utils.ParseInt(tbFileSizeFrom.Text, 0);
                            sizeTo   = Utils.ParseInt(tbFileSizeTo.Text, 0);
                        }

                        string filePattern = "*.*";
                        if (rbFileRegex.Checked)
                        {
                            filePattern = ".*";
                        }

                        if (!string.IsNullOrEmpty(tbFilePattern.Text))
                        {
                            filePattern = tbFilePattern.Text;
                        }

                        if (rbFileAsterisk.Checked)
                        {
                            filePattern = filePattern.Replace("\\", "");
                        }

                        string[] files;

                        if (DoSearchInResults)
                        {
                            List <string> filesFromSearch = new List <string>();
                            foreach (GrepSearchResult result in searchResults)
                            {
                                if (!filesFromSearch.Contains(result.FileNameReal))
                                {
                                    filesFromSearch.Add(result.FileNameReal);
                                }
                            }
                            files = filesFromSearch.ToArray();
                        }
                        else
                        {
                            files = Utils.GetFileList(tbFolderName.Text, filePattern, rbFileRegex.Checked, cbIncludeSubfolders.Checked,
                                                      cbIncludeHiddenFolders.Checked, sizeFrom, sizeTo);
                        }
                        GrepCore grep = new GrepCore();
                        grep.ShowLinesInContext       = Properties.Settings.Default.ShowLinesInContext;
                        grep.LinesBefore              = Properties.Settings.Default.ContextLinesBefore;
                        grep.LinesAfter               = Properties.Settings.Default.ContextLinesAfter;
                        grep.PreviewFilesDuringSearch = Properties.Settings.Default.PreviewResults;

                        grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile);
                        List <GrepSearchResult> results = null;

                        GrepSearchOption searchOptions = GrepSearchOption.None;
                        if (cbMultiline.Checked)
                        {
                            searchOptions |= GrepSearchOption.Multiline;
                        }
                        if (cbCaseSensitive.Checked)
                        {
                            searchOptions |= GrepSearchOption.CaseSensitive;
                        }

                        if (rbRegexSearch.Checked)
                        {
                            results = grep.Search(files, SearchType.Regex, tbSearchFor.Text, searchOptions, CodePage);
                        }
                        else if (rbXPathSearch.Checked)
                        {
                            results = grep.Search(files, SearchType.XPath, tbSearchFor.Text, searchOptions, CodePage);
                        }
                        else
                        {
                            results = grep.Search(files, SearchType.PlainText, tbSearchFor.Text, searchOptions, CodePage);
                        }

                        grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile);
                        if (results != null)
                        {
                            searchResults = new List <GrepSearchResult>(results);
                            e.Result      = results.Count;
                        }
                        else
                        {
                            searchResults = new List <GrepSearchResult>();
                            e.Result      = 0;
                        }
                    }
                    else
                    {
                        GrepCore grep = new GrepCore();
                        grep.ShowLinesInContext       = Properties.Settings.Default.ShowLinesInContext;
                        grep.LinesBefore              = Properties.Settings.Default.ContextLinesBefore;
                        grep.LinesAfter               = Properties.Settings.Default.ContextLinesAfter;
                        grep.PreviewFilesDuringSearch = Properties.Settings.Default.PreviewResults;

                        grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile);
                        List <string> files = new List <string>();
                        foreach (GrepSearchResult result in searchResults)
                        {
                            if (!result.ReadOnly)
                            {
                                files.Add(result.FileNameReal);
                            }
                        }

                        GrepSearchOption searchOptions = GrepSearchOption.None;
                        if (cbMultiline.Checked)
                        {
                            searchOptions |= GrepSearchOption.Multiline;
                        }
                        if (cbCaseSensitive.Checked)
                        {
                            searchOptions |= GrepSearchOption.CaseSensitive;
                        }

                        if (rbRegexSearch.Checked)
                        {
                            e.Result = grep.Replace(files.ToArray(), SearchType.Regex, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage);
                        }
                        else if (rbXPathSearch.Checked)
                        {
                            e.Result = grep.Replace(files.ToArray(), SearchType.XPath, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage);
                        }
                        else
                        {
                            e.Result = grep.Replace(files.ToArray(), SearchType.PlainText, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage);
                        }

                        grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogException(LogLevel.Error, ex.Message, ex);
                if (e.Argument == SEARCH_KEY)
                {
                    MessageBox.Show("Search failed! See error log.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Replace failed! See error log.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }