Exemple #1
0
        public void ExpectCompletingTheOpInProcessResultToSkipRemainingTasks()
        {
            var task1 = new TestTask <int>(1);
            var task2 = new TestTask <int>(2);
            var task3 = new TestTask <int>(3);

            var operation = new AsyncOperation();

            operation.Add(task1);
            operation.Add(task2, (result, op) =>
            {
                var finalResult = new Result <string>("done");
                op.Complete(finalResult);
            });
            operation.Add(task3);

            Result <string> overallResult = null;

            operation.Execute(result =>
            {
                overallResult = (Result <string>)result;
            });

            Assert.IsTrue(task1.IsExecuted);
            Assert.IsTrue(task2.IsExecuted);
            Assert.IsFalse(task3.IsExecuted);
            Assert.IsFalse(overallResult.IsError);
            Assert.AreEqual("done", overallResult.Data);
        }
Exemple #2
0
        public void ExpectAddedTasksToBeExecutedInOrder()
        {
            var results = new List <int>();

            var task1 = new TestTask <int>(1);
            var task2 = new TestTask <int>(2);
            var task3 = new TestTask <int>(3);

            Action <Result <int>, IOperationControls> processResult = (result, op) =>
            {
                results.Add(result.Data);
            };

            var operation = new AsyncOperation();

            operation.Add(task1, processResult);
            operation.Add(task2, processResult);
            operation.Add(task3, processResult);

            operation.Execute();

            Assert.AreEqual(1, results[0]);
            Assert.AreEqual(2, results[1]);
            Assert.AreEqual(3, results[2]);
        }
Exemple #3
0
        public void ExpectFailingTaskToFailTheOperation()
        {
            var task1 = new TestTask <int>(1);
            var task2 = new TestTask <int>(2)
            {
                Fails = true
            };
            var task3 = new TestTask <int>(3);


            var operation = new AsyncOperation();

            operation.Add(task1);
            operation.Add(task2);
            operation.Add(task3);

            OperationResult overallResult = null;

            operation.Execute(result =>
            {
                overallResult = result;
            });

            Assert.IsTrue(task1.IsExecuted);
            Assert.IsTrue(task2.IsExecuted);
            Assert.IsFalse(task3.IsExecuted);
            Assert.IsTrue(overallResult.IsError);
        }
Exemple #4
0
        private static void SyncProjectInternal(PackageListStatus packageListStatus, Action <OperationResult> onComplete)
        {
            if (packageListStatus.IsProjectUpToDate)
            {
                onComplete(new OperationResult());
                return;
            }

            var operation = new AsyncOperation();

            foreach (var packageStatus in packageListStatus.Packages)
            {
                var version     = packageStatus.RequiredVersion;
                var packageName = packageStatus.PackageName;
                var gitUrl      = packageStatus.GitUrl;
                var relativeInstallDirectory = Settings.RelativePackagesDirectoryPath + packageName;
                var absoluteInstallDirectory = Settings.AbsolutePackagesDirectoryPath + packageName;

                if (packageStatus.IsMissing)
                {
                    operation.Add(new AddSubmoduleTask(gitUrl, relativeInstallDirectory));
                    operation.Add(new CheckoutSubmoduleTask(absoluteInstallDirectory, version));
                }
                else if (packageStatus.IsOnWrongVersion)
                {
                    operation.Add(new CheckoutSubmoduleTask(absoluteInstallDirectory, version));
                }
            }

            operation.Execute(onComplete);
        }
Exemple #5
0
 public void ExpectAddToThrowIfTheOperationIsRunning()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         var operation = new AsyncOperation();
         operation.Add(new TestTask <int>(1), (result, op) =>
         {
             operation.Add(new TestTask <int>(2), (result1, op1) => {});
         });
         operation.Execute();
     });
 }
Exemple #6
0
 public static void AddSync <TResult>(this AsyncOperation operation, Func <TResult> task, Action <TResult, IOperationControls> processResult = null)
     where TResult : OperationResult
 {
     operation.Add(new BasicTask <TResult>(onComplete =>
     {
         onComplete(task());
     }), processResult);
 }
Exemple #7
0
 public void ExpectAddToThrowWithNullParameters()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var operation = new AsyncOperation();
         operation.Add <OperationResult>(null);
     });
 }
