|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "spec_helper" |
| 5 | +require "dependabot/bundler" |
| 6 | +require "dependabot/dependency_graphers" |
| 7 | + |
| 8 | +# TODO: Implement a concrete Bundler class |
| 9 | +RSpec.describe "Dependabot::DependencyGraphers::Generic" do |
| 10 | + context "with a bundler project" do |
| 11 | + subject(:grapher) do |
| 12 | + Dependabot::DependencyGraphers.for_package_manager("bundler").new( |
| 13 | + dependency_files:, |
| 14 | + dependencies: |
| 15 | + ) |
| 16 | + end |
| 17 | + |
| 18 | + let(:parser) do |
| 19 | + Dependabot::FileParsers.for_package_manager("bundler").new( |
| 20 | + dependency_files:, |
| 21 | + repo_contents_path: nil, |
| 22 | + source: source, |
| 23 | + credentials: [], |
| 24 | + reject_external_code: false |
| 25 | + ) |
| 26 | + end |
| 27 | + |
| 28 | + let(:source) do |
| 29 | + Dependabot::Source.new( |
| 30 | + provider: "github", |
| 31 | + repo: "dependabot-fixtures/dependabot-test-ruby-package", |
| 32 | + directory: "/", |
| 33 | + branch: "main" |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + let(:dependencies) { parser.parse } |
| 38 | + |
| 39 | + # NOTE: This documents existing behaviour where Gemfile PURLs do not include a resolved version |
| 40 | + # |
| 41 | + # Package URLs deal in resolved versions, so for a Gemfile only project we only have a range |
| 42 | + # which cannot currently be submitted to the Dependency Submission API. |
| 43 | + # |
| 44 | + # This is working as intended, but when we implement a concrete Bundler grapher we will want |
| 45 | + # to execute a `bundle install` to resolve the file at runtime so we can submit the resolved |
| 46 | + # dependencies. |
| 47 | + context "with a Gemfile only" do |
| 48 | + let(:gemfile) do |
| 49 | + bundler_project_dependency_file("subdependency", filename: "Gemfile") |
| 50 | + end |
| 51 | + |
| 52 | + let(:dependency_files) do |
| 53 | + [gemfile] |
| 54 | + end |
| 55 | + |
| 56 | + it "specifies the Gemfile as the relevant dependency file" do |
| 57 | + expect(grapher.relevant_dependency_file).to eql(gemfile) |
| 58 | + end |
| 59 | + |
| 60 | + it "correctly serializes the resolved dependencies" do |
| 61 | + expect(grapher.resolved_dependencies.count).to be(1) |
| 62 | + |
| 63 | + ibandit = grapher.resolved_dependencies["ibandit"] |
| 64 | + expect(ibandit[:package_url]).to eql("pkg:gem/ibandit") |
| 65 | + expect(ibandit[:relationship]).to eql("direct") |
| 66 | + expect(ibandit[:scope]).to eql("runtime") |
| 67 | + expect(ibandit[:dependencies]).to be_empty |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context "with a Gemfile and Gemfile.lock" do |
| 72 | + let(:gemfile) do |
| 73 | + bundler_project_dependency_file("subdependency", filename: "Gemfile") |
| 74 | + end |
| 75 | + |
| 76 | + let(:gemfile_lock) do |
| 77 | + bundler_project_dependency_file("subdependency", filename: "Gemfile.lock") |
| 78 | + end |
| 79 | + |
| 80 | + let(:dependency_files) do |
| 81 | + [gemfile, gemfile_lock] |
| 82 | + end |
| 83 | + |
| 84 | + it "specifies the Gemfile.lock as the relevant dependency file" do |
| 85 | + expect(grapher.relevant_dependency_file).to eql(gemfile_lock) |
| 86 | + end |
| 87 | + |
| 88 | + it "correctly serializes the resolved dependencies" do |
| 89 | + resolved_dependencies = grapher.resolved_dependencies |
| 90 | + |
| 91 | + expect(resolved_dependencies.count).to be(2) |
| 92 | + |
| 93 | + expect(resolved_dependencies.keys).to eql(%w(ibandit i18n)) |
| 94 | + |
| 95 | + ibandit = resolved_dependencies["ibandit"] |
| 96 | + expect(ibandit[:package_url]).to eql("pkg:gem/[email protected]") |
| 97 | + expect(ibandit[:relationship]).to eql("direct") |
| 98 | + expect(ibandit[:scope]).to eql("runtime") |
| 99 | + expect(ibandit[:dependencies]).to be_empty # NYI: We don't set any subdependencies yet, this should contain i18n |
| 100 | + |
| 101 | + i18n = resolved_dependencies["i18n"] |
| 102 | + expect(i18n[:package_url]).to eql("pkg:gem/[email protected]") |
| 103 | + expect(i18n[:relationship]).to eql("indirect") |
| 104 | + expect(i18n[:scope]).to eql("runtime") |
| 105 | + expect(i18n[:dependencies]).to be_empty |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments