public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true, string targetBranch = null)
        {
            if (configuration == null)
            {
                configuration = new Config();
                configuration.Reset();
            }

            var log = new NullLog();
            var metaDataCalculator        = new MetaDataCalculator();
            var baseVersionCalculator     = new TestBaseVersionStrategiesCalculator(log);
            var mainlineVersionCalculator = new MainlineVersionCalculator(log, metaDataCalculator);
            var nextVersionCalculator     = new NextVersionCalculator(log, metaDataCalculator, baseVersionCalculator, mainlineVersionCalculator);
            var variableProvider          = new VariableProvider(nextVersionCalculator, new TestEnvironment());
            var gitVersionContext         = new GitVersionContext(repository ?? fixture.Repository, log, targetBranch, configuration, isForTrackedBranchOnly, commitId);
            var executeGitVersion         = ExecuteGitVersion(gitVersionContext);
            var variables = variableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);

            try
            {
                return(variables);
            }
            catch (Exception)
            {
                Console.WriteLine("Test failing, dumping repository graph");
                gitVersionContext.Repository.DumpGraph();
                throw;
            }
        }
Ejemplo n.º 2
0
        public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
        {
            if (configuration == null)
            {
                configuration = new Config();
                configuration.Reset();
            }

            var log = sp.GetService <ILog>();
            var variableProvider = sp.GetService <IVariableProvider>();
            var versionFinder    = sp.GetService <IGitVersionFinder>();

            var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, log, targetBranch, configuration, onlyTrackedBranches, commitId);
            var executeGitVersion = versionFinder.FindVersion(gitVersionContext);
            var variables         = variableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);

            try
            {
                return(variables);
            }
            catch (Exception)
            {
                Console.WriteLine("Test failing, dumping repository graph");
                gitVersionContext.Repository.DumpGraph();
                throw;
            }
        }
Ejemplo n.º 3
0
        public static void WriteVersionVariables(this RepositoryFixtureBase fixture, string versionFile)
        {
            var versionInfo = fixture.GetVersion();

            using var stream = File.Open(versionFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            using var writer = new StreamWriter(stream);
            writer.Write(versionInfo.ToString());
        }
Ejemplo n.º 4
0
        public MsBuildExeFixture(RepositoryFixtureBase fixture, string workingDirectory = "")
        {
            this.fixture = fixture;
            ProjectPath  = Path.Combine(workingDirectory, "app.csproj");

            var versionFile = Path.Combine(workingDirectory, "gitversion.json");

            fixture.WriteVersionVariables(versionFile);
        }
    public static void MakeACommit(this RepositoryFixtureBase fixture, string commitMsg)
    {
        fixture.Repository.MakeACommit(commitMsg);
        var diagramBuilder = (StringBuilder)typeof(SequenceDiagram)
                             .GetField("_diagramBuilder", BindingFlags.Instance | BindingFlags.NonPublic)
                             .GetValue(fixture.SequenceDiagram);
        Func <string, string> getParticipant = participant => (string)typeof(SequenceDiagram)
                                               .GetMethod("GetParticipant", BindingFlags.Instance | BindingFlags.NonPublic)
                                               .Invoke(fixture.SequenceDiagram, new object[]
        {
            participant
        });

        diagramBuilder.AppendLineFormat("{0} -> {0}: Commit '{1}'", getParticipant(fixture.Repository.Head.FriendlyName),
                                        commitMsg);
    }
Ejemplo n.º 6
0
        public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string branch = null)
        {
            if (configuration == null)
            {
                configuration = new Config();
                configuration.Reset();
            }

            repository ??= fixture.Repository;

            var options = Options.Create(new GitVersionOptions
            {
                WorkingDirectory = repository.Info.WorkingDirectory,
                ConfigInfo       = { OverrideConfig = configuration },
                RepositoryInfo   =
                {
                    TargetBranch = branch,
                    CommitId     = commitId,
                },
                Settings = { OnlyTrackedBranches = onlyTrackedBranches }
            });

            var sp = ConfigureServices(services =>
            {
                services.AddSingleton(options);
            });

            var variableProvider      = sp.GetService <IVariableProvider>();
            var nextVersionCalculator = sp.GetService <INextVersionCalculator>();
            var contextOptions        = sp.GetService <Lazy <GitVersionContext> >();

            var context = contextOptions.Value;

            try
            {
                var executeGitVersion = nextVersionCalculator.FindVersion();
                var variables         = variableProvider.GetVariablesFor(executeGitVersion, context.Configuration, context.IsCurrentCommitTagged);

                return(variables);
            }
            catch (Exception)
            {
                Console.WriteLine("Test failing, dumping repository graph");
                repository.DumpGraph();
                throw;
            }
        }
