コード例 #1
0
        public void CloneWhenExcludeIsNotEmptyRootIsTrunkTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                IncludeMetaData = true,
                NoBranches      = true,
                NoTags          = true,
                RootIsTrunk     = true,
                Exclude         = new List <string>()
                {
                    "ex1",
                    "ex2"
                }
            };

            string ignorePathsRegEx  = @"^(?:)(?:ex1|ex2)";
            string expectedArguments = $"svn fetch --ignore-paths=\"{ignorePathsRegEx}\"";

            mock.Setup(
                f => f.Run("git", It.IsAny <string>())
                ).Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(
                f => f.Run("git", expectedArguments, It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan),
                Times.Once()
                );
        }
コード例 #2
0
        public void CloneWhenAuthorsIsNotEmptyTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                SubpathToTrunk  = "subpath",
                IncludeMetaData = true,
                NoBranches      = true,
                NoTags          = true,
                RootIsTrunk     = false,
                Authors         = "author1"
            };

            string expectedArguments = "config svn.authorsfile author1";

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(0);

            mock.Setup(f => f.Run("git", It.IsAny <string>())).Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object, "config");

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(f => f.Run("git", expectedArguments), Times.Once());
        }
コード例 #3
0
        public void CloneWhenRevisionIsNotEmptyTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                SubpathToTrunk  = "subpath",
                IncludeMetaData = true,
                NoBranches      = true,
                NoTags          = true,
                RootIsTrunk     = false,
                Revision        = "123:456"
            };

            string expectedArguments = "svn fetch -r 123:456";

            mock.Setup(
                f => f.Run("git", expectedArguments, It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                ).Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(
                f => f.Run("git", expectedArguments, It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan),
                Times.Once()
                );
        }
コード例 #4
0
        public void CloneUserNameAndPasswordAreEmptyStrings()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                UserName        = "******",
                UserNameMethod  = CredentialsMethod.empty_string,
                Password        = "******",
                PasswordMethod  = CredentialsMethod.empty_string,
                IncludeMetaData = false,
                NoMinimizeUrl   = true,
                RootIsTrunk     = true
            };

            string expectedArguments = $"svn init --prefix=svn/ --username=\"\" --no-metadata --no-minimize-url --trunk=\"/\" {_testSvnUrl}";

            mock.Setup(f => f.Run("git", It.IsAny <string>())).Returns(0);

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(f => f.RunGitSvnInteractiveCommand(expectedArguments, string.Empty), Times.Once());
        }
コード例 #5
0
        public void CloneWhenRootIsNotTrunkHasSubPathToTrunkGitCommandExecutionFailTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                SubpathToTrunk  = "subpath",
                IncludeMetaData = true,
                NoBranches      = true,
                NoTags          = true,
                RootIsTrunk     = false,
            };

            string expectedExceptionMessage = string.Format(ExceptionHelper.ExceptionMessage.FAIL_TO_EXECUTE_COMMAND, $"git svn init --prefix=svn/ --trunk=\"subpath\" {_testSvnUrl}");

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(-1);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            Exception ex = Record.Exception(() => grabber.Clone());

            // Assert
            Assert.IsType <MigrateException>(ex);
            Assert.Equal(expectedExceptionMessage, ex.Message);
        }
コード例 #6
0
        public void CloneWhenRootIsNotTrunkHasSubPathToTrunkAndBranchesTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                SubpathToTrunk  = "subpath",
                IncludeMetaData = true,
                NoBranches      = false,
                NoTags          = true,
                RootIsTrunk     = false,
                Branches        = new List <string>()
                {
                    "branch1",
                    "branch2"
                }
            };

            string expectedArguments = $"svn init --prefix=svn/ --trunk=\"subpath\" --branches=\"branch1\" --branches=\"branch2\" {_testSvnUrl}";

            mock.Setup(f => f.Run("git", It.IsAny <string>()))
            .Returns(0);

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(f => f.RunGitSvnInteractiveCommand(expectedArguments, options.Password), Times.Once());
        }
