-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.rb
160 lines (131 loc) · 4.21 KB
/
resolver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "rubygems"
require "cutest"
def gemspec(name, version, dependencies = {})
Gem::Specification.new do |spec|
spec.name = name
spec.version = version
spec.summary = name
dependencies.each do |dep|
spec.add_dependency(*dep)
end
end
end
test "gemspec" do
assert_equal "1", gemspec("x", 1).version.to_s
assert_equal ">= 1", gemspec("a", 1, "x" => ">= 1").runtime_dependencies.first.requirement.to_s
assert_equal "= 1", gemspec("a", 1, "x" => "1").runtime_dependencies.first.requirement.to_s
end
class Resolver
attr :versions
attr :runtime_dependees
attr :gems_to_remove
attr :gems_missing
def initialize(gems_list)
@versions = Hash.new { |hash, key| hash[key] = [] }
@runtime_dependees = Hash.new { |hash, key| hash[key] = [] }
@gems_to_remove = []
@gems_missing = []
populate_lists(gems_list)
end
def resolve!
remove = []
runtime_dependees.each do |name, dependees|
keep, _remove = versions[name].sort.partition do |gem|
satisfies_all_dependees?(gem, dependees)
end
remove.concat(_remove)
remove.concat(keep[1..-1]) if keep[1..-1]
if keep.empty?
deps = collect_missing_versions(name, dependees)
@gems_missing << [name, deps]
end
end
@gems_to_remove = remove
end
private
def collect_missing_versions(name, dependees)
dependees.map do |dependee|
dependee.runtime_dependencies.detect do |dep|
dep.name == name
end.requirement.to_s
end
end
def satisfies_all_dependees?(gem, dependees)
dependees.all? do |dependee|
dep = dependee.runtime_dependencies.detect do |dep|
dep.name == gem.name
end
dep && dep.match?(gem.name, gem.version)
end
end
def populate_lists(gems_list)
gems_list.each do |gem|
@versions[gem.name] << gem
gem.runtime_dependencies.each do |dep|
@runtime_dependees[dep.name] << gem
end
end
end
end
setup do
[].tap do |gems|
gems << gemspec("a", 1, "x" => ">= 1")
gems << gemspec("b", 1, "x" => "1")
gems << gemspec("x", 1)
gems << gemspec("x", 2)
end
end
test "versions" do |gems|
resolver = Resolver.new(gems)
assert_equal "1", resolver.versions["a"].first.version.to_s
assert_equal "1", resolver.versions["b"].first.version.to_s
assert_equal "1", resolver.versions["x"].first.version.to_s
assert_equal "2", resolver.versions["x"].last.version.to_s
end
test "runtime_dependees" do |gems|
resolver = Resolver.new(gems)
assert_equal [], resolver.runtime_dependees["a"]
assert_equal [], resolver.runtime_dependees["b"]
assert_equal "a", resolver.runtime_dependees["x"].first.name
assert_equal "1", resolver.runtime_dependees["x"].first.version.to_s
assert_equal "b", resolver.runtime_dependees["x"].last.name
assert_equal "1", resolver.runtime_dependees["x"].last.version.to_s
end
test "simple gems to remove" do |gems|
resolver = Resolver.new(gems)
resolver.resolve!
assert_equal [gemspec("x", "2")], resolver.gems_to_remove
end
setup do
[].tap do |gems|
gems << gemspec("a", "1.0.0", "b" => ">= 0")
gems << gemspec("b", "1.0.0", "x" => "~> 1.0.0")
gems << gemspec("x", "1.0.0")
gems << gemspec("x", "1.1.0")
gems << gemspec("x", "1.2.0")
end
end
test "gems to remove" do |gems|
resolver = Resolver.new(gems)
resolver.resolve!
assert resolver.gems_to_remove.include?(gemspec("x", "1.1.0"))
assert resolver.gems_to_remove.include?(gemspec("x", "1.2.0"))
assert !resolver.gems_to_remove.include?(gemspec("x", "1.0.0"))
assert !resolver.gems_to_remove.include?(gemspec("a", "1.0.0", "b" => ">= 0"))
assert !resolver.gems_to_remove.include?(gemspec("b", "1.0.0", "x" => "~> 1.0.0"))
end
setup do
[].tap do |gems|
gems << gemspec("a", "1", "x" => "< 3") # 02/02/2010
gems << gemspec("b", "1", "x" => ">= 2") # 04/04/2010
gems << gemspec("x", "1") # 01/01/2010
gems << gemspec("x", "3") # 03/03/2010
end
end
test "missing gems" do |gems|
resolver = Resolver.new(gems)
resolver.resolve!
assert resolver.gems_to_remove.include?(gemspec("x", "1"))
assert resolver.gems_to_remove.include?(gemspec("x", "3"))
assert resolver.gems_missing.include?(["x", ["< 3", ">= 2"]])
end