|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "pypi_packages" |
| 4 | + |
| 5 | +RSpec.describe PypiPackages do |
| 6 | + describe ".from_json_file" do |
| 7 | + let(:tap) { Tap.fetch("homebrew", "foo") } |
| 8 | + let(:formula_name) { "test-formula" } |
| 9 | + let(:mappings) { nil } |
| 10 | + |
| 11 | + before do |
| 12 | + allow(tap).to receive(:pypi_formula_mappings).and_return(mappings) |
| 13 | + end |
| 14 | + |
| 15 | + context "when JSON is `nil`" do |
| 16 | + it "returns an instance with defined_pypi_mapping: false" do |
| 17 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 18 | + expect(pkgs).to be_a(described_class) |
| 19 | + expect(pkgs.defined_pypi_mapping?).to be(false) |
| 20 | + expect(pkgs.needs_manual_update?).to be(false) |
| 21 | + expect(pkgs.package_name).to be_nil |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + context "when JSON is an empty hash" do |
| 26 | + let(:mappings) { {} } |
| 27 | + |
| 28 | + it "returns an instance with defined_pypi_mapping: false" do |
| 29 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 30 | + expect(pkgs).to be_a(described_class) |
| 31 | + expect(pkgs.defined_pypi_mapping?).to be(false) |
| 32 | + expect(pkgs.needs_manual_update?).to be(false) |
| 33 | + expect(pkgs.package_name).to be_nil |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context "when mapping entry is `false`" do |
| 38 | + let(:mappings) { { formula_name => false } } |
| 39 | + |
| 40 | + it "returns an instance with needs_manual_update: true" do |
| 41 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 42 | + expect(pkgs).to be_a(described_class) |
| 43 | + expect(pkgs.defined_pypi_mapping?).to be(true) |
| 44 | + expect(pkgs.needs_manual_update?).to be(true) |
| 45 | + expect(pkgs.package_name).to be_nil |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when mapping entry is a String" do |
| 50 | + let(:mappings) { { formula_name => "bar" } } |
| 51 | + |
| 52 | + it "returns an instance with package_name set" do |
| 53 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 54 | + expect(pkgs.package_name).to eq("bar") |
| 55 | + expect(pkgs.extra_packages).to eq([]) |
| 56 | + expect(pkgs.exclude_packages).to eq([]) |
| 57 | + expect(pkgs.dependencies).to eq([]) |
| 58 | + expect(pkgs.defined_pypi_mapping?).to be(true) |
| 59 | + expect(pkgs.needs_manual_update?).to be(false) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "when mapping entry is `true`" do |
| 64 | + let(:mappings) { { formula_name => true } } |
| 65 | + |
| 66 | + it "raises a Sorbet type error" do |
| 67 | + expect do |
| 68 | + described_class.from_json_file(tap, formula_name) |
| 69 | + end.to raise_error(TypeError, /got type TrueClass/) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context "when mapping entry is a Hash" do |
| 74 | + let(:mappings) do |
| 75 | + { |
| 76 | + formula_name => { |
| 77 | + "package_name" => "bar", |
| 78 | + "extra_packages" => ["baz"], |
| 79 | + "exclude_packages" => ["qux"], |
| 80 | + "dependencies" => ["quux"], |
| 81 | + }, |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + it "returns an instance with all fields populated" do |
| 86 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 87 | + expect(pkgs.package_name).to eq("bar") |
| 88 | + expect(pkgs.extra_packages).to eq(["baz"]) |
| 89 | + expect(pkgs.exclude_packages).to eq(["qux"]) |
| 90 | + expect(pkgs.dependencies).to eq(["quux"]) |
| 91 | + expect(pkgs.defined_pypi_mapping?).to be(true) |
| 92 | + expect(pkgs.needs_manual_update?).to be(false) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context "when mapping entry hash omits optional keys" do |
| 97 | + let(:mappings) do |
| 98 | + { formula_name => { "package_name" => "bar" } } |
| 99 | + end |
| 100 | + |
| 101 | + it "fills missing keys with empty arrays" do |
| 102 | + pkgs = described_class.from_json_file(tap, formula_name) |
| 103 | + expect(pkgs.package_name).to eq("bar") |
| 104 | + expect(pkgs.extra_packages).to eq([]) |
| 105 | + expect(pkgs.exclude_packages).to eq([]) |
| 106 | + expect(pkgs.dependencies).to eq([]) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context "when mapping entry hash uses Array for `package_name`" do |
| 111 | + let(:mappings) do |
| 112 | + { formula_name => { "package_name" => ["bar"] } } |
| 113 | + end |
| 114 | + |
| 115 | + it "raises a Sorbet type error" do |
| 116 | + expect do |
| 117 | + described_class.from_json_file(tap, formula_name) |
| 118 | + end.to raise_error(TypeError, /got type Array/) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context "when mapping entry hash uses String for s key" do |
| 123 | + let(:mappings) do |
| 124 | + { formula_name => { "extra_packages" => "bar" } } |
| 125 | + end |
| 126 | + |
| 127 | + it "raises a Sorbet type error" do |
| 128 | + expect do |
| 129 | + described_class.from_json_file(tap, formula_name) |
| 130 | + end.to raise_error(TypeError, /got type String/) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context "when tap is `nil`" do |
| 135 | + it "fills missing keys with empty arrays" do |
| 136 | + pkgs = described_class.from_json_file(nil, formula_name) |
| 137 | + expect(pkgs.defined_pypi_mapping?).to be(false) |
| 138 | + expect(pkgs.package_name).to be_nil |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments