Skip to content

Simple project to approve the public API of a project (useful for semantic versioning)

License

Notifications You must be signed in to change notification settings

gpetrou/ApiApprover

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build status

ApiApprover

This package is obsoleted with a compiler warning

Api Approver is a simple NuGet package built on top of ApprovalTests.Net which approves the public API of an assembly.

Whenever the public API changes the Api Approver test will fail. Running the test manually will pop up a diff tool allowing you to see the changes. If the changes are fine (i.e an added overload) then the changes can simply be approved and test will pass again.

ApiApprover

There are times though that changes to the public API is accidental. Api Approver simply adds a step that all public API changes have to be reviewed by a developer and accepted, saving accidental breaking changes being shipped.

PublicApiGenerator

PublicApiGenerator has no dependencies and simply creates a string the represents the public API. Any approval library can be used to approve the generated public API.

How do I use it

Install-package PublicApiGenerator

var publicApi = ApiGenerator.GeneratePublicApi(typeof(Library).Assembly);

Manual

[Fact]
public void my_assembly_has_no_public_api_changes()
{
    var publicApi = ApiGenerator.GeneratePublicApi(typeof(Library).Assembly);

    var approvedFilePath = "PublicApi.approved.txt";
    if (!File.Exists(approvedFilePath))
    {
        // Create a file to write to.
        using (var sw = File.CreateText(approvedFilePath)) { }
    }

    var approvedApi = File.ReadAllText(approvedFilePath);

    Assert.Equal(approvedApi, publicApi);
}

Shoudly

Install-package Shouldly

[Fact]
public void my_assembly_has_no_public_api_changes()
{
    var publicApi = ApiGenerator.GeneratePublicApi(typeof(Library).Assembly);

    //Shouldly
    publicApi.ShouldMatchApproved();
}

ApprovalTests

Install-package ApprovalTests

[Fact]
public void my_assembly_has_no_public_api_changes()
{
    var publicApi = ApiGenerator.GeneratePublicApi(typeof(Library).Assembly);;
    var writer = new ApprovalTextWriter(publicApi, "txt");
    var approvalNamer = new AssemblyPathNamer(assembly.Location);
    Approvals.Verify(writer, approvalNamer, Approvals.GetReporter());
}

private class AssemblyPathNamer : UnitTestFrameworkNamer
{
    private readonly string name;

    public AssemblyPathNamer(string assemblyPath)
    {
        name = Path.GetFileNameWithoutExtension(assemblyPath);
    }

    public override string Name
    {
        get { return name; }
    }
}

About

Simple project to approve the public API of a project (useful for semantic versioning)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%