github.com/sdboyer/gps@v0.16.3/rootdata_test.go (about)

     1  package gps
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestRootdataExternalImports(t *testing.T) {
     9  	fix := basicFixtures["shared dependency with overlapping constraints"]
    10  
    11  	params := SolveParameters{
    12  		RootDir:         string(fix.ds[0].n),
    13  		RootPackageTree: fix.rootTree(),
    14  		Manifest:        fix.rootmanifest(),
    15  		ProjectAnalyzer: naiveAnalyzer{},
    16  	}
    17  
    18  	is, err := Prepare(params, newdepspecSM(fix.ds, nil))
    19  	if err != nil {
    20  		t.Fatalf("Unexpected error while prepping solver: %s", err)
    21  	}
    22  	rd := is.(*solver).rd
    23  
    24  	want := []string{"a", "b"}
    25  	got := rd.externalImportList()
    26  	if !reflect.DeepEqual(want, got) {
    27  		t.Errorf("Unexpected return from rootdata.externalImportList:\n\t(GOT): %s\n\t(WNT): %s", got, want)
    28  	}
    29  
    30  	// Add a require
    31  	rd.req["c"] = true
    32  
    33  	want = []string{"a", "b", "c"}
    34  	got = rd.externalImportList()
    35  	if !reflect.DeepEqual(want, got) {
    36  		t.Errorf("Unexpected return from rootdata.externalImportList:\n\t(GOT): %s\n\t(WNT): %s", got, want)
    37  	}
    38  
    39  	// Add same path as import
    40  	poe := rd.rpt.Packages["root"]
    41  	poe.P.Imports = []string{"a", "b", "c"}
    42  	rd.rpt.Packages["root"] = poe
    43  
    44  	// should still be the same
    45  	got = rd.externalImportList()
    46  	if !reflect.DeepEqual(want, got) {
    47  		t.Errorf("Unexpected return from rootdata.externalImportList:\n\t(GOT): %s\n\t(WNT): %s", got, want)
    48  	}
    49  
    50  	// Add an ignore, but not on the required path (Prepare makes that
    51  	// combination impossible)
    52  
    53  	rd.ig["b"] = true
    54  	want = []string{"a", "c"}
    55  	got = rd.externalImportList()
    56  	if !reflect.DeepEqual(want, got) {
    57  		t.Errorf("Unexpected return from rootdata.externalImportList:\n\t(GOT): %s\n\t(WNT): %s", got, want)
    58  	}
    59  }
    60  
    61  func TestGetApplicableConstraints(t *testing.T) {
    62  	fix := basicFixtures["shared dependency with overlapping constraints"]
    63  
    64  	params := SolveParameters{
    65  		RootDir:         string(fix.ds[0].n),
    66  		RootPackageTree: fix.rootTree(),
    67  		Manifest:        fix.rootmanifest(),
    68  		ProjectAnalyzer: naiveAnalyzer{},
    69  	}
    70  
    71  	is, err := Prepare(params, newdepspecSM(fix.ds, nil))
    72  	if err != nil {
    73  		t.Fatalf("Unexpected error while prepping solver: %s", err)
    74  	}
    75  	rd := is.(*solver).rd
    76  
    77  	table := []struct {
    78  		name   string
    79  		mut    func()
    80  		result []workingConstraint
    81  	}{
    82  		{
    83  			name: "base case, two constraints",
    84  			mut:  func() {},
    85  			result: []workingConstraint{
    86  				{
    87  					Ident:      mkPI("a"),
    88  					Constraint: mkSVC("1.0.0"),
    89  				},
    90  				{
    91  					Ident:      mkPI("b"),
    92  					Constraint: mkSVC("1.0.0"),
    93  				},
    94  			},
    95  		},
    96  		{
    97  			name: "with unconstrained require",
    98  			mut: func() {
    99  				// No constraint means it doesn't show up
   100  				rd.req["c"] = true
   101  			},
   102  			result: []workingConstraint{
   103  				{
   104  					Ident:      mkPI("a"),
   105  					Constraint: mkSVC("1.0.0"),
   106  				},
   107  				{
   108  					Ident:      mkPI("b"),
   109  					Constraint: mkSVC("1.0.0"),
   110  				},
   111  			},
   112  		},
   113  		{
   114  			name: "with unconstrained import",
   115  			mut: func() {
   116  				// Again, no constraint means it doesn't show up
   117  				poe := rd.rpt.Packages["root"]
   118  				poe.P.Imports = []string{"a", "b", "d"}
   119  				rd.rpt.Packages["root"] = poe
   120  			},
   121  			result: []workingConstraint{
   122  				{
   123  					Ident:      mkPI("a"),
   124  					Constraint: mkSVC("1.0.0"),
   125  				},
   126  				{
   127  					Ident:      mkPI("b"),
   128  					Constraint: mkSVC("1.0.0"),
   129  				},
   130  			},
   131  		},
   132  		{
   133  			name: "constraint on required",
   134  			mut: func() {
   135  				rd.rm.Deps["c"] = ProjectProperties{
   136  					Constraint: NewBranch("foo"),
   137  				}
   138  			},
   139  			result: []workingConstraint{
   140  				{
   141  					Ident:      mkPI("a"),
   142  					Constraint: mkSVC("1.0.0"),
   143  				},
   144  				{
   145  					Ident:      mkPI("b"),
   146  					Constraint: mkSVC("1.0.0"),
   147  				},
   148  				{
   149  					Ident:      mkPI("c"),
   150  					Constraint: NewBranch("foo"),
   151  				},
   152  			},
   153  		},
   154  		{
   155  			name: "override on imported",
   156  			mut: func() {
   157  				rd.ovr["d"] = ProjectProperties{
   158  					Constraint: NewBranch("bar"),
   159  				}
   160  			},
   161  			result: []workingConstraint{
   162  				{
   163  					Ident:      mkPI("a"),
   164  					Constraint: mkSVC("1.0.0"),
   165  				},
   166  				{
   167  					Ident:      mkPI("b"),
   168  					Constraint: mkSVC("1.0.0"),
   169  				},
   170  				{
   171  					Ident:      mkPI("c"),
   172  					Constraint: NewBranch("foo"),
   173  				},
   174  				{
   175  					Ident:           mkPI("d"),
   176  					Constraint:      NewBranch("bar"),
   177  					overrConstraint: true,
   178  				},
   179  			},
   180  		},
   181  		{
   182  			// It is certainly the simplest and most rule-abiding solution to
   183  			// drop the constraint in this case, but is there a chance it would
   184  			// violate the principle of least surprise?
   185  			name: "ignore imported and overridden pkg",
   186  			mut: func() {
   187  				rd.ig["d"] = true
   188  			},
   189  			result: []workingConstraint{
   190  				{
   191  					Ident:      mkPI("a"),
   192  					Constraint: mkSVC("1.0.0"),
   193  				},
   194  				{
   195  					Ident:      mkPI("b"),
   196  					Constraint: mkSVC("1.0.0"),
   197  				},
   198  				{
   199  					Ident:      mkPI("c"),
   200  					Constraint: NewBranch("foo"),
   201  				},
   202  			},
   203  		},
   204  	}
   205  
   206  	for _, fix := range table {
   207  		t.Run(fix.name, func(t *testing.T) {
   208  			fix.mut()
   209  
   210  			got := rd.getApplicableConstraints()
   211  			if !reflect.DeepEqual(fix.result, got) {
   212  				t.Errorf("unexpected applicable constraint set:\n\t(GOT): %+v\n\t(WNT): %+v", got, fix.result)
   213  			}
   214  		})
   215  	}
   216  }