github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/tools/test_matrix_generator/matrix_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"golang.org/x/tools/go/packages"
     9  )
    10  
    11  // TestLoadPackagesConfig ensure packages config json loads as expected.
    12  func TestLoadPackagesConfig(t *testing.T) {
    13  	configFile := `{"packagesPath": ".", "includeOthers": true, "packages": [{"name": "testPackage"}]}`
    14  	config := loadPackagesConfig(configFile)
    15  	if config.PackagesPath != "." || !config.IncludeOthers || len(config.Packages) != 1 {
    16  		t.Errorf("loadPackagesConfig failed for valid input")
    17  	}
    18  
    19  	invalidConfigFile := "invalidJSON"
    20  	defer func() {
    21  		if recover() == nil {
    22  			t.Errorf("loadPackagesConfig did not panic for invalid JSON input")
    23  		}
    24  	}()
    25  	loadPackagesConfig(invalidConfigFile)
    26  }
    27  
    28  // TestBuildMatrices ensures test matrices are built from config json as expected.
    29  func TestBuildMatrices(t *testing.T) {
    30  	t.Run("top level package only default runner", func(t *testing.T) {
    31  		name := "counter"
    32  		configFile := fmt.Sprintf(`{"packagesPath": ".", "includeOthers": true, "packages": [{"name": "%s"}]}`, name)
    33  		allPackges := goPackageFixture("counter/count", "counter/print/int", "counter/log")
    34  		cfg := loadPackagesConfig(configFile)
    35  		matrices := buildTestMatrices(cfg, func(dir string) []*packages.Package {
    36  			return allPackges
    37  		})
    38  		require.Equal(t, name, matrices[0].Name)
    39  		require.Equal(t, defaultCIRunner, matrices[0].Runner)
    40  		require.Equal(t, fmt.Sprintf("%s %s %s ", allPackges[0].PkgPath, allPackges[1].PkgPath, allPackges[2].PkgPath), matrices[0].Packages)
    41  		fmt.Println(matrices[0].Name, matrices[0].Runner, matrices[0].Packages)
    42  	})
    43  	t.Run("top level package only override runner", func(t *testing.T) {
    44  		name := "counter"
    45  		runner := "buildjet-4vcpu-ubuntu-2204"
    46  		configFile := fmt.Sprintf(`{"packagesPath": ".", "packages": [{"name": "%s", "runner":  "%s"}]}`, name, runner)
    47  		allPackges := goPackageFixture("counter/count", "counter/print/int", "counter/log")
    48  		cfg := loadPackagesConfig(configFile)
    49  		matrices := buildTestMatrices(cfg, func(dir string) []*packages.Package {
    50  			return allPackges
    51  		})
    52  		require.Equal(t, name, matrices[0].Name)
    53  		require.Equal(t, runner, matrices[0].Runner)
    54  		require.Equal(t, fmt.Sprintf("%s %s %s ", allPackges[0].PkgPath, allPackges[1].PkgPath, allPackges[2].PkgPath), matrices[0].Packages)
    55  	})
    56  	t.Run("top level package with sub packages include others", func(t *testing.T) {
    57  		topLevelPkgName := "network"
    58  		subPkg1 := "network/p2p/node"
    59  		subPkg2 := "module/chunks"
    60  		subPkg3 := "crypto/hash"
    61  		subPkg4 := "model/bootstrap"
    62  		subPkg1Runner := "buildjet-4vcpu-ubuntu-2204"
    63  		configFile := fmt.Sprintf(`
    64  			{"packagesPath": ".", "includeOthers": true, "packages": [{"name": "%s", "subpackages": [{"name": "%s", "runner": "%s"}, {"name": "%s"}, {"name": "%s"}, {"name": "%s"}]}]}`,
    65  			topLevelPkgName, subPkg1, subPkg1Runner, subPkg2, subPkg3, subPkg4)
    66  		allPackges := goPackageFixture(
    67  			"network",
    68  			"network/alsp",
    69  			"network/cache",
    70  			"network/channels",
    71  			"network/p2p/node",
    72  			"network/p2p/node/internal",
    73  			"module",
    74  			"module/chunks/chunky",
    75  			"crypto/hash",
    76  			"crypto/random",
    77  			"crypto/hash/ecc",
    78  			"model/bootstrap",
    79  			"model/bootstrap/info",
    80  			"model",
    81  		)
    82  		cfg := loadPackagesConfig(configFile)
    83  		matrices := buildTestMatrices(cfg, func(dir string) []*packages.Package {
    84  			return allPackges
    85  		})
    86  		require.Len(t, matrices, 6)
    87  		for _, matrix := range matrices {
    88  			switch matrix.Name {
    89  			case topLevelPkgName:
    90  				require.Equal(t, defaultCIRunner, matrix.Runner)
    91  				require.Equal(t, fmt.Sprintf("%s %s %s %s ", allPackges[0].PkgPath, allPackges[1].PkgPath, allPackges[2].PkgPath, allPackges[3].PkgPath), matrix.Packages)
    92  			case subPkg1:
    93  				require.Equal(t, subPkg1Runner, matrix.Runner)
    94  				require.Equal(t, fmt.Sprintf("%s %s ", allPackges[4].PkgPath, allPackges[5].PkgPath), matrix.Packages)
    95  			case subPkg2:
    96  				require.Equal(t, defaultCIRunner, matrix.Runner)
    97  				require.Equal(t, fmt.Sprintf("%s ", allPackges[7].PkgPath), matrix.Packages)
    98  			case subPkg3:
    99  				require.Equal(t, defaultCIRunner, matrix.Runner)
   100  				require.Equal(t, fmt.Sprintf("%s %s ", allPackges[8].PkgPath, allPackges[10].PkgPath), matrix.Packages)
   101  			case subPkg4:
   102  				require.Equal(t, defaultCIRunner, matrix.Runner)
   103  				require.Equal(t, fmt.Sprintf("%s %s ", allPackges[11].PkgPath, allPackges[12].PkgPath), matrix.Packages)
   104  			case "others":
   105  				require.Equal(t, defaultCIRunner, matrix.Runner)
   106  				require.Equal(t, fmt.Sprintf("%s %s %s ", allPackges[6].PkgPath, allPackges[9].PkgPath, allPackges[13].PkgPath), matrix.Packages)
   107  			default:
   108  				require.Fail(t, fmt.Sprintf("unexpected matrix name: %s", matrix.Name))
   109  			}
   110  		}
   111  	})
   112  	t.Run("top level package with sub packages and exclude", func(t *testing.T) {
   113  		topLevelPkgName := "network"
   114  		subPkg1 := "network/p2p/node"
   115  		subPkg1Runner := "buildjet-4vcpu-ubuntu-2204"
   116  		configFile := fmt.Sprintf(`
   117  			{"packagesPath": ".", "packages": [{"name": "%s", "exclude": ["network/alsp"], "subpackages": [{"name": "%s", "runner": "%s"}]}]}`,
   118  			topLevelPkgName, subPkg1, subPkg1Runner)
   119  		allPackges := goPackageFixture(
   120  			"network",
   121  			"network/alsp",
   122  			"network/cache",
   123  			"network/channels",
   124  			"network/p2p/node",
   125  			"network/p2p/node/internal",
   126  			"module",
   127  			"module/chunks/chunky",
   128  			"crypto/hash",
   129  			"crypto/random",
   130  			"crypto/hash/ecc",
   131  			"model/bootstrap",
   132  			"model/bootstrap/info",
   133  			"model",
   134  		)
   135  		cfg := loadPackagesConfig(configFile)
   136  		matrices := buildTestMatrices(cfg, func(dir string) []*packages.Package {
   137  			return allPackges
   138  		})
   139  		require.Len(t, matrices, 2)
   140  		for _, matrix := range matrices {
   141  			switch matrix.Name {
   142  			case topLevelPkgName:
   143  				require.Equal(t, defaultCIRunner, matrix.Runner)
   144  				require.Equal(t, fmt.Sprintf("%s %s %s ", allPackges[0].PkgPath, allPackges[2].PkgPath, allPackges[3].PkgPath), matrix.Packages)
   145  			case subPkg1:
   146  				require.Equal(t, subPkg1Runner, matrix.Runner)
   147  				require.Equal(t, fmt.Sprintf("%s %s ", allPackges[4].PkgPath, allPackges[5].PkgPath), matrix.Packages)
   148  			default:
   149  				require.Fail(t, fmt.Sprintf("unexpected matrix name: %s", matrix.Name))
   150  			}
   151  		}
   152  	})
   153  }
   154  
   155  func goPackageFixture(pkgs ...string) []*packages.Package {
   156  	goPkgs := make([]*packages.Package, len(pkgs))
   157  	for i, pkg := range pkgs {
   158  		goPkgs[i] = &packages.Package{PkgPath: fullGoPackagePath(pkg)}
   159  	}
   160  	return goPkgs
   161  }