コード例 #1
0
        public void GetFileNameKeyTest()
        {
            const string TestFileName1 = @"c:\temp\This Is Some Folder\This Is Some File.cs";
            const string TestFileName2 = @"c:\temp\this is some folder\this is some file.cs";
            const string TestFileName3 = @"e:\temp\this is some folder\this is some file.cs";
            const string TestKey       = "_1132715649";

            //
            // The same key should be returned accross multiple runs.
            //
            string key1 = BackupUtilities.CreateFileNameKey(TestFileName1);

            Assert.AreEqual(TestKey, key1);

            //
            // Case should be ignored
            //
            string key2 = BackupUtilities.CreateFileNameKey(TestFileName2);

            Assert.AreEqual(TestKey, key2);

            //
            // Different files should produce different keys
            //
            string key3 = BackupUtilities.CreateFileNameKey(TestFileName3);

            Assert.IsNotNull(key3, "Key should not be null.");
            Assert.IsNotEmpty(key3, "Key should not be empty.");
            Assert.AreNotEqual(key2, key3, "Keys should be unique per file.");
        }
コード例 #2
0
 public void GetFileNameKeyEmptyTest()
 {
     Assert.Throws <ArgumentException>(
         delegate
     {
         string key = BackupUtilities.CreateFileNameKey(string.Empty);
     });
 }
コード例 #3
0
 public void BackupFilesNullBackupRootTest()
 {
     Assert.Throws <ArgumentException>(
         delegate
     {
         BackupUtilities.BackupFiles(null, "123456789", new string[] { "c:\test\test.cs" });
     });
 }
コード例 #4
0
 public void RestoreFilesNullKeyTest()
 {
     Assert.Throws <ArgumentException>(
         delegate
     {
         BackupUtilities.RestoreFiles("c:\temp", null);
     });
 }
コード例 #5
0
 public void RestoreFilesNullBackupRootTest()
 {
     Assert.Throws <ArgumentException>(
         delegate
     {
         BackupUtilities.RestoreFiles(null, "123456789");
     });
 }
コード例 #6
0
 public void GetFileNameKeyNullTest()
 {
     Assert.Throws <ArgumentNullException>(
         delegate
     {
         string key = BackupUtilities.CreateFileNameKey(null);
     });
 }
コード例 #7
0
 public void BackupFilesNullKeyTest()
 {
     Assert.Throws <ArgumentException>(
         delegate
     {
         BackupUtilities.BackupFiles("c:\temp", null, new string[] { "c:\test\test.cs" });
     });
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: wvdvegt/narrange
        /// <summary>
        /// Runs NArrange using the specified arguments.
        /// </summary>
        /// <param name="logger">Logger for messages.</param>
        /// <param name="commandArgs">Command arguments.</param>
        /// <returns>True if succesful, otherwise false.</returns>
        public static bool Run(ILogger logger, CommandArguments commandArgs)
        {
            bool success = true;

            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            else if (commandArgs == null)
            {
                throw new ArgumentNullException("commandArgs");
            }

            if (commandArgs.Restore)
            {
                logger.LogMessage(LogLevel.Verbose, "Restoring {0}...", commandArgs.Input);
                string key = BackupUtilities.CreateFileNameKey(commandArgs.Input);
                try
                {
                    success = BackupUtilities.RestoreFiles(BackupUtilities.BackupRoot, key);
                }
                catch (Exception ex)
                {
                    logger.LogMessage(LogLevel.Warning, ex.Message);
                    success = false;
                }

                if (success)
                {
                    logger.LogMessage(LogLevel.Info, "Restored");
                }
                else
                {
                    logger.LogMessage(LogLevel.Error, "Restore failed");
                }
            }
            else
            {
                //
                // Arrange the source code file
                //
                FileArranger fileArranger = new FileArranger(commandArgs.Configuration, logger);
                success = fileArranger.Arrange(commandArgs.Input, commandArgs.Output, commandArgs.Backup);

                if (!success)
                {
                    logger.LogMessage(LogLevel.Error, "Unable to arrange {0}.", commandArgs.Input);
                }
                else
                {
                    logger.LogMessage(LogLevel.Info, "Arrange successful.");
                }
            }

            return(success);
        }
コード例 #9
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            ((Logger)logger).EmitHeader();

            //! Get Active Document Path.
            DTE dte = (DTE)ServiceProvider.GetServiceAsync(typeof(DTE))?.Result;

            if (dte == null)
            {
                logger.LogMessage(LogLevel.Error, "NArrange Could not obtain DTE.", new object[] { });

                return;
            }

            string document = dte?.ActiveDocument?.FullName;

            if (String.IsNullOrEmpty(document))
            {
                logger.LogMessage(LogLevel.Error, "NArrange Could not obtain the Path of the ActiveDocument.", new object[] { });

                return;
            }

            //! No CommandArguments (i.e. null).
            FileArranger fileArranger = new FileArranger(null, logger);

            string key = BackupUtilities.CreateFileNameKey(document);

            if (Directory.Exists(Path.Combine(BackupUtilities.BackupRoot, key)))
            {
                logger.LogMessage(LogLevel.Info, $"Restoring backup from {Path.Combine(BackupUtilities.BackupRoot, key)}...");

                //! Try to Undo NArrange of the Active File.
                Boolean success = BackupUtilities.RestoreFiles(BackupUtilities.BackupRoot, key);

                switch (success)
                {
                case true:
                    logger.LogMessage(LogLevel.Info, "Undo NArrange Successful.", new object[] { });
                    break;

                case false:
                    logger.LogMessage(LogLevel.Error, "Undo NArrange Failure.", new object[] { });
                    break;
                }
            }
            else
            {
                logger.LogMessage(LogLevel.Warning, "NArrange backup folder not found.", new object[] { });
            }
        }
