Skip to content

Commit 6b4b62e

Browse files
committed
Refine GoModGraph handling and improve reconciliation logic in GoModUpdater
1 parent 7e9a9fa commit 6b4b62e

4 files changed

Lines changed: 62 additions & 51 deletions

File tree

go_modules/lib/dependabot/go_modules/file_updater/go_mod_graph.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def parse_graph(output)
7474

7575
output.each_line do |line|
7676
line.strip.split(/\s+/).each do |entry|
77-
result.add(entry) unless entry.empty?
77+
# Only include versioned entries (module@version), skip
78+
# the root module which has no @version suffix.
79+
result.add(entry) if entry.include?("@")
7880
end
7981
end
8082

@@ -91,14 +93,14 @@ def group_by_path(entries)
9193
result = T.let(Hash.new { |h, k| h[k] = Set.new }, T::Hash[String, T::Set[String]])
9294

9395
entries.each do |entry|
94-
at_index = T.must(entry).rindex("@")
96+
at_index = entry.rindex("@")
9597
next unless at_index
9698

97-
path = T.must(entry)[0...at_index]
98-
version = T.must(entry)[(at_index + 1)..]
99+
path = entry[0...at_index]
100+
version = entry[(at_index + 1)..]
99101
next unless path && version && !path.empty?
100102

101-
result[path].add(version)
103+
T.must(result[path]).add(version)
102104
end
103105

104106
result

go_modules/lib/dependabot/go_modules/file_updater/go_mod_updater.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,13 @@ def update_files
234234
# to the update — Go tooling can over-prune /go.mod checksums.
235235
if original_go_sum && updated_go_sum
236236
graph_after = GoModGraph.capture
237-
updated_go_sum = reconcile_go_sum(
238-
original_go_sum,
239-
updated_go_sum,
240-
graph_before.changed_modules(graph_after)
241-
)
237+
unless graph_before.empty? || graph_after.empty?
238+
updated_go_sum = reconcile_go_sum(
239+
original_go_sum,
240+
updated_go_sum,
241+
graph_before.changed_modules(graph_after)
242+
)
243+
end
242244
end
243245

244246
{ go_mod: updated_go_mod, go_sum: updated_go_sum }
@@ -432,8 +434,6 @@ def in_repo_path(&block)
432434
).returns(String)
433435
end
434436
def reconcile_go_sum(original, updated, changed_modules)
435-
return updated if changed_modules.empty?
436-
437437
updated_lines = updated.lines(chomp: true).reject(&:empty?)
438438
updated_set = updated_lines.to_set
439439

go_modules/spec/dependabot/go_modules/file_updater/go_mod_graph_spec.rb

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@
4747

4848
describe "#changed_modules" do
4949
it "detects modules with changed versions" do
50-
before_graph = described_class.new(modules: Set[
51-
"github.com/onsi/gomega@v1.39.0",
52-
"google.golang.org/grpc@v1.81.1",
53-
"gonum.org/v1/gonum@v0.17.0"
54-
])
55-
56-
after_graph = described_class.new(modules: Set[
57-
"github.com/onsi/gomega@v1.40.0",
58-
"google.golang.org/grpc@v1.81.1",
59-
"gonum.org/v1/gonum@v0.17.0"
60-
])
50+
before_graph = described_class.new(
51+
modules: Set[
52+
"github.com/onsi/gomega@v1.39.0",
53+
"google.golang.org/grpc@v1.81.1",
54+
"gonum.org/v1/gonum@v0.17.0"
55+
]
56+
)
57+
58+
after_graph = described_class.new(
59+
modules: Set[
60+
"github.com/onsi/gomega@v1.40.0",
61+
"google.golang.org/grpc@v1.81.1",
62+
"gonum.org/v1/gonum@v0.17.0"
63+
]
64+
)
6165

6266
changed = before_graph.changed_modules(after_graph)
6367
expect(changed).to include("github.com/onsi/gomega")
@@ -66,40 +70,46 @@
6670
end
6771

6872
it "detects added modules" do
69-
before_graph = described_class.new(modules: Set[
70-
"github.com/onsi/gomega@v1.39.0"
71-
])
73+
before_graph = described_class.new(
74+
modules: Set["github.com/onsi/gomega@v1.39.0"]
75+
)
7276