Exemple #8
0
        public static AsyncOperation RemoveSubmodule(string localRelativeSubmodulePath)
        {
            if (localRelativeSubmodulePath == null)
            {
                throw new ArgumentNullException("localRelativeSubmodulePath");
            }

            var projectRoot = Project.RootDirectory;

            var operation = new AsyncOperation();

            // Remove from gitmodules
            operation.AddSync(() =>
            {
                RemoveFromGitConfig(projectRoot + "/.gitmodules", localRelativeSubmodulePath);
            });

            // stage that change
            operation.Add(new GitTask("add .gitmodules"));

            // remove lines in .git/config
            operation.AddSync(() =>
            {
                RemoveFromGitConfig(projectRoot + "/.git/config", localRelativeSubmodulePath);
            });

            // Delete the actual files
            operation.Add(new GitTask("rm --cached -f " + localRelativeSubmodulePath));

            // Run rm -rf .git/modules/path_to_submodule (no trailing slash).
            operation.AddSync(() =>
            {
                var directory = projectRoot + "/.git/modules/" + localRelativeSubmodulePath;
                FileUtil.DeleteFileOrDirectory(directory);
            });

            // Delete the now untracked submodule files rm -rf path_to_submodule
            operation.AddSync(() =>
            {
                var directory = projectRoot + "/" + localRelativeSubmodulePath;
                FileUtil.DeleteFileOrDirectory(directory);
            });

            return(operation);
        }
Exemple #9
0
        public static AsyncOperation CompilePackageListStatus()
        {
            var operation         = new AsyncOperation();
            var packageListStatus = new PackageListStatus();

            foreach (var package in RootStore.Instance.Packages.InstalledPackages.Value.Packages)
            {
                var packageStatus = new PackageStatus
                {
                    RequiredVersion = package.Version,
                    PackageName     = package.Name,
                    GitUrl          = package.GitUrl
                };
                packageListStatus.Packages.Add(packageStatus);
                var packageDirectory = Settings.AbsolutePackagesDirectoryPath + package.Name;
                if (!Directory.Exists(packageDirectory))
                {
                    packageStatus.IsMissing = true;
                    continue;
                }

                var getGitBranchOrTagTask = new GetGitBranchOrTagTask(package);
                var packageVersion        = package.Version;
                operation.Add(getGitBranchOrTagTask, (result, op) =>
                {
                    packageStatus.IsOnWrongVersion = result.Data != packageVersion;
                });
            }

            operation.AddSync(() =>
            {
                Debug.Log("Project package status: ");

                foreach (var package in packageListStatus.Packages)
                {
                    Debug.Log(package);
                }

                Debug.Log("The project is " +
                          (packageListStatus.IsProjectUpToDate ? "" : "not ") + "up to date");

                return(new Result <PackageListStatus>(packageListStatus));
            }, (result, op) =>
            {
                op.Complete(result);
            });

            return(operation);
        }
Exemple #10
0
        public void ExpectFailingOpInProcessResultToFailTheOperation()
        {
            var task1 = new TestTask <int>(1);
            var task2 = new TestTask <int>(2);
            var task3 = new TestTask <int>(3);

            var operation = new AsyncOperation();

            operation.Add(task1);
            operation.Add(task2, (result, op) => { op.Fail(new Error("test-error", "Error")); });
            operation.Add(task3);

            OperationResult overallResult = null;

            operation.Execute(result =>
            {
                overallResult = result;
            });

            Assert.IsTrue(task1.IsExecuted);
            Assert.IsTrue(task2.IsExecuted);
            Assert.IsFalse(task3.IsExecuted);
            Assert.IsTrue(overallResult.IsError);
        }
Exemple #11
0
        public static AsyncOperation SyncProject()
        {
            var tasks = new AsyncOperation();

            PackageListStatus packageListStatus = null;

            // Compile package list status
            tasks.Add(new BasicTask <OperationResult>(CompileProjectPackageListStatus), (result, op) =>
            {
                if (result.IsError)
                {
                    Debug.LogError("Failed to compile package status");
                    Debug.LogError(result.Error.Message);
                    return;
                }

                packageListStatus = ((Result <PackageListStatus>)result).Data;
            });

            // Sync project
            tasks.Add(new BasicTask <OperationResult>(onComplete => SyncProjectInternal(packageListStatus, onComplete)));

            return(tasks);
        }
Exemple #12
0
        public static void AddSync(this AsyncOperation operation, Action task)
        {
            operation.Add(new BasicTask <OperationResult>(onComplete =>
            {
                var result = new OperationResult();

                try
                {
                    task();
                }
                catch (Exception e)
                {
                    result.Err(new Error(e));
                }

                onComplete(result);
            }));
        }
Exemple #13
0
        public void ExpectAddedTaskToBeExecutedAndDataToBePassedToProcessResultCallback()
        {
            var isProcessResultExecuted = false;
            var resultValue             = 0;

            var task = new TestTask <int>(42);

            var operation = new AsyncOperation();

            operation.Add(task, (r, op) =>
            {
                resultValue             = r.Data;
                isProcessResultExecuted = true;
            });

            operation.Execute();

            Assert.IsTrue(task.IsExecuted);
            Assert.IsTrue(isProcessResultExecuted);
            Assert.AreEqual(42, resultValue);
        }