コード例 #10
0
        public void ZipAndUnzipTest()
        {
            string sourceFolder      = BackupUtilities.CreateTempFilePath();
            string destinationFolder = BackupUtilities.CreateTempFilePath();
            string zipFile           = BackupUtilities.CreateTempFilePath() + ".zip";

            try
            {
                Directory.CreateDirectory(sourceFolder);

                string file1Text = "This is test file 1.";
                string file2Text = "This is test file 2.";
                string file3Text = "This is test file 3.";

                string file1          = Path.Combine(sourceFolder, "File1.txt");
                string file2Directory = Path.Combine(sourceFolder, "a");
                string file2          = Path.Combine(file2Directory, "File2.txt");
                string file3Directory = Path.Combine(sourceFolder, "b");
                string file3          = Path.Combine(file3Directory, "File3.txt");

                File.WriteAllText(file1, file1Text);
                Directory.CreateDirectory(file2Directory);
                File.WriteAllText(file2, file2Text);
                Directory.CreateDirectory(file3Directory);
                File.WriteAllText(file3, file3Text);

                ZipUtilities.Zip(sourceFolder, zipFile);

                Assert.IsTrue(File.Exists(zipFile), "Expected zip file to exist.");
                TestUtilities.AssertNotEmpty(zipFile);

                ZipUtilities.Unzip(zipFile, destinationFolder);

                FileSystemInfo[] fileSystemInfos =
                    new DirectoryInfo(destinationFolder).GetFileSystemInfos();
                Assert.IsTrue(fileSystemInfos.Length > 0, "Unzip did not create any files in the destination directory.");

                file1          = Path.Combine(destinationFolder, "File1.txt");
                file2Directory = Path.Combine(destinationFolder, "a");
                file2          = Path.Combine(file2Directory, "File2.txt");
                file3Directory = Path.Combine(destinationFolder, "b");
                file3          = Path.Combine(file3Directory, "File3.txt");

                Assert.IsTrue(File.Exists(file1), "Unzipped file was not found.");
                Assert.AreEqual(file1Text, File.ReadAllText(file1), "Unexpected file contents.");
                Assert.IsTrue(File.Exists(file2), "Unzipped file was not found.");
                Assert.AreEqual(file2Text, File.ReadAllText(file2), "Unexpected file contents.");
                Assert.IsTrue(File.Exists(file3), "Unzipped file was not found.");
                Assert.AreEqual(file3Text, File.ReadAllText(file3), "Unexpected file contents.");
            }
            finally
            {
                try
                {
                    Directory.Delete(sourceFolder, true);
                    Directory.Delete(destinationFolder, true);
                    File.Delete(zipFile);
                }
                catch
                {
                }
            }
        }
コード例 #11
0
 public void BackupFilesNullBackupRootTest()
 {
     Assert.Throws(typeof(ArgumentException), () => BackupUtilities.BackupFiles(null, "123456789", new string[] { "c:\test\test.cs" }));
 }
コード例 #12
0
 public void RestoreFilesNullKeyTest()
 {
     Assert.Throws(typeof(ArgumentException), () => BackupUtilities.RestoreFiles("c:\temp", null));
 }