73-
after_graph = described_class.new(modules: Set[
74-
"github.com/onsi/gomega@v1.39.0",
75-
"github.com/kr/pretty@v0.3.1"
76-
])
77+
after_graph = described_class.new(
78+
modules: Set[
79+
"github.com/onsi/gomega@v1.39.0",
80+
"github.com/kr/pretty@v0.3.1"
81+
]
82+
)
7783

7884
changed = before_graph.changed_modules(after_graph)
7985
expect(changed).to include("github.com/kr/pretty")
8086
expect(changed).not_to include("github.com/onsi/gomega")
8187
end
8288

8389
it "detects removed modules" do
84-
before_graph = described_class.new(modules: Set[
85-
"github.com/onsi/gomega@v1.39.0",
86-
"github.com/old/dep@v1.0.0"
87-
])
90+
before_graph = described_class.new(
91+
modules: Set[
92+
"github.com/onsi/gomega@v1.39.0",
93+
"github.com/old/dep@v1.0.0"
94+
]
95+
)
8896

89-
after_graph = described_class.new(modules: Set[
90-
"github.com/onsi/gomega@v1.39.0"
91-
])
97+
after_graph = described_class.new(
98+
modules: Set["github.com/onsi/gomega@v1.39.0"]
99+
)
92100

93101
changed = before_graph.changed_modules(after_graph)
94102
expect(changed).to include("github.com/old/dep")
95103
expect(changed).not_to include("github.com/onsi/gomega")
96104
end
97105

98106
it "returns empty set when graphs are identical" do
99-
graph = described_class.new(modules: Set[
100-
"github.com/onsi/gomega@v1.39.0",
101-
"gonum.org/v1/gonum@v0.17.0"
102-
])
107+
graph = described_class.new(
108+
modules: Set[
109+
"github.com/onsi/gomega@v1.39.0",
110+
"gonum.org/v1/gonum@v0.17.0"
111+
]
112+
)
103113

104114
expect(graph.changed_modules(graph)).to be_empty
105115
end

go_modules/spec/dependabot/go_modules/file_updater/go_mod_updater_spec.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,17 @@ module github.com/dependabot/vgotest
290290
"#{gonum_zip_line}\n#{gonum_gomod_line}\n"
291291
pruned_sum = original_sum.lines.reject { |l| l.chomp == gonum_gomod_line }.join
292292

293+
read_count = 0
293294
allow(File).to receive(:read).and_call_original
294-
allow(File).to receive(:read).with("go.sum").and_return(original_sum, pruned_sum)
295+
allow(File).to receive(:read).with("go.sum") do
296+
read_count += 1
297+
read_count == 1 ? original_sum : pruned_sum
298+
end
295299

296-
# Graph shows gonum unchanged before and after
297-
graph_with_gonum = Dependabot::GoModules::FileUpdater::GoModGraph.new(
300+
# Graph: rsc.io/quote updated, gonum unchanged
301+
graph_before = Dependabot::GoModules::FileUpdater::GoModGraph.new(
298302
modules: Set[
299-
"rsc.io/quote@v1.5.2",
303+
"rsc.io/quote@v1.4.0",
300304
"gonum.org/v1/gonum@v0.16.0"
301305
]
302306
)
@@ -309,7 +313,7 @@ module github.com/dependabot/vgotest
309313

310314
allow(Dependabot::GoModules::FileUpdater::GoModGraph)
311315
.to receive(:capture)
312-
.and_return(graph_with_gonum, graph_after)
316+
.and_return(graph_before, graph_after)
313317
end
314318

315319
it "restores the unrelated go.mod checksum line" do
@@ -319,11 +323,6 @@ module github.com/dependabot/vgotest
319323

320324
context "when a module version changes legitimately" do
321325
before do
322-
original_sum = fixture("projects", project_name, "go.sum")
323-
324-
allow(File).to receive(:read).and_call_original
325-
allow(File).to receive(:read).with("go.sum").and_return(original_sum)
326-
327326
# Graph shows rsc.io/quote changed version (it's being updated)
328327
graph_before = Dependabot::GoModules::FileUpdater::GoModGraph.new(
329328
modules: Set["rsc.io/quote@v1.4.0"]

0 commit comments

Comments
 (0)