public void GetResult_Malformed_Json_At_Commit_Throws()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            // Write the version file (with parsing errors)
            var file = Path.Combine(fixture.RepositoryPath, Constants.ConfigurationFileName);

            using (var writer = File.AppendText(file))
            {
                writer.WriteLine("This will not parse");
                writer.Flush();
            }

            fixture.Repository.Index.Add(Constants.ConfigurationFileName);
            fixture.Repository.Index.Write();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            Action action = () => sut.GetResult();

            // Assert
            action.Should().Throw <InvalidOperationException>()
            .WithMessage($"Could not read '{Constants.ConfigurationFileName}', has it been committed?");
        }
        public void GetResult_WithProcessors_CallsInOrder()
        {
            // Arrange
            var calledProcessors = new List <int>();
            var processors       = new[]
            {
                Substitute.For <IVersionProcessor>(),
                Substitute.For <IVersionProcessor>(),
                Substitute.For <IVersionProcessor>()
            };

            for (var x = 0; x < processors.Length; x++)
            {
                var value = x;
                processors[x].When(p => p.Process(Arg.Any <IVersionContext>()))
                .Do(_ => calledProcessors.Add(value));
            }

            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, processors);

            // Act
            var result = sut.GetResult();

            // Assert
            calledProcessors.Should().ContainInOrder(0, 1, 2);
        }
        public void GetResult_Feature_Branch_Changes_Version_Resets_On_Merge()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var config = fixture.GetConfig();

            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.BranchTo("feature/other");
            fixture.MakeACommit();

            config.Version = "0.1.1";
            fixture.SetConfig(config);

            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.Checkout("master");
            fixture.MergeNoFF("feature/other");

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            result.Height.Should().Be(1);
        }
        public void GetResult_BranchOverride_AppliesOverride()
        {
            // Arrange
            var expectedLabel = new List <string> {
                "{branchName}"
            };
            var expectedMeta = new List <string> {
                "meta"
            };

            var config = new RepositoryConfiguration
            {
                Version  = "0.1.0",
                Branches =
                {
                    Overrides        =
                    {
                        new BranchOverrideConfiguration
                        {
                            Match    = "feature/other",
                            Label    = expectedLabel,
                            Metadata = expectedMeta
                        }
                    }
                }
            };

            using var fixture = new SimpleVersionRepositoryFixture(config, _serializer);

            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.BranchTo("feature/other");
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            // Use a processor to capture the configuration during processing.
            var            processor = Substitute.For <IVersionProcessor>();
            VersionContext context   = null;

            processor.When(x => x.Process(Arg.Any <VersionContext>()))
            .Do(call => context = call.Arg <VersionContext>());

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, new[] { processor });

            // Act
            var result = sut.GetResult();

            context.Configuration.Label.Should().BeEquivalentTo(expectedLabel, options => options.WithStrictOrdering());
            context.Configuration.Metadata.Should().BeEquivalentTo(expectedMeta, options => options.WithStrictOrdering());
        }
        public void GetResult_First_Commit_Height_Is_One()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.Height.Should().Be(1);
        }
        public void GetResult_SetsSha()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.Sha.Should().Be(fixture.Repository.Head.Tip.Sha);
        }
        public void GetResult_NoEnvironmentBranchName_UsesRepoValueInResult()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.BranchName.Should().Be(fixture.Repository.Head.FriendlyName);
        }
        public void GetResult_EmptyProcessors_ReturnsResult()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.Should().NotBeNull();
        }
