public void BeingOnBuildServerWithOutputJsonAndOutputFileDoesNotFail(string outputFile, string fileName)
        {
            using var fixture = new RemoteRepositoryFixture();
            fixture.Repository.MakeATaggedCommit("1.2.3");
            fixture.Repository.MakeACommit();

            var env = new KeyValuePair <string, string>(TeamCity.EnvironmentVariableName, "8.0.0");

            var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: $" /output json /output buildserver /output file /outputfile {outputFile}", environments: env);

            result.ExitCode.ShouldBe(0);
            const string version = "0.1.0+4";

            result.Output.ShouldContain($"##teamcity[buildNumber '{version}']");
            result.OutputVariables.ShouldNotBeNull();
            result.OutputVariables.FullSemVer.ShouldBeEquivalentTo(version);

            var filePath = Path.Combine(fixture.LocalRepositoryFixture.RepositoryPath, fileName);
            var json     = File.ReadAllText(filePath);

            var outputVariables = VersionVariables.FromDictionary(JsonConvert.DeserializeObject <Dictionary <string, string> >(json));

            outputVariables.ShouldNotBeNull();
            outputVariables.FullSemVer.ShouldBeEquivalentTo(version);
        }
        public void WorkingDirectoryWithoutGitFolderFailsWithInformativeMessage()
        {
            var result = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, arguments: null, logToFile: false);

            result.ExitCode.ShouldNotBe(0);
            result.Output.ShouldContain("Cannot find the .git directory");
        }
        public void WixVersionFileContentTest()
        {
            using (var fixture = new EmptyRepositoryFixture())
            {
                fixture.MakeATaggedCommit("1.2.3");
                fixture.MakeACommit();

                var gitVersionExecutionResults = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);
                VersionVariables vars          = gitVersionExecutionResults.OutputVariables;

                GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " -updatewixversionfile");

                var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, WixVersionFileName));
                var gitVersionVars      = VersionVariables.AvailableVariables;

                foreach (var variable in gitVersionVars)
                {
                    string value;
                    vars.TryGetValue(variable, out value);
                    //Make sure the variable is present in the Wix file
                    Assert.IsTrue(gitVersionVarsInWix.ContainsKey(variable));
                    //Make sure the values are equal
                    Assert.AreEqual(value, gitVersionVarsInWix[variable]);
                }
            }
        }
        public void InvalidArgumentsExitCodeShouldNotBeZero()
        {
            using var fixture = new EmptyRepositoryFixture();
            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");

            result.ExitCode.ShouldNotBe(0);
            result.Output.ShouldContain("Could not parse command line parameter '/invalid-argument'");
        }
        public void WorkingDirectoryWithoutCommitsFailsWithInformativeMessage()
        {
            using var fixture = new EmptyRepositoryFixture();

            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null, logToFile: false);

            result.ExitCode.ShouldNotBe(0);
            result.Output.ShouldContain("No commits found on the current branch.");
        }
Exemple #6
0
        public void WixVersionFileCreationTest()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /updatewixversionfile");
            Assert.IsTrue(File.Exists(Path.Combine(fixture.RepositoryPath, wixVersionFileName)));
        }
        public void CheckBuildServerVerbosityConsole(string verbosityArg, string expectedOutput)
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: $@" {verbosityArg} -output buildserver /l ""/tmp/path""", logToFile: false);

            result.ExitCode.ShouldBe(0);
            result.Output.ShouldContain(expectedOutput);
        }
        public void LogPathContainsForwardSlash()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: @" /l ""/tmp/path""", logToFile: false);

            result.ExitCode.ShouldBe(0);
            result.Output.ShouldContain(@"""MajorMinorPatch"":""1.2.4""");
        }
        public void BeingOnBuildServerDoesntOverrideOutputJson()
        {
            using var fixture = new RemoteRepositoryFixture();
            fixture.Repository.MakeATaggedCommit("1.2.3");
            fixture.Repository.MakeACommit();

            var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", isTeamCity: true);

            result.ExitCode.ShouldBe(0);
            result.Output.ShouldStartWith("{");
            result.Output.TrimEnd().ShouldEndWith("}");
        }
Exemple #10
0
        public void BeingOnBuildServerDoesntOverrideOutputJson()
        {
            using var fixture = new RemoteRepositoryFixture();
            fixture.Repository.MakeATaggedCommit("1.2.3");
            fixture.Repository.MakeACommit();

            var env = new KeyValuePair <string, string>(TeamCity.EnvironmentVariableName, "8.0.0");

            var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json", environments: env);

            result.ExitCode.ShouldBe(0);
            result.Output.ShouldStartWith("{");
            result.Output.TrimEnd().ShouldEndWith("}");
        }
Exemple #11
0
        public void GivenJenkinsPipelineHasDuplicatedOrigin_VersionIsCalculatedProperly()
        {
            string pipelineBranch     = "BRANCH_NAME";
            string pipelineBranchOrig = Environment.GetEnvironmentVariable(pipelineBranch);

            using (var fixture = new EmptyRepositoryFixture())
            {
                var remoteRepositoryPath = PathHelper.GetTempPath();
                Repository.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);
                    // Jenkins Pipeline will create a duplicate origin:
                    fixture.Repository.Network.Remotes.Add("origin1", 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, "master"); // HEAD cannot be pointing at the merge commit
                    remoteRepository.Refs.Add("refs/heads/pull/5/head", new ObjectId(mergeCommitSha));

                    // Checkout PR commit
                    Commands.Fetch((Repository)fixture.Repository, "origin", new string[0], new FetchOptions(), null);
                    Commands.Checkout(fixture.Repository, mergeCommitSha);
                }

                // Emulating Jenkins environment variable
                Environment.SetEnvironmentVariable(pipelineBranch, "PR-5");
                Environment.SetEnvironmentVariable("JENKINS_URL", "url");

                var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath);

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

                // Cleanup repository files
                DirectoryHelper.DeleteDirectory(remoteRepositoryPath);

                Environment.SetEnvironmentVariable(pipelineBranch, pipelineBranchOrig);
                Environment.SetEnvironmentVariable("JENKINS_URL", null);
            }
        }
