Project to validate clean architecture in golang
"archTest" is a Go package that provides a testing framework for architecture compliance in Go projects. It allows you to define and enforce architectural rules and dependencies between packages.
To use archTest, you need to have Go installed and set up on your machine. You can install the package using the following go get command:
go get github.com/ANDERSON1808/archTest@latest
To use archTest, you need to import the package in your test file:
import "github.com/ANDERSON1808/archTest.git
The Package function is the entry point for defining architecture tests. It creates a new PackageTest instance.
func Package(t TestingT, packageName ...string) *PackageTest
- t: A TestingT interface implementation that provides testing functionality (e.g., *testing.T).
- packageName: Variadic parameter(s) representing the package name(s) to be tested.
The PackageTest type provides the following methods:
IncludeTests
func (t *PackageTest) IncludeTests() *PackageTest
This method includes test packages when analyzing dependencies. Ignoring
func (t *PackageTest) Ignoring(e ...string) *PackageTest
This method allows you to specify packages to be ignored during the dependency analysis.
ShouldNotDependDirectlyOn
func (t *PackageTest) ShouldNotDependDirectlyOn(pegs ...string)
This method specifies that the tested package(s) should not depend directly on the provided package(s). It generates an error if a direct dependency is found.
ShouldNotDependOn
func (t *PackageTest) ShouldNotDependOn(pkgs ...string)
This method specifies that the tested package(s) should not depend on the provided package(s) at any depth. It generates an error if a dependency is found.
TestingT Interface The TestingT interface is used for error reporting during testing. It is usually implemented by testing frameworks such as *testing.T. The interface provides an Errorf method to report test failures.
Here's an example of how to use archTest to enforce architectural rules in a Go project:
package myproject_test
import (
"testing"
"github.com/ANDERSON1808/archTest"
)
func TestArchitecture(t *testing.T) {
archTest.Package(t, "myproject").
IncludeTests().
ShouldNotDependDirectlyOn("externalpkg").
ShouldNotDependOn("badpkg").
ShouldNotDependOn("anotherpkg")
}
In the example above, the TestArchitecture function defines architectural rules for the myproject package. It ensures that the myproject package should not depend directly on the externalpkg package and should not depend on the badpkg and anotherpkg packages at any depth. If any violations are found, the test will fail.
archtest.Package(t, "github.com/myorg/myproject/....").
ShouldNotDependOn("github.com/some/package")
archtest.Package(t, "github.com/myorg/myproject/....").
ShouldNotDependDirectlyOn("github.com/some/package")
archtest.Package(t, "github.com/myorg/myproject/....").
IncludeTests().
ShouldNotDependDirectlyOn("github.com/some/package")
archTest provides a convenient way to enforce architectural rules and dependencies in Go projects. By using the provided methods, you can define your architectural constraints and ensure that your codebase adheres to them.