Ejemplo n.º 1
0
        /// <summary>
        /// The main entry of the program
        /// </summary>
        /// <param name="args">The arguments</param>
        public static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                PrintUsage();
                return;
            }

            IEnumerable<string> copyDirs = GetDirectoriesToCopy(args);
            string srcRoot = args[0];
            string destRoot = args[1];

            if (copyDirs == null || !copyDirs.Any())
            {
                throw new InvalidOperationException("There were no directories found to back up.");
            }

            string logDir = Path.Combine(Path.GetTempPath(), "backup");
            string logPathWithoutExtension = Path.Combine(logDir, "backup-" + DateTime.Now.ToString("yyyyMMdd"));
            string log = logPathWithoutExtension + ".log";
            string err = logPathWithoutExtension = ".err";
            if (!Directory.Exists(logDir))
            {
                Directory.CreateDirectory(logDir);
            }

            Copier copier = new Copier(new List<ILog>() { new ConsoleLog(), new FileLog(log, err) });

            foreach (string dir in copyDirs)
            {
                string src = Path.Combine(srcRoot, dir);
                string dest = Path.Combine(destRoot, dir);
                copier.UpdateDirectory(src, dest);
            }
        }
Ejemplo n.º 2
0
        public void CopyIfNewerDestinationDoesNotExist()
        {
            File.WriteAllText(this.file1, "file");
            Copier copier = new Copier(new MemoryLog());

            copier.TryCopyIfNewer(this.file1, this.file2).Should().BeTrue();
            File.ReadAllText(this.file2).Should().Be("file");
        }
Ejemplo n.º 3
0
 public void CopyIfNewerSourceDoesNotExist()
 {
     Action action = () =>
         {
             Copier copier = new Copier(new MemoryLog());
             copier.TryCopyIfNewer(this.file1, this.file2);
         };
     action.ShouldThrow<FileNotFoundException>();
 }
Ejemplo n.º 4
0
        public void CopyIfNewerSameTime()
        {
            File.WriteAllText(this.file1, "file1");
            File.Copy(this.file1, this.file2);
            Copier copier = new Copier(new MemoryLog());

            copier.TryCopyIfNewer(this.file1, this.file2).Should().BeFalse();
            File.ReadAllText(this.file2).Should().Be("file1");
        }
Ejemplo n.º 5
0
        public void CopyIfNewerSourceOlder()
        {
            File.WriteAllText(this.file1, "file1");
            Thread.Sleep(5);
            File.WriteAllText(this.file2, "file2");
            Copier copier = new Copier(new MemoryLog());

            copier.TryCopyIfNewer(this.file1, this.file2).Should().BeFalse();
            File.ReadAllText(this.file2).Should().Be("file2");
        }
Ejemplo n.º 6
0
 public void ExceptionsLogged()
 {
     File.WriteAllText(this.file1, "some text");
     MemoryLog log = new MemoryLog();
     Copier copier = new Copier(log);
     using (var fs = File.Open(this.file1, FileMode.Open)) // open file handle
     {
         copier.TryCopy(this.file1, this.file2).Should().BeFalse();
         log.Error.ToString().Should().Contain(this.file1);
         log.Error.ToString().Should().Contain(this.file2);
     }
 }
Ejemplo n.º 7
0
        public void ExistingFileOverwritten()
        {
            File.WriteAllText(this.file1, "some text");
            File.WriteAllText(this.file2, "some other text");
            MemoryLog log = new MemoryLog();
            Copier copier = new Copier(log);

            copier.TryCopy(this.file1, this.file2).Should().BeTrue();
            File.ReadAllText(this.file2).Should().Be("some text");
            log.Log.ToString().Should().Contain(this.file1);
            log.Log.ToString().Should().Contain(this.file2);
        }
Ejemplo n.º 8
0
        public void UpdateDirectorySubdirectories()
        {
            string dir1 = Path.Combine(Path.GetTempPath(), "dir1");
            string subdir1 = Path.Combine(dir1, "subpath");
            string dir2 = Path.Combine(Path.GetTempPath(), "dir2");
            string subdir2 = Path.Combine(dir2, "subpath");
            string file1 = Path.Combine(subdir1, "file");
            string file2 = Path.Combine(subdir2, "file");
            Directory.CreateDirectory(dir1);
            Directory.CreateDirectory(subdir1);
            File.WriteAllText(file1, "contents");
            Copier copier = new Copier(new MemoryLog());

            try
            {
                copier.UpdateDirectory(dir1, dir2);
                Directory.Exists(dir2).Should().BeTrue();
                Directory.Exists(subdir2).Should().BeTrue();
                File.Exists(file2).Should().BeTrue();
            }
            finally
            {

                File.Delete(file1);
                Directory.Delete(subdir1);
                Directory.Delete(dir1);

                File.Delete(file2);
                Directory.Delete(subdir2);
                Directory.Delete(dir2);
            }
        }
Ejemplo n.º 9
0
 public void UpdateDirectorySourceDoesNotExist()
 {
     string dir1 = Path.Combine(Path.GetTempPath(), "dir1");
     string dir2 = Path.Combine(Path.GetTempPath(), "dir2");
     Action action = () =>
         {
             Copier copier = new Copier(new MemoryLog());
             copier.UpdateDirectory(dir1, dir2);
         };
     action.ShouldThrow<DirectoryNotFoundException>();
 }
Ejemplo n.º 10
0
        public void NewFileCreated()
        {
            File.WriteAllText(this.file1, "some text");
            MemoryLog log = new MemoryLog();
            Copier copier = new Copier(log);

            copier.TryCopy(this.file1, this.file2).Should().BeTrue();
            File.ReadAllText(this.file2).Should().Be("some text");
            log.Log.ToString().Should().Contain(this.file1);
            log.Log.ToString().Should().Contain(this.file2);
        }