Exemple #12
0
        public void WixVersionFileVarCountTest()
        {
            //Make sure we have captured all the version variables by count in the Wix version file
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null);

            GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /updatewixversionfile");

            var gitVersionVarsInWix = GetGitVersionVarsInWixFile(Path.Combine(fixture.RepositoryPath, wixVersionFileName));
            var gitVersionVars      = VersionVariables.AvailableVariables;

            Assert.AreEqual(gitVersionVars.Count(), gitVersionVarsInWix.Count);
        }
        public void RunsMsBuildProvideViaCommandLineArg()
        {
            const string taggedVersion = "1.2.3";

            using var fixture = new EmptyRepositoryFixture();
            fixture.Repository.MakeATaggedCommit(taggedVersion);

            var buildFile = Path.Combine(fixture.RepositoryPath, "RunsMsBuildProvideViaCommandLineArg.proj");

            File.Delete(buildFile);
            File.WriteAllText(buildFile, TestProject);
            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, projectFile: "RunsMsBuildProvideViaCommandLineArg.proj", projectArgs: "/target:OutputResults");

            result.ExitCode.ShouldBe(0);
            result.Log.ShouldContain("FullSemVer: 1.2.3");
        }
Exemple #14
0
        public void BeingOnBuildServerWithOutputJsonDoesNotFail()
        {
            using var fixture = new RemoteRepositoryFixture();
            fixture.Repository.MakeATaggedCommit("1.2.3");
            fixture.Repository.MakeACommit();

            var env = new KeyValuePair <string, string>(TeamCity.EnvironmentVariableName, "8.0.0");

            var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: " /output json /output buildserver", environments: env);

            result.ExitCode.ShouldBe(0);
            const string version = "0.1.0+4";

            result.Output.ShouldContain($"##teamcity[buildNumber '{version}']");
            result.OutputVariables.ShouldNotBeNull();
            result.OutputVariables.FullSemVer.ShouldBeEquivalentTo(version);
        }
Exemple #15
0
        public void RunExecViaCommandLine()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            var buildFile = Path.Combine(fixture.RepositoryPath, "RunExecViaCommandLine.csproj");

            File.Delete(buildFile);

            File.WriteAllText(buildFile, MsBuildProjectArgTest.TestProject);
            var exec     = "dotnet";
            var execArgs = "msbuild RunExecViaCommandLine.csproj /target:OutputResults";
            var result   = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, exec, execArgs);

            result.ExitCode.ShouldBe(0, result.Log);
            result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
        }
        public void RunExecViaCommandLine()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.MakeATaggedCommit("1.2.3");
            fixture.MakeACommit();

            var buildFile = Path.Combine(fixture.RepositoryPath, "RunExecViaCommandLine.csproj");

            File.Delete(buildFile);
            const string buildFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  <Target Name=""OutputResults"">
    <Message Text=""GitVersion_FullSemVer: $(GitVersion_FullSemVer)""/>
  </Target>
</Project>";

            File.WriteAllText(buildFile, buildFileContent);
            var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, ExecCommand.BuildTool, "RunExecViaCommandLine.csproj /target:OutputResults");

            result.ExitCode.ShouldBe(0, result.Log);
            result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
        }
Exemple #17
0
        public void RunsMsBuildProvideViaCommandLineArg()
        {
            const string TaggedVersion = "1.2.3";

            using (var fixture = new EmptyRepositoryFixture())
            {
                fixture.Repository.MakeATaggedCommit(TaggedVersion);

                var buildFile = Path.Combine(fixture.RepositoryPath, "RunsMsBuildProvideViaCommandLineArg.proj");
                File.Delete(buildFile);
                const string buildFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
  <Target Name=""OutputResults"">
    <Message Text=""GitVersion_FullSemVer: $(GitVersion_FullSemVer)""/>
  </Target>
</Project>";
                File.WriteAllText(buildFile, buildFileContent);
                var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, projectFile: "RunsMsBuildProvideViaCommandLineArg.proj", projectArgs: "/target:OutputResults");

                result.ExitCode.ShouldBe(0);
                result.Log.ShouldContain("FullSemVer: 1.2.3");
            }
        }
        public void GivenARemoteWithATagOnMaster_AndAPullRequestWithTwoCommits_AndBuildIsRunningInTeamCity_VersionIsCalculatedProperly(string pullRequestRef)
        {
            using (var fixture = new EmptyRepositoryFixture())
            {
                var remoteRepositoryPath = PathHelper.GetTempPath();
                Repository.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, "master"); // HEAD cannot be pointing at the merge commit
                    remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));

                    // Checkout PR commit
                    Commands.Fetch((Repository)fixture.Repository, "origin", new string[0], new FetchOptions(), null);
                    Commands.Checkout(fixture.Repository, mergeCommitSha);
                }

                var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, isTeamCity: true);

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

                // Cleanup repository files
                DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
            }
        }
Exemple #19
0
        public void WorkingDirectoryWithoutGitFolderCrashesWithInformativeMessage()
        {
            var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, false);

            results.Output.ShouldContain("Can't find the .git directory in");
        }