Skip to content

Commit

Permalink
Adding exception handlers and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin-sandhu committed Aug 20, 2024
1 parent 02f7d23 commit 87b447f
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def updated_pnpm_lock_content(pnpm_lock)
PLATFORM_VERSION_REQUIREMENT = /wanted {(?<supported_ver>.*)} \(current: (?<detected_ver>.*)\)/
PLATFORM_PACAKGE_MANAGER = "pnpm"

ERR_PNPM_BROKEN_METADATA_JSON = /ERR_PNPM_BROKEN_METADATA_JSON/
ERR_PNPM_PEER_DEP_ISSUES = /ERR_PNPM_PEER_DEP_ISSUES/
ERR_PNPM_WORKSPACE_PKG_NOT_FOUND = /ERR_PNPM_WORKSPACE_PKG_NOT_FOUND/

def run_pnpm_update(pnpm_lock:)
SharedHelpers.in_a_temporary_repo_directory(base_dir, repo_contents_path) do
File.write(".npmrc", npmrc_content(pnpm_lock))
Expand Down Expand Up @@ -151,6 +155,22 @@ def handle_pnpm_lock_updater_error(error, pnpm_lock)

raise_unsupported_engine_error(error_message, pnpm_lock) if error_message.match?(ERR_PNPM_UNSUPPORTED_ENGINE)

if error_message.match?(ERR_PNPM_WORKSPACE_PKG_NOT_FOUND)
dependency_names = dependencies.map(&:name).join(", ")

msg = "No package named \"#{dependency_names}\" present in workspace."
Dependabot.logger.warn(error_message)
raise Dependabot::DependencyFileNotResolvable, msg
end

if error_message.match?(ERR_PNPM_BROKEN_METADATA_JSON)
msg = "Error (ERR_PNPM_BROKEN_METADATA_JSON) while resolving \"pnpm-lock.yaml\" file."
Dependabot.logger.warn(error_message)
raise Dependabot::DependencyFileNotResolvable, msg
end

raise_peer_deps_issues(error_message, pnpm_lock) if error_message.match?(ERR_PNPM_PEER_DEP_ISSUES)

if error_message.match?(ERR_PNPM_UNSUPPORTED_PLATFORM)
raise_unsupported_platform_error(error_message,
pnpm_lock)
Expand Down Expand Up @@ -229,6 +249,13 @@ def raise_unsupported_platform_error(error_message, _pnpm_lock)
raise Dependabot::ToolVersionNotSupported.new(PLATFORM_PACAKGE_MANAGER, supported_version, detected_version)
end

def raise_peer_deps_issues(error_message, _pnpm_lock)
dependency_names = dependencies.map(&:name).join(", ")
msg = "Unmet peer dependencies error while updating #{dependency_names}."
Dependabot.logger.warn("#{msg} #{error_message.partition(ERR_PNPM_PEER_DEP_ISSUES).last}")
raise Dependabot::DependencyFileNotResolvable, msg
end

def npmrc_content(pnpm_lock)
NpmrcBuilder.new(
credentials: credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,64 @@
end
end

context "with a registry resolution that returns missing_workspace_package response" do
let(:dependency_name) { "@storybook/react-vite" }
let(:version) { "8.2.9" }
let(:previous_version) { "8.1.1" }
let(:requirements) do
[{
file: "package.json",
requirement: "8.2.9",
groups: ["optionalDependencies"],
source: nil
}]
end
let(:previous_requirements) do
[{
file: "package.json",
requirement: "8.1.1",
groups: ["optionalDependencies"],
source: nil
}]
end

let(:project_name) { "pnpm/missing_workspace_package" }

it "raises a helpful error" do
expect { updated_pnpm_lock_content }
.to raise_error(Dependabot::DependencyFileNotResolvable)
end
end

context "with a registry resolution that returns err_pnpm_peer_dep_issues response" do
let(:dependency_name) { "@typescript-eslint/eslint-plugin" }
let(:version) { "7.4.1" }
let(:previous_version) { "7.4.0" }
let(:requirements) do
[{
file: "package.json",
requirement: "7.4.1",
groups: ["devDependencies"],
source: nil
}]
end
let(:previous_requirements) do
[{
file: "package.json",
requirement: "7.4.0",
groups: ["devDependencies"],
source: nil
}]
end

let(:project_name) { "pnpm/peer_dep_issues" }

it "raises a helpful error" do
expect { updated_pnpm_lock_content }
.to raise_error(Dependabot::DependencyFileNotResolvable)
end
end

context "when there is a private repo we don't have access to and returns a 4xx error" do
let(:project_name) { "pnpm/private_repo_no_access" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"author": "name",
"private": true,
"description": "The storybook for waystone-ui",
"devDependencies": {
"@waystone/tsconfig": "workspace:*",
"storybook": "^8.1.1"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- 'apps/*'
- 'packages/*'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "name",
"private": true,
"packageManager": "pnpm@9.6.0",
"workspaces": [
"packages/*",
"packages/ts-for-gir/*",
"examples/*"
],
"devDependencies": {
"turbo": "2.0.14"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages:
- "packages/*"
- "packages/ts-for-gir/*"
- "examples/*"

0 comments on commit 87b447f

Please sign in to comment.