<commit_before>﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BackyLogic;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Security.AccessControl;

namespace TestBacky
{
    [TestClass]
    public class TestRealFileSystem
    {
        //[TestMethod]
        public void UnmarkAsReadOnly()
        {
            UnmarkDirectoryAsReadOnlyRecursive(@"D:\TargetGuid");
        }

        [TestMethod]
        public void RealFileSystem_01_Run_backup_and_test_files_on_disk()
        {
            var source = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var target = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(target);
            Directory.CreateDirectory(source);

            try
            {
                var fs = new OSFileSystem();
                var expectedStates = SimulateRunningBackups(source, target, fs);
                TestsUtils.AssertState(fs, target, source, expectedStates);
            }
            finally
            {
                UnmarkDirectoryAsReadOnlyRecursive(target);
                Directory.Delete(target, true);
                Directory.Delete(source, true);
            }
        }

        [TestMethod]
        public void RealFileSystem_02_Run_clone_on_different_stages()
        {
            var backupSource = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var cloneTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var cloneSource = new CloneSource { BackupPath = backupTarget, OriginalSourcePath = backupSource };

            Directory.CreateDirectory(backupSource);
            Directory.CreateDirectory(backupTarget);
            Directory.CreateDirectory(cloneTarget);


            try
            {
                var fs = new OSFileSystem();
                var expectedStates = SimulateRunningBackups(backupSource, backupTarget, fs);

                for (int i = 0; i < expectedStates.Length; i++)
                {
                    Directory.Delete(cloneTarget, true);
                    var cmd = new CloneBackupCommand(fs, cloneSource, cloneTarget, i+1);
                    cmd.Execute();

                    var actual = Directory.GetFiles(cloneTarget, "*", SearchOption.AllDirectories)
                        .Select(x => EmulatorFile.FromlFileName(x, x.Replace(cloneTarget + "\\", ""), fs));
                    TestsUtils.AssertEmulatorFiles(fs, expectedStates[i], actual, "clone: " + i);
                }
            }
            finally
            {
                UnmarkDirectoryAsReadOnlyRecursive(backupTarget);
                Directory.Delete(backupSource, true);
                Directory.Delete(backupTarget, true);
                Directory.Delete(cloneTarget, true);
            }
        }


<commit_msg>added real file system test<commit_after>﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BackyLogic;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Security.AccessControl;

namespace TestBacky
{
    [TestClass]
    public class TestRealFileSystem
    {
        //[TestMethod]
        public void UnmarkAsReadOnly()
        {
            UnmarkDirectoryAsReadOnlyRecursive(@"D:\TargetGuid");
        }

        [TestMethod]
        public void RealFileSystem_01_Run_backup_and_test_files_on_disk()
        {
            var source = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var target = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(target);
            Directory.CreateDirectory(source);

            try
            {
                var fs = new OSFileSystem();
                var expectedStates = SimulateRunningBackups(source, target, fs);
                TestsUtils.AssertState(fs, target, source, expectedStates);
            }
            finally
            {
                UnmarkDirectoryAsReadOnlyRecursive(target);
                Directory.Delete(target, true);
                Directory.Delete(source, true);
            }
        }

        [TestMethod]
        public void RealFileSystem_02_Run_clone_on_different_stages()
        {
            var backupSource = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var cloneTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var cloneSource = new CloneSource { BackupPath = backupTarget, OriginalSourcePath = backupSource };

            Directory.CreateDirectory(backupSource);
            Directory.CreateDirectory(backupTarget);
            Directory.CreateDirectory(cloneTarget);


            try
            {
                var fs = new OSFileSystem();
                var expectedStates = SimulateRunningBackups(backupSource, backupTarget, fs);

                for (int i = 0; i < expectedStates.Length; i++)
                {
                    Directory.Delete(cloneTarget, true);
                    var cmd = new CloneBackupCommand(fs, cloneSource, cloneTarget, i+1);
                    cmd.Execute();

                    var actual = Directory.GetFiles(cloneTarget, "*", SearchOption.AllDirectories)
                        .Select(x => EmulatorFile.FromlFileName(x, x.Replace(cloneTarget + "\\", ""), fs));
                    TestsUtils.AssertEmulatorFiles(fs, expectedStates[i], actual, "clone: " + i);
                }
            }
            finally
            {
                UnmarkDirectoryAsReadOnlyRecursive(backupTarget);
                Directory.Delete(backupSource, true);
                Directory.Delete(backupTarget, true);
                Directory.Delete(cloneTarget, true);
            }
        }

        [TestMethod]
        public void RealFileSystem_03_Run_backup_the_backup()
        {
            var backupSource1 = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupSource2 = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupSource3 = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            var backupTheBackupTarget = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
            

            Directory.CreateDirectory(backupSource1);
            Directory.CreateDirectory(backupSource2);
            Directory.CreateDirectory(backupSource3);
            Directory.CreateDirectory(backupTarget);
            Directory.CreateDirectory(backupTheBackupTarget);


            try
            {
                var fs = new OSFileSystem();
                SimulateRunningBackups(backupSource1, backupTarget, fs);
                SimulateRunningBackups(backupSource2, backupTarget, fs);

                // Backup the backup
                var btbCommand = new BackupTheBackupCommand(fs, backupTarget, backupTheBackupTarget);
                btbCommand.Execute();

                // Running another backup with a new source and then backup the backup again
                SimulateRunningBackups(backupSource3, backupTarget, fs);
                btbCommand.Execute();

                // Run regular backup on one of the source
                WriteFile(backupSource1, "file1234.txt", "hello1234", DateTime.Now);
                var backupCmd = new RunBackupCommand(fs, backupSource1, backupTarget);
                backupCmd.Execute();

                btbCommand.Execute();

                var expected = fs.EnumerateFiles(backupTarget).Select(x => EmulatorFile.FromlFileName(x, x.Replace(backupTarget + "\\", ""), fs)).ToArray();
                var actual = fs.EnumerateFiles(backupTheBackupTarget).Select(x => EmulatorFile.FromlFileName(x, x.Replace(backupTheBackupTarget + "\\", ""), fs)).ToArray();
                TestsUtils.AssertEmulatorFiles(fs, expected, actual, "");
            }
            finally
            {
                UnmarkDirectoryAsReadOnlyRecursive(backupTarget);
                UnmarkDirectoryAsReadOnlyRecursive(backupTheBackupTarget);
                Directory.Delete(backupSource1, true);
                Directory.Delete(backupSource2, true);
                Directory.Delete(backupSource3, true);
                Directory.Delete(backupTarget, true);
                Directory.Delete(backupTheBackupTarget, true);
            }
        }



{'fraction_non_alphanumeric': 0.0933793400905758, 'fraction_numerical': 0.0038818201423334053, 'mean_word_length': 2.246412320616031, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2622718', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2220, 'n_words': 395}