コード例 #7
0
        public void CloneWhenRootIsNotTrunkWithoutBranchesAndTagsTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                IncludeMetaData = true,
                NoBranches      = true,
                NoTags          = true,
                RootIsTrunk     = false
            };

            string expectedArguments = $"svn init --prefix=svn/ {_testSvnUrl}";

            mock.Setup(f => f.Run("git", It.IsAny <string>())).Returns(0);

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(f => f.RunGitSvnInteractiveCommand(expectedArguments, options.Password), Times.Once());
        }
コード例 #8
0
        public void CloneWhenRootIsTrunkHasMinimizeUrlTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options()
            {
                IncludeMetaData = true,
                NoMinimizeUrl   = true,
                RootIsTrunk     = true
            };

            string expectedArguments = $"svn init --prefix=svn/ --no-minimize-url --trunk=\"/\" {_testSvnUrl}";

            mock.Setup(f => f.Run("git", It.IsAny <string>())).Returns(0);

            mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(0);

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            mock.Verify(f => f.RunGitSvnInteractiveCommand(expectedArguments, options.Password), Times.Once());
        }
コード例 #9
0
        public void FetchWithLimitedRetriesButWithProgressMadeTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options
            {
                FetchAttempts = 1
            };

            List <string> responses = new List <string>
            {
                "r127 = somehash (ref/somewhere/svn)",
                "r128 = somehash (ref/somewhere/svn)",
                "r129 = somehash (ref/somewhere/svn)"
            };

            int timesCalled = 0;

            mock.Setup(
                f => f.Run("git", It.IsAny <string>(), It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                )
            .Returns(
                delegate(string cmd, string arguments, Action <string> onStdout, Action <string> onStdErr, string workDir, TimeSpan timeout)
            {
                onStdout(responses[timesCalled]);

                ++timesCalled;

                if (timesCalled >= responses.Count)
                {
                    return(0);
                }
                else
                {
                    return(128);
                }
            }
                );

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert

            // Times it will be called:
            // 1 - to go from rev -1 to rev 127 (we treat this as making progress)
            // 2 - to go from rev 127 to rev 128 (Made Progress)
            // 3 - to go from rev 128 to rev 129 (Made Progress, returns 0, breaks out)
            Assert.Equal(responses.Count, timesCalled);
        }
コード例 #10
0
        public void FetchWithLimitedRetriesButWithNoProgressMadeAfter1FailureTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options
            {
                FetchAttempts = 1
            };

            List <Tuple <string, int> > responses = new List <Tuple <string, int> >
            {
                new Tuple <string, int>("r126 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r127 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r128 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r128 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r128 = somehash (ref/somewhere/svn)", 128)
            };

            int timesCalled = 0;

            mock.Setup(
                f => f.Run("git", It.IsAny <string>(), It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                )
            .Returns(
                delegate(string cmd, string arguments, Action <string> onStdout, Action <string> onStdErr, string workDir, TimeSpan timeout)
            {
                onStdout(responses[timesCalled].Item1);

                return(responses[timesCalled++].Item2);
            }
                );

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            Exception ex = Record.Exception(() => grabber.Clone());

            // Assert
            Assert.IsType <MigrateException>(ex);

            // Times it will be called:
            // 1 - to go from rev -1 to rev 126 (we treat this as making progress)
            // 2 - to go from rev 126 to rev 127 (Made progress even though returned non-zero)
            // 3 - to go from rev 127 to rev 128 (Made progress even though returned non-zero)
            // 4 - to go from rev 128 to rev 128 (First Failure, did not make progress, increment attempt.)
            // 5 - to go from rev 128 to rev 128 (Attempt 1, no success.  Should throw exception).
            Assert.Equal(responses.Count, timesCalled);
        }
