Ejemplo n.º 1
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.º 2
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.º 3
0
        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;
                }
            }
        }