github.com/replit/upm@v0.0.0-20240423230255-9ce4fc3ea24c/test-suite/List_test.go (about)

     1  package testSuite
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/replit/upm/internal/api"
     7  	testUtils "github.com/replit/upm/test-suite/utils"
     8  )
     9  
    10  func TestList(t *testing.T) {
    11  	for _, bt := range languageBackends {
    12  		bt.Start(t)
    13  
    14  		var templatesToPackages map[string][]string
    15  		switch bt.Backend.Name {
    16  		case "nodejs-npm":
    17  			fallthrough
    18  		case "nodejs-pnpm":
    19  			fallthrough
    20  		case "nodejs-yarn":
    21  			fallthrough
    22  		case "bun":
    23  			templatesToPackages = map[string][]string{
    24  				"no-deps": {},
    25  				"one-dep": {"express"},
    26  				"many-deps": {
    27  					"enquirer",
    28  					"eslint",
    29  					"express",
    30  					"svelte",
    31  					"vite",
    32  					"@codemirror/state",
    33  				},
    34  			}
    35  
    36  		case "python3-poetry":
    37  			templatesToPackages = map[string][]string{
    38  				"no-deps": {},
    39  				"one-dep": {"django"},
    40  				"many-deps": {
    41  					"django",
    42  					"boatman",
    43  					"ws4py",
    44  				},
    45  			}
    46  
    47  		case "python3-pip":
    48  			templatesToPackages = map[string][]string{
    49  				"no-deps": {},
    50  				"one-dep": {"django"},
    51  				"many-deps": {
    52  					"django",
    53  					"boatman",
    54  					"ws4py",
    55  				},
    56  			}
    57  		default:
    58  			t.Run(bt.Backend.Name, func(t *testing.T) {
    59  				t.Skip("no test")
    60  			})
    61  			continue
    62  		}
    63  
    64  		bt.Subtest(bt.Backend.Name, func(bt testUtils.BackendT) {
    65  			doList(bt, templatesToPackages)
    66  		})
    67  	}
    68  }
    69  
    70  func doList(bt testUtils.BackendT, templatesToPackages map[string][]string) {
    71  	for tmpl, expectPkgs := range templatesToPackages {
    72  		template := bt.Backend.Name + "/" + tmpl + "/"
    73  
    74  		bt.Subtest(tmpl, func(bt testUtils.BackendT) {
    75  			bt.AddTestFile(template+bt.Backend.Specfile, bt.Backend.Specfile)
    76  			if tmpl != "no-deps" && bt.Backend.QuirksIsReproducible() {
    77  				bt.AddTestFile(template+bt.Backend.Lockfile, bt.Backend.Lockfile)
    78  			}
    79  
    80  			specs := bt.UpmListSpecFile()
    81  			if len(specs) != len(expectPkgs) {
    82  				bt.Fail("Expected %v packages, got %v", len(expectPkgs), len(specs))
    83  			}
    84  
    85  			var locks []api.PkgInfo
    86  			if bt.Backend.QuirksIsReproducible() {
    87  				locks = bt.UpmListLockFile()
    88  				if len(locks) < len(expectPkgs) {
    89  					bt.Fail("Expected %v packages, got %v", len(expectPkgs), len(locks))
    90  				}
    91  			}
    92  
    93  			for _, pkg := range expectPkgs {
    94  				found := false
    95  				for _, spec := range specs {
    96  					if pkg == spec.Name {
    97  						found = true
    98  						break
    99  					}
   100  				}
   101  
   102  				if !found {
   103  					bt.Fail("package %v not found in spec list", pkg)
   104  				}
   105  
   106  				if bt.Backend.QuirksIsReproducible() {
   107  					found = false
   108  					for _, lock := range locks {
   109  						if pkg == lock.Name {
   110  							found = true
   111  							break
   112  						}
   113  					}
   114  
   115  					if !found {
   116  						bt.Fail("package %v not found in lock list", pkg)
   117  					}
   118  				}
   119  			}
   120  		})
   121  	}
   122  }