コード例 #11
0
        public void FetchWithLimitedRetriesAnMultipleFailures()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options
            {
                FetchAttempts = 1
            };

            List <Tuple <string, int> > responses = new List <Tuple <string, int> >
            {
                new Tuple <string, int>("r126 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r126 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r128 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r128 = somehash (ref/somewhere/svn)", 128),
                new Tuple <string, int>("r129 = somehash (ref/somewhere/svn)", 0)
            };

            int timesCalled = 0;

            mock.Setup(
                f => f.Run("git", It.IsAny <string>(), It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                )
            .Returns(
                delegate(string cmd, string arguments, Action <string> onStdout, Action <string> onStdErr, string workDir, TimeSpan timeout)
            {
                onStdout(responses[timesCalled].Item1);

                return(responses[timesCalled++].Item2);
            }
                );

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert

            // Times it will be called:
            // 1 - to go from rev -1 to rev 126 (we treat this as making progress)
            // 2 - to go from rev 126 to rev 126 (First Failure)
            // 3 - to go from rev 126 to rev 128 (Made progress even though returned non-zero.  Attempts reset)
            // 4 - to go from rev 128 to rev 128 (First Failure for this rev, did not make progress, increment attempt.)
            // 5 - to go from rev 128 to rev 129 (Returns 0, breaks out)
            Assert.Equal(responses.Count, timesCalled);
        }
コード例 #12
0
        public void CloneWhenRootIsTrunkWithAllAndUserNamePasswordAreEnvParametersTest()
        {
            const string envVarName = "svn2gitnetxtest_user";
            const string userName   = "******";

            const string envVarPassword = "******";
            const string password       = "******";

            try
            {
                Environment.SetEnvironmentVariable(envVarName, userName);
                Environment.SetEnvironmentVariable(envVarPassword, password);

                // Prepare
                var     mock    = new Mock <ICommandRunner>();
                Options options = new Options()
                {
                    UserName        = envVarName,
                    UserNameMethod  = CredentialsMethod.env_var,
                    Password        = envVarPassword,
                    PasswordMethod  = CredentialsMethod.env_var,
                    IncludeMetaData = false,
                    NoMinimizeUrl   = true,
                    RootIsTrunk     = true
                };

                string expectedArguments = $"svn init --prefix=svn/ --username=\"{userName}\" --no-metadata --no-minimize-url --trunk=\"/\" {_testSvnUrl}";

                mock.Setup(f => f.Run("git", It.IsAny <string>())).Returns(0);

                mock.Setup(f => f.RunGitSvnInteractiveCommand(It.IsAny <string>(), It.IsAny <string>()))
                .Returns(0);

                IGrabber grabber = CreateGrabber(options, mock.Object);

                // Act
                grabber.Clone();

                // Assert
                mock.Verify(f => f.RunGitSvnInteractiveCommand(expectedArguments, password), Times.Once());
            }
            finally
            {
                Environment.SetEnvironmentVariable(envVarName, string.Empty);
                Environment.SetEnvironmentVariable(envVarPassword, string.Empty);
            }
        }
コード例 #13
0
        public void FetchWithUnlimitedRetriesTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options
            {
                // -1 means unlimited attempts.
                FetchAttempts = -1
            };

            int       timesCalled = 0;
            const int maxAttempts = 3;

            mock.Setup(
                f => f.Run("git", It.IsAny <string>(), It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                )
            .Returns(
                delegate(string cmd, string arguments, Action <string> onStdout, Action <string> onStdErr, string workDir, TimeSpan timeout)
            {
                // Only make progress on 1 revision.
                onStdout("r1 = somehash (ref/somewhere/svn)");

                // After 3 attempts, return 0
                // to emulate the fetch finally working.
                ++timesCalled;
                if (timesCalled <= maxAttempts)
                {
                    return(128);
                }
                else
                {
                    return(0);
                }
            }
                );

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            grabber.Clone();

            // Assert
            Assert.Equal(maxAttempts + 1, timesCalled);
        }
