コード例 #1
0
    public void GetIdAsVersion_Roundtrip_WithSubdirectoryVersionFiles()
    {
        var rootVersionExpected = VersionOptions.FromVersion(new Version(1, 0));

        this.Context.VersionFile.SetVersion(this.RepoPath, rootVersionExpected);

        var          subPathVersionExpected = VersionOptions.FromVersion(new Version(1, 1));
        const string subPathRelative        = "a";
        string       subPath = Path.Combine(this.RepoPath, subPathRelative);

        Directory.CreateDirectory(subPath);
        this.Context.VersionFile.SetVersion(subPath, subPathVersionExpected);

        this.InitializeSourceControl();

        Commit  head = this.LibGit2Repository.Head.Commits.First();
        Version rootVersionActual    = this.GetVersion(committish: head.Sha);
        Version subPathVersionActual = this.GetVersion(subPathRelative, head.Sha);

        // Verify that the versions calculated took the path into account.
        Assert.Equal(rootVersionExpected.Version.Version.Minor, rootVersionActual?.Minor);
        Assert.Equal(subPathVersionExpected.Version.Version.Minor, subPathVersionActual?.Minor);

        // Verify that we can find the commit given the version and path.
        Assert.Equal(head, LibGit2GitExtensions.GetCommitFromVersion(this.Context, rootVersionActual));
        this.Context.RepoRelativeProjectDirectory = subPathRelative;
        Assert.Equal(head, LibGit2GitExtensions.GetCommitFromVersion(this.Context, subPathVersionActual));

        // Verify that mismatching path and version results in a null value.
        Assert.Null(LibGit2GitExtensions.GetCommitFromVersion(this.Context, rootVersionActual));
        this.Context.RepoRelativeProjectDirectory = string.Empty;
        Assert.Null(LibGit2GitExtensions.GetCommitFromVersion(this.Context, subPathVersionActual));
    }
コード例 #2
0
    private void VerifyCommitsWithVersion(Commit[] commits)
    {
        Requires.NotNull(commits, nameof(commits));

        for (int i = 0; i < commits.Length; i++)
        {
            Version encodedVersion = this.GetVersion(committish: commits[i].Sha);
            Assert.Equal(i + 1, encodedVersion.Build);
            Assert.Equal(commits[i], LibGit2GitExtensions.GetCommitFromVersion(this.Context, encodedVersion));
        }
    }
コード例 #3
0
    [SkippableFact(Skip = "It fails already.")] // Skippable, only run test on specific machine
    public void TestBiggerRepo()
    {
        var testBiggerRepoPath = @"D:\git\NerdBank.GitVersioning";

        Skip.If(!Directory.Exists(testBiggerRepoPath));

        using var largeRepo = new Repository(testBiggerRepoPath);
        foreach (var commit in largeRepo.Head.Commits)
        {
            var version = this.GetVersion("src", commit.Sha);
            this.Logger.WriteLine($"commit {commit.Id} got version {version}");
            using var context = LibGit2Context.Create("src", commit.Sha);
            var backAgain = LibGit2GitExtensions.GetCommitFromVersion(context, version);
            Assert.Equal(commit, backAgain);
        }
    }
コード例 #4
0
    public void GetIdAsVersion_Roundtrip(string version, string assemblyVersion, int versionHeightOffset)
    {
        var          semanticVersion          = SemanticVersion.Parse(version);
        const string repoRelativeSubDirectory = "subdir";

        this.WriteVersionFile(
            new VersionOptions
        {
            Version             = semanticVersion,
            AssemblyVersion     = new VersionOptions.AssemblyVersionOptions(new Version(assemblyVersion)),
            VersionHeightOffset = versionHeightOffset,
        },
            repoRelativeSubDirectory);

        Commit[]  commits  = new Commit[16]; // create enough that statistically we'll likely hit interesting bits as MSB and LSB
        Version[] versions = new Version[commits.Length];
        for (int i = 0; i < commits.Length; i++)
        {
            commits[i] = this.LibGit2Repository.Commit($"Commit {i + 1}", this.Signer, this.Signer, new CommitOptions {
                AllowEmptyCommit = true
            });
            versions[i] = this.GetVersion(repoRelativeSubDirectory, commits[i].Sha);
            this.Logger.WriteLine($"Commit {commits[i].Id.Sha.Substring(0, 8)} as version: {versions[i]}");
        }

        this.Context.RepoRelativeProjectDirectory = repoRelativeSubDirectory;
        for (int i = 0; i < commits.Length; i++)
        {
            Assert.Equal(commits[i], LibGit2GitExtensions.GetCommitFromVersion(this.Context, versions[i]));

            // Also verify that we can find it without the revision number.
            // This is important because stable, publicly released NuGet packages
            // that contain no assemblies may only have major.minor.build as their version evidence.
            // But folks who specify a.b.c version numbers don't have any unique version component for the commit at all without the 4th integer.
            if (semanticVersion.Version.Build == -1)
            {
                Assert.Equal(commits[i], LibGit2GitExtensions.GetCommitFromVersion(this.Context, new Version(versions[i].Major, versions[i].Minor, versions[i].Build)));
            }
        }
    }