Ejemplo n.º 7
0
        public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
        {
            ConfigurationProvider.ApplyDefaultsTo(configuration);
            var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, configuration, isForTrackedBranchOnly, commitId);
            var executeGitVersion = ExecuteGitVersion(gitVersionContext);
            var variables         = VariableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);

            try
            {
                return(variables);
            }
            catch (Exception)
            {
                Console.WriteLine("Test failing, dumping repository graph");
                gitVersionContext.Repository.DumpGraph();
                throw;
            }
        }
        public static void AssertFullSemver(this RepositoryFixtureBase fixture, Config configuration, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
        {
            ConfigurationProvider.ApplyDefaultsTo(configuration);
            Console.WriteLine("---------");

            try
            {
                var variables = fixture.GetVersion(configuration, repository, commitId, isForTrackedBranchOnly);
                variables.FullSemVer.ShouldBe(fullSemver);
            }
            catch (Exception)
            {
                (repository ?? fixture.Repository).DumpGraph();
                throw;
            }
            if (commitId == null)
            {
                fixture.SequenceDiagram.NoteOver(fullSemver, fixture.Repository.Head.FriendlyName, color: "#D3D3D3");
            }
        }
Ejemplo n.º 9
0
        public static void AssertFullSemver(this RepositoryFixtureBase fixture, Config configuration, string fullSemver, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
        {
            configuration.Reset();
            Console.WriteLine("---------");

            try
            {
                var variables = fixture.GetVersion(configuration, repository, commitId, onlyTrackedBranches, targetBranch);
                variables.FullSemVer.ShouldBe(fullSemver);
            }
            catch (Exception)
            {
                (repository ?? fixture.Repository).DumpGraph();
                throw;
            }
            if (commitId == null)
            {
                fixture.SequenceDiagram.NoteOver(fullSemver, fixture.Repository.Head.FriendlyName, color: "#D3D3D3");
            }
        }
    private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pullRequestRef, Dictionary <string, string> env)
    {
        using var fixture = new EmptyRepositoryFixture();
        var remoteRepositoryPath = ExecutableHelper.GetTempPath();

        RepositoryFixtureBase.Init(remoteRepositoryPath);
        using (var remoteRepository = new Repository(remoteRepositoryPath))
        {
            remoteRepository.Config.Set("user.name", "Test");
            remoteRepository.Config.Set("user.email", "*****@*****.**");
            fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
            Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
            remoteRepository.MakeATaggedCommit("1.0.3");

            var branch = remoteRepository.CreateBranch("FeatureBranch");
            Commands.Checkout(remoteRepository, branch);
            remoteRepository.MakeCommits(2);
            Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha);
            //Emulate merge commit
            var mergeCommitSha = remoteRepository.MakeACommit().Sha;
            Commands.Checkout(remoteRepository, TestBase.MainBranch); // HEAD cannot be pointing at the merge commit
            remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));

            // Checkout PR commit
            Commands.Fetch((Repository)fixture.Repository, "origin", Array.Empty <string>(), new FetchOptions(), null);
            Commands.Checkout(fixture.Repository, mergeCommitSha);
        }

        var programFixture = new ProgramFixture(fixture.RepositoryPath);

        programFixture.WithEnv(env.ToArray());

        var result = await programFixture.Run();

        result.ExitCode.ShouldBe(0);
        result.OutputVariables.ShouldNotBeNull();
        result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3");

        // Cleanup repository files
        DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
    }
    public static void MakeACommit(this RepositoryFixtureBase fixture, string commitMsg)
    {
        fixture.Repository.MakeACommit(commitMsg);
        var diagramBuilder = (StringBuilder?)typeof(SequenceDiagram)
                             .GetField("diagramBuilder", BindingFlags.Instance | BindingFlags.NonPublic)
                             ?.GetValue(fixture.SequenceDiagram);

        string?GetParticipant(string participant) =>
        (string?)typeof(SequenceDiagram).GetMethod("GetParticipant", BindingFlags.Instance | BindingFlags.NonPublic)
        ?.Invoke(fixture.SequenceDiagram,
                 new object[]
        {
            participant
        });

        var participant = GetParticipant(fixture.Repository.Head.FriendlyName);

        if (participant != null)
        {
            diagramBuilder?.AppendLineFormat("{0} -> {0}: Commit '{1}'", participant, commitMsg);
        }
    }
    private static async Task VerifyTagCheckoutVersionIsCalculatedProperly(Dictionary <string, string> env)
    {
        using var fixture = new EmptyRepositoryFixture();
        var remoteRepositoryPath = ExecutableHelper.GetTempPath();

        RepositoryFixtureBase.Init(remoteRepositoryPath);
        using (var remoteRepository = new Repository(remoteRepositoryPath))
        {
            remoteRepository.Config.Set("user.name", "Test");
            remoteRepository.Config.Set("user.email", "*****@*****.**");
            fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
            Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
            remoteRepository.MakeATaggedCommit("0.1.0");
            Commands.Checkout(remoteRepository, remoteRepository.CreateBranch("develop"));
            remoteRepository.MakeACommit();
            Commands.Checkout(remoteRepository, remoteRepository.CreateBranch("release/0.2.0"));
            remoteRepository.MakeACommit();
            Commands.Checkout(remoteRepository, TestBase.MainBranch);
            remoteRepository.MergeNoFF("release/0.2.0", Generate.SignatureNow());
            remoteRepository.MakeATaggedCommit("0.2.0");

            Commands.Fetch((Repository)fixture.Repository, "origin", Array.Empty <string>(), new FetchOptions(), null);
            Commands.Checkout(fixture.Repository, "0.2.0");
        }

        var programFixture = new ProgramFixture(fixture.RepositoryPath);

        programFixture.WithEnv(env.ToArray());

        var result = await programFixture.Run();

        result.ExitCode.ShouldBe(0);
        result.OutputVariables.ShouldNotBeNull();
        result.OutputVariables.FullSemVer.ShouldBe("0.2.0");

        // Cleanup repository files
        DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
    }
