public void TestSearchRegexReturnsCorrectNumber() { Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dnGR\\wP", GrepSearchOption.CaseSensitive, -1); Assert.Equal(results.Count, 1); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.CaseSensitive, -1); Assert.Equal(results.Count, 0); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1); Assert.Equal(results.Count, 0); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.None, -1); Assert.Equal(results.Count, 1); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.None, -1); Assert.Equal(results.Count, 2); Assert.Equal(results[0].Matches.Count, 3); Assert.Equal(results[1].Matches.Count, 282); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.Multiline, -1); Assert.Equal(results.Count, 2); Assert.Equal(results[0].Matches.Count, 3); Assert.Equal(results[1].Matches.Count, 282); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.Multiline, -1); Assert.Equal(results.Count, 2); Assert.Equal(results[0].Matches.Count, 3); Assert.Equal(results[1].Matches.Count, 282); Assert.Empty(core.Search(null, SearchType.Regex, "string", GrepSearchOption.Multiline, -1)); Assert.Empty(core.Search(new string[] { }, SearchType.Regex, "string", GrepSearchOption.Multiline, -1)); }
public void WriteToCsvTest() { Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null); File.WriteAllText(destinationFolder + "\\test.csv", "hello"); var core = new GrepCore(); var results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "string", GrepSearchOption.None, -1); Assert.Equal(results.Count, 2); Assert.Equal(results[0].Matches.Count, 3); Assert.Equal(results[1].Matches.Count, 282); Utils.SaveResultsAsCSV(results, destinationFolder + "\\test.csv"); string[] stringLines = File.ReadAllLines(destinationFolder + "\\test.csv"); Assert.Equal(stringLines.Length, 177); Assert.Equal(stringLines[0].Split(',')[0].Trim(), "File Name"); Assert.Equal(stringLines[1].Split(',')[1].Trim(), "1"); Assert.Equal(stringLines[2].Split(',')[2].Trim(), "\"\tstring returnedLine = Utils.GetLine(body"); }
private void undo() { if (CanUndo) { MessageBoxResult response = MessageBox.Show("Undo will revert modified file(s) back to their original state. Any changes made to the file(s) after the replace will be overwritten. Are you sure you want to procede?", "Undo", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel); if (response == MessageBoxResult.Yes) { GrepCore core = new GrepCore(); bool result = core.Undo(UndoFolder); if (result) { MessageBox.Show("Files have been successfully reverted.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); Utils.DeleteTempFolder(); } else { MessageBox.Show("There was an error reverting files. Please examine the error log.", "Failure", MessageBoxButton.OK, MessageBoxImage.Error); } CanUndo = false; } } }
void grep_ProcessedFile(object sender, GrepCore.ProgressStatus progress) { workerSearchReplace.ReportProgress((int)progress.ProcessedFiles, progress); }
private void doSearchReplace(object sender, DoWorkEventArgs e) { try { if (!workerSearchReplace.CancellationPending) { timer = DateTime.Now; Dictionary<string, object> workerParams = (Dictionary<string, object>)e.Argument; //TODO: Check if this is needed MainViewModel param = (MainViewModel)workerParams["State"]; if (param.CurrentGrepOperation == GrepOperation.Search || param.CurrentGrepOperation == GrepOperation.SearchInResults) { int sizeFrom = 0; int sizeTo = 0; if (param.UseFileSizeFilter == FileSizeFilter.Yes) { sizeFrom = param.SizeFrom; sizeTo = param.SizeTo; } string filePatternInclude = "*.*"; if (param.TypeOfFileSearch == FileSearchType.Regex) filePatternInclude = ".*"; if (!string.IsNullOrEmpty(param.FilePattern)) filePatternInclude = param.FilePattern; string filePatternExclude = ""; if (!string.IsNullOrEmpty(param.FilePatternIgnore)) filePatternExclude = param.FilePatternIgnore; IEnumerable<string> files; Utils.CancelSearch = false; if (param.CurrentGrepOperation == GrepOperation.SearchInResults) { files = (List<string>)workerParams["Files"]; } else { files = Utils.GetFileListEx(FileOrFolderPath, filePatternInclude, filePatternExclude, param.TypeOfFileSearch == FileSearchType.Regex, param.IncludeSubfolder, param.IncludeHidden, param.IncludeBinary, sizeFrom, sizeTo); } if (Utils.CancelSearch) { e.Result = null; return; } if (param.TypeOfSearch == SearchType.Regex) { try { Regex pattern = new Regex(param.SearchFor); } catch (ArgumentException regException) { MessageBox.Show("Incorrect pattern: " + regException.Message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); e.Result = null; return; } } GrepCore grep = new GrepCore(); grep.SearchParams.FuzzyMatchThreshold = settings.Get<double>(GrepSettings.Key.FuzzyMatchThreshold); grep.SearchParams.LinesBefore = settings.Get<int>(GrepSettings.Key.ContextLinesBefore); grep.SearchParams.LinesAfter = settings.Get<int>(GrepSettings.Key.ContextLinesAfter); grep.SearchParams.ShowLinesInContext = settings.Get<bool>(GrepSettings.Key.ShowLinesInContext); GrepSearchOption searchOptions = GrepSearchOption.None; if (Multiline) searchOptions |= GrepSearchOption.Multiline; if (CaseSensitive) searchOptions |= GrepSearchOption.CaseSensitive; if (Singleline) searchOptions |= GrepSearchOption.SingleLine; if (WholeWord) searchOptions |= GrepSearchOption.WholeWord; if (StopAfterFirstMatch) searchOptions |= GrepSearchOption.StopAfterFirstMatch; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); e.Result = grep.Search(files, param.TypeOfSearch, param.SearchFor, searchOptions, param.CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); } else { GrepCore grep = new GrepCore(); grep.SearchParams.FuzzyMatchThreshold = settings.Get<double>(GrepSettings.Key.FuzzyMatchThreshold); grep.SearchParams.LinesBefore = settings.Get<int>(GrepSettings.Key.ContextLinesBefore); grep.SearchParams.LinesAfter = settings.Get<int>(GrepSettings.Key.ContextLinesAfter); grep.SearchParams.ShowLinesInContext = settings.Get<bool>(GrepSettings.Key.ShowLinesInContext); GrepSearchOption searchOptions = GrepSearchOption.None; if (Multiline) searchOptions |= GrepSearchOption.Multiline; if (CaseSensitive) searchOptions |= GrepSearchOption.CaseSensitive; if (Singleline) searchOptions |= GrepSearchOption.SingleLine; if (WholeWord) searchOptions |= GrepSearchOption.WholeWord; if (StopAfterFirstMatch) searchOptions |= GrepSearchOption.WholeWord; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); string[] files = ((List<string>)workerParams["Files"]).ToArray(); e.Result = grep.Replace(files, param.TypeOfSearch, Utils.GetBaseFolder(param.FileOrFolderPath), param.SearchFor, param.ReplaceWith, searchOptions, param.CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); } } } catch (Exception ex) { logger.LogException(LogLevel.Error, ex.Message, ex); bool isSearch = true; if (e.Argument is MainViewModel) { MainViewModel param = (MainViewModel)e.Argument; if (param.CurrentGrepOperation == GrepOperation.Search || param.CurrentGrepOperation == GrepOperation.SearchInResults) isSearch = true; else isSearch = false; } if (isSearch) MessageBox.Show("Search failed! See error log.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); else MessageBox.Show("Replace failed! See error log.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
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); }
public void SearchWIthMultipleLines(SearchType type) { Utils.CopyFiles(sourceFolder + "\\TestCase12", destinationFolder + "\\TestCase12", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase12", "issue-165.txt"), type, "asdf\r\nqwer", GrepSearchOption.Multiline, -1); Assert.Equal(results[0].Matches.Count, 1); Assert.Equal(results[0].SearchResults.Count, 5); }
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, destinationFolder + "\\TestCase8", "here", "\\n", GrepSearchOption.None, -1); Assert.Equal(File.ReadAllText(destinationFolder + "\\TestCase8\\test.txt", Encoding.ASCII).Trim().Split('\n').Length, 2); }
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, destinationFolder + "\\TestCase9", "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."); } }
public void TestSearchXPathReturnsCorrectResultsCount() { Utils.CopyFiles(sourceFolder + "\\TestCase4", destinationFolder + "\\TestCase4", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase4", "app.config"), SearchType.XPath, "//setting", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1); Assert.Equal(results.Count, 1); Assert.Equal(results[0].SearchResults.Count, 84); }
public void TestSearchWholeWord_Issue_114_Plain() { Utils.CopyFiles(sourceFolder + "\\TestCase10", destinationFolder + "\\TestCase10", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase10", "issue-114.txt"), SearchType.PlainText, "protected", GrepSearchOption.WholeWord, -1); Assert.Equal(results.Count, 1); Assert.Equal(results[0].SearchResults.Count, 1); }
public void TestResultSequence() { Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "test-file-plain-big.txt"), SearchType.PlainText, "string", GrepSearchOption.CaseSensitive, -1); Assert.Equal(results.Count, 1); var resultLines = results[0].GetLinesWithContext(3, 3); int lastLine = 0; foreach (var line in resultLines) { if (line.LineNumber <= lastLine) Assert.True(false, "Lines are not sequential"); lastLine = line.LineNumber; } }
public void TestSearchWithStopAfterFirstMatch() { Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.CaseSensitive, -1); Assert.True(results.Count > 1); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.StopAfterFirstMatch, -1); Assert.Equal(1, results.Count); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.CaseSensitive, -1); Assert.True(results.Count > 1); results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.StopAfterFirstMatch, -1); Assert.Equal(1, results.Count); }
public void TestSearchContainsValidPattern() { Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dnGREP", GrepSearchOption.CaseSensitive, -1); Assert.Equal(results.Count, 1); Assert.Equal("dnGREP", results[0].Pattern); }
private void undoToolStripMenuItem_Click(object sender, EventArgs e) { if (CanUndo) { DialogResult response = MessageBox.Show("Undo will revert modified file(s) back to their original state. Any changes made to the file(s) after the replace will be overwritten. Are you sure you want to procede?", "Undo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3); if (response == DialogResult.Yes) { GrepCore core = new GrepCore(); core.ShowLinesInContext = Properties.Settings.Default.ShowLinesInContext; core.LinesBefore = Properties.Settings.Default.ContextLinesBefore; core.LinesAfter = Properties.Settings.Default.ContextLinesAfter; core.PreviewFilesDuringSearch = Properties.Settings.Default.PreviewResults; bool result = core.Undo(undoFolder); if (result) { MessageBox.Show("Files have been successfully reverted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Utils.DeleteTempFolder(); } else { MessageBox.Show("There was an error reverting files. Please examine the error log.", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error); } CanUndo = false; } } }
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); } }