<commit_before>﻿using System;
using System.Collections.Generic;
using GitTfs.Commands;
using GitTfs.Core;
using GitTfs.Core.TfsInterop;
using GitTfs.VsFake;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace GitTfs.Test.Commands
{
    public class InitBranchTest : BaseTest
    {
        #region Test Init
        private readonly MoqAutoMocker<InitBranch> mocks;

        public InitBranchTest()
        {
            mocks = new MoqAutoMocker<InitBranch>();
            var globals = mocks.Get<Globals>();
            var globalsMock = Mock.Get(globals).SetupAllProperties();
            globals.Repository = mocks.Get<IGitRepository>();
        }

        private void InitMocks4Tests(string gitBranchToInit, out Mock<IGitRepository> gitRepositoryMock,
            out Mock<IGitTfsRemote> trunkGitTfsRemoteMock, out Mock<IGitTfsRemote> newBranchRemoteMock, out Mock<ITfsHelper> tfsHelperMock)
        {
            var tfsHelper = mocks.Get<ITfsHelper>();
            tfsHelperMock = Mock.Get(tfsHelper);

            gitRepositoryMock = Mock.Get(mocks.Get<IGitRepository>()).SetupAllProperties();
            gitRepositoryMock.Name = nameof(gitRepositoryMock);
            gitRepositoryMock.SetupGet(r => r.GitDir).Returns(".");
            gitRepositoryMock.Setup(r => r.HasRemote(It.IsAny<string>())).Returns(true);

            var globals = mocks.Get<Globals>();
            globals.Repository = gitRepositoryMock.Object;
            globals.GitDir = ".git";

            trunkGitTfsRemoteMock = new Mock<IGitTfsRemote>().SetupAllProperties();
            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            var trunkGitTfsRemote = trunkGitTfsRemoteMock.Object;
            trunkGitTfsRemote.TfsUsername = "user";
            trunkGitTfsRemote.TfsPassword = "<PASSWORD>";
            trunkGitTfsRemote.TfsRepositoryPath = "$/MyProject/Trunk";
            trunkGitTfsRemote.TfsUrl = "http://myTfsServer:8080/tfs";
            trunkGitTfsRemote.Tfs = tfsHelper;
            //trunkGitTfsRemote.Tfs = new TfsHelper(mocks.Container, null);

            newBranchRemoteMock = Mock.Get(mocks.Get<IGitTfsRemote>()).SetupAllProperties();
            newBranchRemoteMock.Name = nameof(newBranchRemoteMock);
            newBranchRemoteMock.Object.Id = gitBranchToInit;
        }
        #endregion

        #region Init a Branch
        [Fact]
        public void ShouldInitBranchWhenNoBranchGitNameProposed()
        {
            ShouldInitBranch(null);
        }

        [Fact]
        public void ShouldInitBranchWhenBranchGitNameProposed()
        {
            ShouldInitBranch("MyBranch");
        }

        private void ShouldInitBranch(string expectedGitBranchName)
        {
            const string GIT_BRANCH_TO_INIT = "MyBranch";

            InitMocks4Tests(GIT_BRANCH_TO_INIT, out var gitRepositoryMock, out var trunkGitTfsRemoteMock, out var newBranchRemoteMock, out var tfsHelperMock);

            tfsHelperMock.Setup(t => t.GetRootChangesetForBranch("$/MyProject/MyBranch", -1, null)).Returns(new List<RootBranch>() { new RootBranch(2010, "$/MyProject/MyBranch") });

            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            trunkGitTfsRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>())).Returns(new GitTfsRemote.FetchResult() { IsSuccess = true }).Verifiable();
            trunkGitTfsRemoteMock.Setup(t => t.InitBranch(It.IsAny<RemoteOptions>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<IRenameResult>())).Returns(newBranchRemoteMock.Object).Verifiable();
            trunkGitTfsRemoteMock.Object.Tfs = tfsHelperMock.Object;

            gitRepositoryMock.Name = nameof(gitRepositoryMock);
            gitRepositoryMock.Setup(x => x.ReadTfsRemote("default")).Returns(trunkGitTfsRemoteMock.Object).Verifiable();
            gitRepositoryMock.Setup(x => x.ReadAllTfsRemotes()).Returns(new List<IGitTfsRemote> {trunkGitTfsRemoteMock.Object})
                .Verifiable();

            //newBranchRemoteMock.SetupGet(r => r.RemoteRef).Returns("refs/remote/tfs/" + GIT_BRANCH_TO_INIT).Verifiable();
            newBranchRemoteMock.Name = nameof(newBranchRemoteMock);
            newBranchRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>()))
                .Returns(new GitTfsRemote.FetchResult() { IsSuccess = true }).Verifiable();
            newBranchRemoteMock.Object.MaxCommitHash = "sha1AfterFetch";

            Assert.Equal(GitTfsExitCodes.OK, mocks.ClassUnderTest.Run("$/MyProject/MyBranch", expectedGitBranchName));

            gitRepositoryMock.Verify();
            //trunkGitTfsRemoteMock.Verify();
            newBranchRemoteMock.Verify();
        }

        [Fact]
        public void ShouldDoNothingBecauseRemoteAlreadyExisting()
        {
            const string GIT_BRANCH_TO_INIT = "myBranch";

            InitMocks4Tests(GIT_BRANCH_TO_INIT, out var gitRepository, out var trunkGitTfsRemoteMock, out var newBranchRemoteMock, out var tfsHelperMock);

            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            trunkGitTfsRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>())).Returns(new GitTfsRemote.FetchResult() { IsSuccess = true });
            trunkGitTfsRemoteMock.Setup(t => t.InitBranch(It.IsAny<RemoteOptions>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<IRenameResult>())).Returns(newBranchRemoteMock.Object);

            tfsHelperMock.Setup(t => t.GetRootChangesetForBranch("$/MyProject/MyBranch", -1, null)).Returns(new List<RootBranch>() { new RootBranch(2010, "$/MyProject/MyBranch") });

            IGitTfsRemote existingBranchRemote = new Mock<IGitTfsRemote>().SetupAllProperties().Object;
            existingBranchRemote.TfsUsername = "user";
            existingBranchRemote.TfsPassword = "<PASSWORD>";
            existingBranchRemote.TfsRepositoryPath = "$/MyProject/MyBranch";
            existingBranchRemote.TfsUrl = "http://myTfsServer:8080/tfs";
            existingBranchRemote.Tfs = new TfsHelper(mocks.Container, null);

            gitRepository.Name = nameof(gitRepository);
            gitRepository.Setup(x => x.ReadTfsRemote("default")).Returns(trunkGitTfsRemoteMock.Object).Verifiable();
            gitRepository.Setup(x => x.ReadAllTfsRemotes()).Returns(new List<IGitTfsRemote> { trunkGitTfsRemoteMock.Object, existingBranchRemote }).Verifiable();

            newBranchRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>()))<commit_msg>Remove unused Verifiable mock setup call<commit_after>﻿using System;