コード例 #14
0
        public void FetchWithLimitedRetriesTest()
        {
            // Prepare
            var     mock    = new Mock <ICommandRunner>();
            Options options = new Options
            {
                FetchAttempts = 3
            };

            int timesCalled = 0;

            mock.Setup(
                f => f.Run("git", It.IsAny <string>(), It.IsAny <Action <string> >(), null, null, Timeout.InfiniteTimeSpan)
                )
            .Returns(
                delegate(string cmd, string arguments, Action <string> onStdout, Action <string> onStdErr, string workDir, TimeSpan timeout)
            {
                // Only make progress on 1 revision.
                // This will increment our attempts since no progress has been made.
                onStdout("r127 = somehash (ref/somewhere/svn)");

                ++timesCalled;
                return(128);
            }
                );

            IGrabber grabber = CreateGrabber(options, mock.Object);

            // Act
            Exception ex = Record.Exception(() => grabber.Clone());

            // Assert
            Assert.IsType <MigrateException>(ex);

            // Times it will be called:
            // 1 - to go from rev -1 to rev 127 (we treat this as making progress)
            // 2 - to go from rev 127 to rev 127 (First Failure)
            // 3 - to go from rev 127 to rev 127 (Attempt 1)
            // 4 - to go from rev 127 to rev 127 (Attempt 2)
            // 5 - to go from rev 127 to rev 127 (Attempt 3, break out)
            Assert.Equal(options.FetchAttempts + 2, timesCalled);
        }
コード例 #15
0
        public void Run(IGrabber grabber, IFixer fixer)
        {
            if (grabber == null)
            {
                throw new ArgumentNullException("grabber");
            }

            if (fixer == null)
            {
                throw new ArgumentNullException("fixer");
            }

            try
            {
                PreRunPrepare();

                if (Options.Rebase)
                {
                    grabber.FetchBranches();
                }
                else if (!string.IsNullOrWhiteSpace(Options.RebaseBranch))
                {
                    grabber.FetchRebaseBraches();
                }
                else
                {
                    grabber.Clone();
                }

                fixer.FixBranches();
                fixer.FixTags();
                fixer.FixTrunk();
                fixer.OptimizeRepos();
            }
            finally
            {
                PostRunCleanup();
            }
        }
コード例 #16
0
        public void Run(
            IGrabber grabber,
            IFixer fixer,
            IStaleSvnBranchDeleter svnBranchDeleter,
            IGitPusher gitPusher,
            ILockBreaker lockBreaker
            )
        {
            if (grabber == null)
            {
                throw new ArgumentNullException(nameof(grabber));
            }

            if (fixer == null)
            {
                throw new ArgumentNullException(nameof(fixer));
            }

            if (svnBranchDeleter == null)
            {
                throw new ArgumentNullException(nameof(svnBranchDeleter));
            }

            if (lockBreaker == null)
            {
                throw new ArgumentNullException(nameof(lockBreaker));
            }

            try
            {
                PreRunPrepare();

                lockBreaker.BreakLocksIfEnabled();

                if (Options.Rebase)
                {
                    grabber.FetchBranches();
                }
                else if (!string.IsNullOrWhiteSpace(Options.RebaseBranch))
                {
                    grabber.FetchRebaseBraches();
                }
                else
                {
                    grabber.Clone();
                }

                fixer.FixBranches();
                fixer.FixTags();
                fixer.FixTrunk();
                fixer.OptimizeRepos();

                if (Options.StaleSvnBranchPurgeOption != StaleSvnBranchPurgeOptions.nothing)
                {
                    IEnumerable <string> svnBranches = svnBranchDeleter.QueryHeadSvnBranches();

                    IEnumerable <string> gitBranchesToPurge = svnBranchDeleter.GetGitBranchesToPurge(svnBranches);
                    svnBranches = null;

                    svnBranchDeleter.PurgeGitBranches(gitBranchesToPurge);
                    gitBranchesToPurge = null;

                    if (Options.StaleSvnBranchPurgeOption == StaleSvnBranchPurgeOptions.delete_local_and_remote)
                    {
                        gitPusher.PushPrune();
                    }
                }

                if (Options.PushWhenDone)
                {
                    gitPusher.PushAll();
                }
            }
            finally
            {
                PostRunCleanup();
            }
        }