コード例 #13
0
 public void GetFileNameKeyEmptyTest()
 {
     string key = BackupUtilities.CreateFileNameKey(string.Empty);
 }
コード例 #14
0
 public void BackupFilesNullKeyTest()
 {
     BackupUtilities.BackupFiles("c:\temp", null, new string[] { "c:\test\test.cs" });
 }
コード例 #15
0
 public void GetFileNameKeyEmptyTest()
 {
     Assert.Throws(typeof(ArgumentException), () => { string key = BackupUtilities.CreateFileNameKey(string.Empty); });
 }
コード例 #16
0
 public void GetFileNameKeyNullTest()
 {
     Assert.Throws(typeof(ArgumentNullException), () => { string key = BackupUtilities.CreateFileNameKey(null); });
 }
コード例 #17
0
 public void GetFileNameKeyNullTest()
 {
     string key = BackupUtilities.CreateFileNameKey(null);
 }
コード例 #18
0
 public void RestoreFilesNullBackupRootTest()
 {
     Assert.Throws(typeof(ArgumentException), () => BackupUtilities.RestoreFiles(null, "123456789"));
 }
コード例 #19
0
 public void RestoreFilesNullBackupRootTest()
 {
     BackupUtilities.RestoreFiles(null, "123456789");
 }
コード例 #20
0
        public void BackupAndRestoreTest()
        {
            string backupRoot        = BackupUtilities.CreateTempFilePath();
            string sourceFolder      = BackupUtilities.CreateTempFilePath();
            string destinationFolder = BackupUtilities.CreateTempFilePath();

            try
            {
                Directory.CreateDirectory(sourceFolder);

                string file1Text = "This is test file 1.";
                string file2Text = "This is test file 2.";
                string file3Text = "This is test file 3.";

                string file1          = Path.Combine(sourceFolder, "File1.txt");
                string file2Directory = Path.Combine(sourceFolder, "a");
                string file2          = Path.Combine(file2Directory, "File2.txt");
                string file3Directory = Path.Combine(sourceFolder, "b");
                string file3          = Path.Combine(file3Directory, "File3.txt");

                File.WriteAllText(file1, file1Text);
                Directory.CreateDirectory(file2Directory);
                File.WriteAllText(file2, file2Text);
                Directory.CreateDirectory(file3Directory);
                File.WriteAllText(file3, file3Text);

                string key            = BackupUtilities.CreateFileNameKey("Test");
                string backupLocation = BackupUtilities.BackupFiles(
                    backupRoot,
                    key,
                    new string[] { file1, file2, file3 });

                string zipFile = Path.Combine(backupLocation, "files.zip");
                Assert.IsTrue(File.Exists(zipFile), "Expected zip file to exist after backup.");
                TestUtilities.AssertNotEmpty(zipFile);

                //
                // Modify the original files
                //
                File.WriteAllText(file1, "Blah");
                File.WriteAllText(file2, "Blah");
                File.Delete(file3);

                BackupUtilities.RestoreFiles(backupRoot, key);

                Assert.IsTrue(File.Exists(file1), "Restored file was not found.");
                Assert.AreEqual(file1Text, File.ReadAllText(file1), "Unexpected file contents.");
                Assert.IsTrue(File.Exists(file2), "Restored file was not found.");
                Assert.AreEqual(file2Text, File.ReadAllText(file2), "Unexpected file contents.");
                Assert.IsTrue(File.Exists(file3), "Restored file was not found.");
                Assert.AreEqual(file3Text, File.ReadAllText(file3), "Unexpected file contents.");
            }
            finally
            {
                try
                {
                    Directory.Delete(sourceFolder, true);
                    Directory.Delete(destinationFolder, true);
                    Directory.Delete(backupRoot, true);
                }
                catch
                {
                }
            }
        }
コード例 #21
0
 public void RestoreFilesNullKeyTest()
 {
     BackupUtilities.RestoreFiles("c:\temp", null);
 }
コード例 #22
0
 public void BackupFilesNullKeyTest()
 {
     Assert.Throws(typeof(ArgumentException), () => BackupUtilities.BackupFiles("c:\temp", null, new string[] { "c:\test\test.cs" }));
 }
コード例 #23
0
 public void BackupFilesNullBackupRootTest()
 {
     BackupUtilities.BackupFiles(null, "123456789", new string[] { "c:\test\test.cs" });
 }