Skip to content

Commit ae4fe08

Browse files
committed
Fix some test failures introduced by subclasses
1 parent 7eb344e commit ae4fe08

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

npm_and_yarn/lib/dependabot/npm_and_yarn/dependency_grapher.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@ def parsed_package_json
110110

111111
sig { returns(T::Hash[Symbol, T.nilable(Dependabot::DependencyFile)]) }
112112
def lockfiles_hash
113-
{
114-
npm: dependency_files.find { |f| f.name.end_with?(NpmPackageManager::LOCKFILE_NAME) },
115-
yarn: dependency_files.find { |f| f.name.end_with?(YarnPackageManager::LOCKFILE_NAME) },
116-
pnpm: dependency_files.find { |f| f.name.end_with?(PNPMPackageManager::LOCKFILE_NAME) }
117-
}
113+
@lockfiles_hash ||= T.let(
114+
{
115+
npm: dependency_files.find { |f| f.name.end_with?(NpmPackageManager::LOCKFILE_NAME) },
116+
yarn: dependency_files.find { |f| f.name.end_with?(YarnPackageManager::LOCKFILE_NAME) },
117+
pnpm: dependency_files.find { |f| f.name.end_with?(PNPMPackageManager::LOCKFILE_NAME) }
118+
},
119+
T.nilable(T::Hash[Symbol, T.nilable(Dependabot::DependencyFile)])
120+
)
118121
end
119122

120123
sig { returns(String) }
@@ -161,6 +164,7 @@ def inject_ephemeral_lockfile(ephemeral_lockfile)
161164

162165
# Clear our cached lockfile reference so it picks up the new one
163166
remove_instance_variable(:@lockfile) if instance_variable_defined?(:@lockfile)
167+
remove_instance_variable(:@lockfiles_hash) if instance_variable_defined?(:@lockfiles_hash)
164168

165169
# Clear the FileParser's memoized lockfile references so it will
166170
# find the newly injected lockfile when parse is called
@@ -237,6 +241,11 @@ def package_relationships
237241
fetch_package_relationships,
238242
T.nilable(T::Hash[String, T::Array[String]])
239243
)
244+
rescue StandardError => e
245+
errored_fetching_subdependencies!
246+
@subdependency_error = T.let(e, T.nilable(StandardError))
247+
Dependabot.logger.error("Error fetching subdependencies: #{e.message}")
248+
@package_relationships = {}
240249
end
241250

242251
sig { returns(T::Hash[String, T::Array[String]]) }

npm_and_yarn/spec/dependabot/npm_and_yarn/dependency_grapher_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@
872872
corrupt_lockfile = Dependabot::DependencyFile.new(
873873
name: "package-lock.json", content: "not valid json {{{", directory: "/"
874874
)
875-
grapher.instance_variable_set(:@npm_lockfile, corrupt_lockfile)
875+
grapher.send(:lockfiles_hash)[:npm] = corrupt_lockfile
876876
end
877877

878878
it "sets the errored_fetching_subdependencies flag" do
@@ -907,7 +907,7 @@
907907
corrupt_lockfile = Dependabot::DependencyFile.new(
908908
name: "yarn.lock", content: "\x00\x01 invalid", directory: "/"
909909
)
910-
grapher.instance_variable_set(:@yarn_lockfile, corrupt_lockfile)
910+
grapher.send(:lockfiles_hash)[:yarn] = corrupt_lockfile
911911
end
912912

913913
it "sets the errored_fetching_subdependencies flag" do
@@ -942,7 +942,7 @@
942942
corrupt_lockfile = Dependabot::DependencyFile.new(
943943
name: "pnpm-lock.yaml", content: ": :\n invalid: [yaml", directory: "/"
944944
)
945-
grapher.instance_variable_set(:@pnpm_lockfile, corrupt_lockfile)
945+
grapher.send(:lockfiles_hash)[:pnpm] = corrupt_lockfile
946946
end
947947

948948
it "sets the errored_fetching_subdependencies flag" do

0 commit comments

Comments
 (0)