using System.Collections.Generic;
using GitTfs.Commands;
using GitTfs.Core;
using GitTfs.Core.TfsInterop;
using GitTfs.VsFake;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace GitTfs.Test.Commands
{
    public class InitBranchTest : BaseTest
    {
        #region Test Init
        private readonly MoqAutoMocker<InitBranch> mocks;

        public InitBranchTest()
        {
            mocks = new MoqAutoMocker<InitBranch>();
            var globals = mocks.Get<Globals>();
            var globalsMock = Mock.Get(globals).SetupAllProperties();
            globals.Repository = mocks.Get<IGitRepository>();
        }

        private void InitMocks4Tests(string gitBranchToInit, out Mock<IGitRepository> gitRepositoryMock,
            out Mock<IGitTfsRemote> trunkGitTfsRemoteMock, out Mock<IGitTfsRemote> newBranchRemoteMock, out Mock<ITfsHelper> tfsHelperMock)
        {
            var tfsHelper = mocks.Get<ITfsHelper>();
            tfsHelperMock = Mock.Get(tfsHelper);

            gitRepositoryMock = Mock.Get(mocks.Get<IGitRepository>()).SetupAllProperties();
            gitRepositoryMock.Name = nameof(gitRepositoryMock);
            gitRepositoryMock.SetupGet(r => r.GitDir).Returns(".");
            gitRepositoryMock.Setup(r => r.HasRemote(It.IsAny<string>())).Returns(true);

            var globals = mocks.Get<Globals>();
            globals.Repository = gitRepositoryMock.Object;
            globals.GitDir = ".git";

            trunkGitTfsRemoteMock = new Mock<IGitTfsRemote>().SetupAllProperties();
            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            var trunkGitTfsRemote = trunkGitTfsRemoteMock.Object;
            trunkGitTfsRemote.TfsUsername = "user";
            trunkGitTfsRemote.TfsPassword = "<PASSWORD>";
            trunkGitTfsRemote.TfsRepositoryPath = "$/MyProject/Trunk";
            trunkGitTfsRemote.TfsUrl = "http://myTfsServer:8080/tfs";
            trunkGitTfsRemote.Tfs = tfsHelper;
            //trunkGitTfsRemote.Tfs = new TfsHelper(mocks.Container, null);

            newBranchRemoteMock = Mock.Get(mocks.Get<IGitTfsRemote>()).SetupAllProperties();
            newBranchRemoteMock.Name = nameof(newBranchRemoteMock);
            newBranchRemoteMock.Object.Id = gitBranchToInit;
        }
        #endregion

        #region Init a Branch
        [Fact]
        public void ShouldInitBranchWhenNoBranchGitNameProposed()
        {
            ShouldInitBranch(null);
        }

        [Fact]
        public void ShouldInitBranchWhenBranchGitNameProposed()
        {
            ShouldInitBranch("MyBranch");
        }

        private void ShouldInitBranch(string expectedGitBranchName)
        {
            const string GIT_BRANCH_TO_INIT = "MyBranch";

            InitMocks4Tests(GIT_BRANCH_TO_INIT, out var gitRepositoryMock, out var trunkGitTfsRemoteMock, out var newBranchRemoteMock, out var tfsHelperMock);

            tfsHelperMock.Setup(t => t.GetRootChangesetForBranch("$/MyProject/MyBranch", -1, null)).Returns(new List<RootBranch>() { new RootBranch(2010, "$/MyProject/MyBranch") });

            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            trunkGitTfsRemoteMock.Setup(t => t.InitBranch(It.IsAny<RemoteOptions>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<IRenameResult>())).Returns(newBranchRemoteMock.Object).Verifiable();
            trunkGitTfsRemoteMock.Object.Tfs = tfsHelperMock.Object;

            gitRepositoryMock.Name = nameof(gitRepositoryMock);
            gitRepositoryMock.Setup(x => x.ReadTfsRemote("default")).Returns(trunkGitTfsRemoteMock.Object).Verifiable();
            gitRepositoryMock.Setup(x => x.ReadAllTfsRemotes()).Returns(new List<IGitTfsRemote> {trunkGitTfsRemoteMock.Object})
                .Verifiable();

            //newBranchRemoteMock.SetupGet(r => r.RemoteRef).Returns("refs/remote/tfs/" + GIT_BRANCH_TO_INIT).Verifiable();
            newBranchRemoteMock.Name = nameof(newBranchRemoteMock);
            newBranchRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>()))
                .Returns(new GitTfsRemote.FetchResult() { IsSuccess = true }).Verifiable();
            newBranchRemoteMock.Object.MaxCommitHash = "sha1AfterFetch";

            Assert.Equal(GitTfsExitCodes.OK, mocks.ClassUnderTest.Run("$/MyProject/MyBranch", expectedGitBranchName));

            gitRepositoryMock.Verify();
            trunkGitTfsRemoteMock.Verify();
            newBranchRemoteMock.Verify();
        }

        [Fact]
        public void ShouldDoNothingBecauseRemoteAlreadyExisting()
        {
            const string GIT_BRANCH_TO_INIT = "myBranch";

            InitMocks4Tests(GIT_BRANCH_TO_INIT, out var gitRepository, out var trunkGitTfsRemoteMock, out var newBranchRemoteMock, out var tfsHelperMock);

            trunkGitTfsRemoteMock.Name = nameof(trunkGitTfsRemoteMock);
            trunkGitTfsRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>())).Returns(new GitTfsRemote.FetchResult() { IsSuccess = true });
            trunkGitTfsRemoteMock.Setup(t => t.InitBranch(It.IsAny<RemoteOptions>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<IRenameResult>())).Returns(newBranchRemoteMock.Object);

            tfsHelperMock.Setup(t => t.GetRootChangesetForBranch("$/MyProject/MyBranch", -1, null)).Returns(new List<RootBranch>() { new RootBranch(2010, "$/MyProject/MyBranch") });

            IGitTfsRemote existingBranchRemote = new Mock<IGitTfsRemote>().SetupAllProperties().Object;
            existingBranchRemote.TfsUsername = "user";
            existingBranchRemote.TfsPassword = "<PASSWORD>";
            existingBranchRemote.TfsRepositoryPath = "$/MyProject/MyBranch";
            existingBranchRemote.TfsUrl = "http://myTfsServer:8080/tfs";
            existingBranchRemote.Tfs = new TfsHelper(mocks.Container, null);

            gitRepository.Name = nameof(gitRepository);
            gitRepository.Setup(x => x.ReadTfsRemote("default")).Returns(trunkGitTfsRemoteMock.Object).Verifiable();
            gitRepository.Setup(x => x.ReadAllTfsRemotes()).Returns(new List<IGitTfsRemote> { trunkGitTfsRemoteMock.Object, existingBranchRemote }).Verifiable();

            newBranchRemoteMock.Setup(r => r.Fetch(It.IsAny<bool>(), It.IsAny<int>(), It.IsAny<IRenameResult>()))
{'fraction_non_alphanumeric': 0.1194939795762841, 'fraction_numerical': 0.003353147386069197, 'mean_word_length': 3.295581014729951, 'pattern_counts': {'":': 0, '<': 90, '<?xml version=': 0, '>': 119, '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': '7046980', 'n_tokens_mistral': 3990, 'n_tokens_neox': 3975, 'n_words': 550}