|
| 1 | +package pubgrub |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// TestComplexRubyGemsScenario tests a more realistic scenario with multiple |
| 8 | +// packages that all transitively depend on shared dependencies. |
| 9 | +// |
| 10 | +// This simulates what happens in a real Rails project where many gems |
| 11 | +// depend on common utilities like rubyzip, and wrong version choices |
| 12 | +// early in the search can lead to dead ends. |
| 13 | +// |
| 14 | +// Package structure: |
| 15 | +// - root → [roo, rubyXL, caxlsx, another_gem] |
| 16 | +// - All four packages use rubyzip with different constraints |
| 17 | +// - PubGrub must choose rubyzip version that works for ALL of them |
| 18 | +// |
| 19 | +// This test will help identify if PubGrub's unit propagation and |
| 20 | +// conflict learning are working efficiently. |
| 21 | +func TestComplexRubyGemsScenario(t *testing.T) { |
| 22 | + source := NewMapSource() |
| 23 | + |
| 24 | + // Add rubyzip versions |
| 25 | + source.Add("rubyzip", "1.3.0", nil) |
| 26 | + source.Add("rubyzip", "2.3.0", nil) |
| 27 | + source.Add("rubyzip", "2.4.0", nil) |
| 28 | + source.Add("rubyzip", "2.4.1", nil) |
| 29 | + source.Add("rubyzip", "3.0.0", nil) |
| 30 | + source.Add("rubyzip", "3.1.0", nil) |
| 31 | + |
| 32 | + // Add roo versions - note that OLD versions require rubyzip >= 3.0 |
| 33 | + source.Add("roo", "2.1.0", []Dependency{ |
| 34 | + {Name: "rubyzip", Constraint: ">= 3.0.0, < 4.0.0"}, |
| 35 | + }) |
| 36 | + source.Add("roo", "2.5.0", []Dependency{ |
| 37 | + {Name: "rubyzip", Constraint: ">= 3.0.0, < 4.0.0"}, |
| 38 | + }) |
| 39 | + source.Add("roo", "2.9.0", []Dependency{ |
| 40 | + {Name: "rubyzip", Constraint: ">= 3.0.0, < 4.0.0"}, |
| 41 | + }) |
| 42 | + // Only 2.10.1 and 3.0.0 are compatible with rubyzip 2.x |
| 43 | + source.Add("roo", "2.10.1", []Dependency{ |
| 44 | + {Name: "rubyzip", Constraint: ">= 1.3.0, < 3.0.0"}, |
| 45 | + }) |
| 46 | + source.Add("roo", "3.0.0", []Dependency{ |
| 47 | + {Name: "rubyzip", Constraint: ">= 3.0.0, < 4.0.0"}, |
| 48 | + }) |
| 49 | + |
| 50 | + // Add rubyXL versions - all require rubyzip ~> 2.4 |
| 51 | + source.Add("rubyXL", "3.4.14", []Dependency{ |
| 52 | + {Name: "rubyzip", Constraint: ">= 2.4.0, < 3.0.0"}, |
| 53 | + }) |
| 54 | + source.Add("rubyXL", "3.4.25", []Dependency{ |
| 55 | + {Name: "rubyzip", Constraint: ">= 2.4.0, < 3.0.0"}, |
| 56 | + }) |
| 57 | + source.Add("rubyXL", "3.4.34", []Dependency{ |
| 58 | + {Name: "rubyzip", Constraint: ">= 2.4.0, < 3.0.0"}, |
| 59 | + }) |
| 60 | + |
| 61 | + // Add caxlsx which also depends on rubyzip |
| 62 | + source.Add("caxlsx", "3.3.0", []Dependency{ |
| 63 | + {Name: "rubyzip", Constraint: ">= 1.6.0, < 3.0.0"}, |
| 64 | + }) |
| 65 | + source.Add("caxlsx", "4.0.0", []Dependency{ |
| 66 | + {Name: "rubyzip", Constraint: ">= 2.3.0, < 4.0.0"}, |
| 67 | + }) |
| 68 | + |
| 69 | + // Add another gem that prefers older rubyzip |
| 70 | + source.Add("zip_tricks", "5.6.0", []Dependency{ |
| 71 | + {Name: "rubyzip", Constraint: ">= 1.3.0, < 3.0.0"}, |
| 72 | + }) |
| 73 | + |
| 74 | + // Root depends on all four packages |
| 75 | + rootSource := NewRootSource() |
| 76 | + rootSource.AddPackage(MakeName("roo"), NewAnyVersionCondition()) |
| 77 | + rootSource.AddPackage(MakeName("rubyXL"), NewAnyVersionCondition()) |
| 78 | + rootSource.AddPackage(MakeName("caxlsx"), NewAnyVersionCondition()) |
| 79 | + rootSource.AddPackage(MakeName("zip_tricks"), NewAnyVersionCondition()) |
| 80 | + |
| 81 | + // Create solver |
| 82 | + solver := NewSolver(rootSource, source) |
| 83 | + |
| 84 | + // Solve |
| 85 | + solution, err := solver.Solve(rootSource.Term()) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Expected solution but got error: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + // Verify solution |
| 91 | + solutionMap := make(map[string]string) |
| 92 | + for _, pkg := range solution { |
| 93 | + if pkg.Name.Value() != "$$root" { |
| 94 | + solutionMap[pkg.Name.Value()] = pkg.Version.String() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // The only valid solution should use rubyzip 2.4.x |
| 99 | + // because that's the intersection of all constraints: |
| 100 | + // - roo 2.10.1: >= 1.3.0, < 3.0.0 |
| 101 | + // - rubyXL: >= 2.4.0, < 3.0.0 |
| 102 | + // - caxlsx: depends on which version, but should work with 2.4.x |
| 103 | + // - zip_tricks: >= 1.3.0, < 3.0.0 |
| 104 | + // |
| 105 | + // Intersection: >= 2.4.0, < 3.0.0 → rubyzip 2.4.1 |
| 106 | + |
| 107 | + if solutionMap["roo"] != "2.10.1" { |
| 108 | + t.Errorf("Expected roo 2.10.1, got %s", solutionMap["roo"]) |
| 109 | + } |
| 110 | + if solutionMap["rubyzip"] < "2.4.0" || solutionMap["rubyzip"] >= "3.0.0" { |
| 111 | + t.Errorf("Expected rubyzip in [2.4.0, 3.0.0), got %s", solutionMap["rubyzip"]) |
| 112 | + } |
| 113 | + |
| 114 | + // Print solution for debugging |
| 115 | + t.Logf("Solution found:") |
| 116 | + for name, version := range solutionMap { |
| 117 | + t.Logf(" %s = %s", name, version) |
| 118 | + } |
| 119 | +} |
0 commit comments