Example #9
0
        public void Override_Branches_Do_Not_Work_If_Asterisk_Used_In_Label()
        {
            // Create the configuration model
            var config = new Model.Configuration
            {
                Version  = "1.0.0",
                Label    = { "r*" },
                Branches =
                {
                    Release       =
                    {
                        "^refs/heads/master$",
                        "^refs/heads/release/.+$",
                        "^refs/heads/feature/.+$"
                    },
                    Overrides     =
                    {
                        new Model.BranchConfiguration
                        {
                            Match = "^refs/heads/feature/.+$",
                            Label = new List <string>{
                                "{shortbranchname}"
                            }
                        }
                    }
                }
            };

            // Arrange
            using (var fixture = new SimpleVersionRepositoryFixture(config))
                using (EnvrionmentContext.NoBuildServer())
                {
                    // Make some extra commits on master
                    fixture.MakeACommit();
                    fixture.MakeACommit();
                    fixture.MakeACommit();

                    // branch to a feature branch
                    fixture.BranchTo("feature/PBI-319594-GitVersionDeprecation");
                    fixture.MakeACommit();
                    fixture.MakeACommit();
                    fixture.MakeACommit();

                    // Act
                    var result  = GetResult(fixture);
                    var semver1 = result.Formats[Semver1FormatProcess.FormatKey];
                    var semver2 = result.Formats[Semver2FormatProcess.FormatKey];

                    // Assert
                    semver1.Should().Be("1.0.0-featurePBI319594GitVersionDeprecation-0007");
                    semver2.Should().Be("1.0.0-featurePBI319594GitVersionDeprecation.7");
                }
        }
        public void GetResult_SetsIsRelease(string branchName, bool isRelease)
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            fixture.BranchTo(branchName);

            // Act
            var result = sut.GetResult();

            // Assert
            result.IsRelease.Should().Be(isRelease);
        }
        public void GetResult_EnvironmentBranchName_UsesEnvironmentValueInResult()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var expected = "FROMENV";

            _environment.BranchName.Returns(expected);

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.BranchName.Should().Be(expected);
        }
        public void GetResult_Single_Branch_Increments_Each_Commit()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            // Act
            var result = sut.GetResult();

            // Assert
            result.Height.Should().Be(5);
        }
        public void GetResult_InvalidCanonicalName_Throws(string branchName)
        {
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            _environment.CanonicalBranchName.Returns(branchName);
            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // set to headless state
            var commit = fixture.Repository.Head.Tip;

            Commands.Checkout(fixture.Repository, fixture.Repository.Head.Tip);

            // Act
            Action action = () => sut.GetResult();

            // Assert
            action.Should().Throw <GitException>()
            .And.Message.Should().Be("The branch name could not be resolved from the repository or the environment. Ensure you have checked out a branch (not a commit).");
        }
        public void GetResult_Feature_Branch_Sets_BranchName()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.BranchTo("feature/other");
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            result.BranchName.Should().Be("feature/other");
            result.CanonicalBranchName.Should().Be("refs/heads/feature/other");
        }
        public void GetResult_Modified_No_Version_Or_Label_Changes_Does_Not_Reset()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);

            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            var config = fixture.GetConfig();

            config.Metadata.Add("example");
            fixture.SetConfig(config);

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            // Assert
            result.Height.Should().Be(6);
        }
        public void GetResult_Malformed_Json_Committed_Counts_As_No_Change()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            // Write the version file (with parsing errors)
            var file = Path.Combine(fixture.RepositoryPath, Constants.ConfigurationFileName);

            using (var writer = File.AppendText(file))
            {
                writer.WriteLine("This will not parse");
                writer.Flush();
            }

            fixture.Repository.Index.Add(Constants.ConfigurationFileName);
            fixture.Repository.Index.Write();
            fixture.MakeACommit(); // 5
            fixture.MakeACommit(); // 6
            fixture.MakeACommit(); // 7
            fixture.MakeACommit(); // 8

            var config = new RepositoryConfiguration {
                Version = "0.1.0"
            };

            fixture.SetConfig(config);

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            result.Height.Should().Be(9);
        }
        public void GetResult_Feature_Branch_No_Change_Increments_Merge_Once()
        {
            // Arrange
            using var fixture = new SimpleVersionRepositoryFixture(_serializer);
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.BranchTo("feature/other");
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.Checkout("master");
            fixture.MergeNoFF("feature/other");

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, Enumerable.Empty <IVersionProcessor>());

            // Act
            var result = sut.GetResult();

            result.Height.Should().Be(6);
        }
        public void GetResult_AdvancedBranchOverride_AppliesOverride()
        {
            // Arrange
            var expectedLabel = new List <string> {
                "preL1", "preL2", "L1", "insertedL", "L2", "postL1", "postL2"
            };
            var expectedMeta = new List <string> {
                "preM1", "preM2", "M1", "insertedM", "M2", "postM1", "postM2"
            };

            var config = new RepositoryConfiguration
            {
                Version  = "0.1.0",
                Label    = { "L1", "L2" },
                Metadata = { "M1", "M2" },
                Branches =
                {
                    Overrides               =
                    {
                        new BranchOverrideConfiguration
                        {
                        Match       = "feature/other",
                        PrefixLabel = new List <string> {
                            "preL1", "preL2"
                        },
                        PostfixLabel    = new List <string>{
                            "postL1", "postL2"
                        },
                        InsertLabel     = new Dictionary <int,string>{
                            [1] = "insertedL"
                        },
                        PrefixMetadata  = new List <string>{
                            "preM1", "preM2"
                        },
                        PostfixMetadata = new List <string> {
                            "postM1", "postM2"
                        },
                        InsertMetadata  = new Dictionary <int,string> {
                            [1] = "insertedM"
                        }
                        }
                    }
                }
            };

            using var fixture = new SimpleVersionRepositoryFixture(config, _serializer);
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            fixture.BranchTo("feature/other");
            fixture.MakeACommit();
            fixture.MakeACommit();
            fixture.MakeACommit();

            // Use a processor to capture the configuration during processing.
            var            processor = Substitute.For <IVersionProcessor>();
            VersionContext context   = null;

            processor.When(x => x.Process(Arg.Any <VersionContext>()))
            .Do(call => context = call.Arg <VersionContext>());

            var sut = new GitVersionRepository(fixture.RepositoryPath, _environment, _serializer, new[] { processor });

            // Act
            var result = sut.GetResult();

            context.Configuration.Label.Should().BeEquivalentTo(expectedLabel, options => options.WithStrictOrdering());
            context.Configuration.Metadata.Should().BeEquivalentTo(expectedMeta, options => options.WithStrictOrdering());
        }