github.com/adevinta/maiao@v0.0.0-20240318133227-b6f9656b5e07/license_test.go (about)

     1  package maiao
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  
     8  	pkggodevclient "github.com/guseggert/pkggodev-client"
     9  	"github.com/stretchr/testify/require"
    10  	"golang.org/x/mod/modfile"
    11  )
    12  
    13  var (
    14  	acceptedLicenses = map[string]struct{}{
    15  		"MIT":          struct{}{},
    16  		"Apache-2.0":   struct{}{},
    17  		"BSD-3-Clause": struct{}{},
    18  		"BSD-2-Clause": struct{}{},
    19  		"ISC":          struct{}{},
    20  	}
    21  
    22  	knownUndectedLicenses = map[string]string{
    23  		// bufpipe was later added the MIT license: https://github.com/acomagu/bufpipe/blob/cd7a5f79d3c413d14c0c60fd31dae7b397fc955a/LICENSE
    24  		"github.com/acomagu/bufpipe@v1.0.3": "MIT",
    25  	}
    26  )
    27  
    28  func TestLicenses(t *testing.T) {
    29  	b, err := ioutil.ReadFile("go.mod")
    30  	require.NoError(t, err)
    31  	file, err := modfile.Parse("go.mod", b, nil)
    32  	require.NoError(t, err)
    33  	client := pkggodevclient.New()
    34  	for _, req := range file.Require {
    35  		pkg, err := client.DescribePackage(pkggodevclient.DescribePackageRequest{
    36  			Package: req.Mod.Path,
    37  		})
    38  		require.NoError(t, err)
    39  		licences := strings.Split(pkg.License, ",")
    40  		for _, license := range licences {
    41  			license = strings.TrimSpace(license)
    42  			if license == "None detected" {
    43  				if known, ok := knownUndectedLicenses[req.Mod.String()]; ok {
    44  					license = known
    45  				}
    46  			}
    47  			if _, ok := acceptedLicenses[license]; !ok {
    48  				t.Errorf("dependency %s is using unexpected license %s. Check that this license complies with MIT in which maiao is released and update the checks accordingly or change dependency", req.Mod, license)
    49  			}
    50  		}
    51  	}
    52  }