Example #1
0
        public void HashForNthCommit()
        {
            var          output               = Helpers.LoadResource(Paths.GitSingleCommitOutput, typeof(GitCommandsTest).Assembly);
            var          executorMock         = new Mock <ICommandLineExecutor>();
            const string repoPath             = "dir";
            const int    nthCommit            = 5;
            const int    allCommits           = 123;
            const string commitCountCommand   = "rev-list --count HEAD";
            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);

            var result = gitCommands.GetHashForNthCommit(repoPath, nthCommit);

            executorMock.Verify(mock => mock.Execute("git", commitCountCommand, repoPath), Times.Once);
            executorMock.Verify(mock => mock.Execute("git", nthCommitHashCommand, repoPath), Times.Once);
            result.Should()
            .Be("abb3cc3d7e405c39eae91b22c41d1281b9075cd4");
        }