コード例 #1
0
    public void UsesGitVersionConfigWhenCreatingDynamicRepository()
    {
        var localRepoPath = PathHelper.GetTempPath();
        var repoBasePath  = Path.GetDirectoryName(PathHelper.GetTempPath());

        Directory.CreateDirectory(localRepoPath);

        try
        {
            using (var remote = new EmptyRepositoryFixture(new Config()))
            {
                remote.Repository.MakeACommit();
                var configFile = Path.Combine(localRepoPath, "GitVersionConfig.yaml");
                File.WriteAllText(configFile, "next-version: 1.0.0");

                var arguments = string.Format(" /url {0} /dynamicRepoLocation {1} /b master", remote.RepositoryPath, repoBasePath);
                var results   = GitVersionHelper.ExecuteIn(localRepoPath, arguments, false);
                results.OutputVariables.SemVer.ShouldBe("1.0.0");
            }
        }
        finally
        {
            DeleteHelper.DeleteGitRepository(localRepoPath);
            DeleteHelper.DeleteGitRepository(repoBasePath);
        }
    }
コード例 #2
0
    public void WorkingDirectoryWithoutGitFolderFailsWithInformativeMessage()
    {
        var result = GitVersionHelper.ExecuteIn(System.Environment.SystemDirectory, arguments: null, logToFile: false);

        result.ExitCode.ShouldNotBe(0);
        result.Output.ShouldContain("Cannot find the .git directory");
    }
コード例 #3
0
    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.FromJson(json);

        outputVariables.ShouldNotBeNull();
        outputVariables.FullSemVer.ShouldBeEquivalentTo(version);
    }
コード例 #4
0
    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'");
    }
コード例 #5
0
    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.");
    }
コード例 #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, this.wixVersionFileName)));
    }
コード例 #7
0
    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);
    }
コード例 #8
0
    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""");
    }
コード例 #9
0
        public ExecutionResults ExecuteGitVersion(bool inProcess = true)
        {
            if (!inProcess)
            {
                return(GitVersionHelper.ExecuteIn(RepositoryPath));
            }

            var vf = new GitVersionFinder();
            var sv = vf.FindVersion(new GitVersionContext(Repository));

            var vars = VariableProvider.GetVariablesFor(sv);

            return(new InProcessExecutionResults(vars));
        }
コード例 #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("}");
    }
コード例 #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", EnvironmentVariableTarget.Process);

            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, EnvironmentVariableTarget.Process);
        }
    }
コード例 #12
0
        public void RunExecViaCommandLine()
        {
            using (var fixture = new EmptyRepositoryFixture())
            {
                fixture.Repository.MakeATaggedCommit(TaggedVersion);
                fixture.Repository.MakeACommit();

                var buildFile = Path.Combine(fixture.RepositoryPath, "TestBuildFile.proj");
                File.WriteAllBytes(buildFile, Resources.TestBuildFile);
                var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, MsBuild, "TestBuildFile.proj /target:OutputResults");

                result.ExitCode.ShouldBe(0);
                result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
            }
        }
コード例 #13
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, "TestBuildFile.proj");
                File.WriteAllBytes(buildFile, Resources.TestBuildFile);
                var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, projectFile: "TestBuildFile.proj", projectArgs: "/target:OutputResults");

                result.ExitCode.ShouldBe(0);
                result.Log.ShouldContain("FullSemVer: 1.2.3+0");
            }
        }
コード例 #14
0
        public void CanTakeVersionFromReleaseBranch()
        {
            using (var fixture = new EmptyRepositoryFixture())
            {
                const string TaggedVersion = "1.0.3";
                fixture.Repository.MakeATaggedCommit(TaggedVersion);
                fixture.Repository.MakeCommits(5);
                fixture.Repository.CreateBranch("alpha-2.0.0");
                fixture.Repository.Checkout("alpha-2.0.0");

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

                result.ExitCode.ShouldBe(0);
                result.OutputVariables[VariableProvider.FullSemVer].ShouldBe("2.0.0-alpha.1+5");
            }
        }
コード例 #15
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, this.wixVersionFileName));
        var gitVersionVars      = VersionVariables.AvailableVariables;

        Assert.AreEqual(gitVersionVars.Count(), gitVersionVarsInWix.Count);
    }
コード例 #16
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);
    }
コード例 #17
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);
            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, SpecifiedArgumentRunner.BuildTool, "RunExecViaCommandLine.csproj /target:OutputResults");

            result.ExitCode.ShouldBe(0, result.Log);
            result.Log.ShouldContain("GitVersion_FullSemVer: 1.2.4+1");
        }
    }
コード例 #18
0
    public void InvalidArgumentsExitCodeShouldNotBeZero()
    {
        using (var fixture = new EmptyRepositoryFixture(new Config()))
        {
            fixture.Repository.MakeATaggedCommit("1.2.3");
            fixture.Repository.MakeACommit();

            var buildFile = Path.Combine(fixture.RepositoryPath, "RunExecViaCommandLine.proj");
            File.Delete(buildFile);
            const string buildFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project 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, arguments: " /invalid-argument");

            result.ExitCode.ShouldBe(1);
            result.Output.ShouldContain("Failed to parse arguments");
        }
    }
コード例 #19
0
    public void RunsMsBuildProvideViaCommandLineArg()
    {
        const string TaggedVersion = "1.2.3";

        using (var fixture = new EmptyRepositoryFixture(new Config()))
        {
            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 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");
        }
    }
コード例 #20
0
    public void WixVersionFileContentTest()
    {
        using var fixture = new EmptyRepositoryFixture();
        fixture.MakeATaggedCommit("1.2.3");
        fixture.MakeACommit();

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

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

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

        foreach (var variable in gitVersionVars)
        {
            vars.TryGetValue(variable, out var 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]);
        }
    }
コード例 #21
0
    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);
        }
    }
コード例 #22
0
    public void WorkingDirectoryWithoutGitFolderCrashesWithInformativeMessage()
    {
        var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, isTeamCity: false, logToFile: false);

        results.Output.ShouldContain("Can't find the .git directory in");
    }
コード例 #23
0
 public void WhenGitHubFlowVersionIsExecuted()
 {
     _result = GitVersionHelper.ExecuteIn(_data.RepositoryPath);
 }
コード例 #24
0
    public void InvalidWorkingDirectoryCrashesWithInformativeMessage()
    {
        var results = GitVersionHelper.ExecuteIn("InvalidDirectory", null, isTeamCity: false, logToFile: false);

        results.Output.ShouldContain("InvalidDirectory");
    }