示例#1
0
        public void CheckoutNthCommitOnBranch()
        {
            var          output               = Helpers.LoadResource(Paths.GitSingleCommitOutput, typeof(GitCommandsTest).Assembly);
            var          executorMock         = new Mock <ICommandLineExecutor>();
            const string repoPath             = "dir";
            const string commitCountCommand   = "rev-list --count HEAD";
            const int    nthCommit            = 5;
            const int    allCommits           = 123;
            var          nthCommitHashCommand = $"log -1 HEAD~{allCommits - nthCommit + 1}";

            executorMock
            .Setup(mock => mock.Execute("git", It.Is <string>(command => command == commitCountCommand), repoPath))
            .Returns(new[] { allCommits.ToString() });
            executorMock
            .Setup(mock => mock.Execute("git", It.Is <string>(command => command == nthCommitHashCommand), repoPath))
            .Returns(output.Split('\n'));
            var gitCommands = new GitCommands(executorMock.Object);

            gitCommands.CheckoutNthCommitOnBranch(repoPath, nthCommit);

            executorMock.Verify(mock => mock.Execute("git", commitCountCommand, repoPath), Times.Exactly(2));
            executorMock.Verify(mock => mock.Execute("git", nthCommitHashCommand, repoPath), Times.Once);
            executorMock.Verify(mock => mock.Execute("git",
                                                     "checkout abb3cc3d7e405c39eae91b22c41d1281b9075cd4",
                                                     repoPath),
                                Times.Once);
        }
示例#2
0
        public void CheckoutNthCommitOnBranchThrowsExceptionWhenCommitNumberIsGreaterThanAllCommitNumber()
        {
            var          executorMock       = new Mock <ICommandLineExecutor>();
            const string repoPath           = "dir";
            const string commitCountCommand = "rev-list --count HEAD";
            const int    nthCommit          = 124;
            const int    allCommits         = 123;

            executorMock
            .Setup(mock => mock.Execute("git", It.Is <string>(command => command == commitCountCommand), repoPath))
            .Returns(new[] { allCommits.ToString() });
            var gitCommands = new GitCommands(executorMock.Object);

            Action act = () => gitCommands.CheckoutNthCommitOnBranch(repoPath, nthCommit);

            act.Should()
            .Throw <ArgumentOutOfRangeException>()
            .WithMessage("*this branch only has*");
        }