Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle String response for "find latest version" #246

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/importmap/npm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_json(uri)
end

def find_latest_version(response)
latest_version = response.dig('dist-tags', 'latest')
latest_version = response.is_a?(String) ? response : response.dig('dist-tags', 'latest')
Copy link
Contributor Author

@vietqhoang vietqhoang Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response may be a blank String. One reported issue (#245 (comment)) made me question this possibility.

If this is the case then a fallback may be worthwhile. I am writing it with available Rails methods, but can change it to use Ruby-only accessible ones.

Suggested change
latest_version = response.is_a?(String) ? response : response.dig('dist-tags', 'latest')
latest_version = response.is_a?(String) ? (response.presence || 'unknown') : response.dig('dist-tags', 'latest')

return latest_version if latest_version

return unless response['versions']
Expand Down
10 changes: 10 additions & 0 deletions test/npm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ def code() "200" end
end
end
end

test "return latest version response is a String type" do
response = "version not found".to_json

@npm.stub(:get_json, response) do
outdated_packages = @npm.outdated_packages

assert_equal('version not found', outdated_packages[0].latest_version)
end
end
end
Loading