Ejemplo n.º 13
0
 public MsBuildExeFixtureResult(RepositoryFixtureBase fixture) => this.fixture = fixture;
Ejemplo n.º 14
0
 public MsBuildExeFixture(RepositoryFixtureBase fixture, string workingDirectory = "")
 {
     this.fixture = fixture;
     ProjectPath  = Path.Combine(workingDirectory, "app.csproj");
 }
 public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool isForTrackedBranchOnly = true)
 {
     fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, isForTrackedBranchOnly);
 }
Ejemplo n.º 16
0
 public MsBuildTaskFixtureResult(RepositoryFixtureBase fixture)
 {
     this.fixture = fixture;
 }
Ejemplo n.º 17
0
        private static INextVersionCalculator GetNextVersionCalculator(RepositoryFixtureBase fixture)
        {
            var sp = BuildServiceProvider(fixture.RepositoryPath, fixture.Repository, fixture.Repository.Head.CanonicalName);

            return(sp.GetService <INextVersionCalculator>());
        }
Ejemplo n.º 18
0
 public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
 {
     fixture.AssertFullSemver(new Config(), fullSemver, repository, commitId, onlyTrackedBranches, targetBranch);
 }
Ejemplo n.º 19
0
 public MsBuildTaskFixture(RepositoryFixtureBase fixture) => this.fixture = fixture;
Ejemplo n.º 20
0
 private static Model.VersionResult GetResult(RepositoryFixtureBase repo) => VersionCalculator.Default().GetResult(repo.RepositoryPath);