-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
add pypi_packages formula DSL
#20864
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # typed: strict | ||
| # frozen_string_literal: true | ||
|
|
||
| # Helper class for `pypi_packages` DSL. | ||
| # @api internal | ||
| class PypiPackages | ||
| sig { returns(T.nilable(String)) } | ||
| attr_reader :package_name | ||
|
|
||
| sig { returns(T::Array[String]) } | ||
| attr_reader :extra_packages | ||
|
|
||
| sig { returns(T::Array[String]) } | ||
| attr_reader :exclude_packages | ||
|
|
||
| sig { returns(T::Array[String]) } | ||
| attr_reader :dependencies | ||
|
|
||
| sig { params(tap: T.nilable(Tap), formula_name: String).returns(T.attached_class) } | ||
| def self.from_json_file(tap, formula_name) | ||
| list_entry = tap&.pypi_formula_mappings&.fetch(formula_name, nil) | ||
|
|
||
| return new(defined_pypi_mapping: false) if list_entry.nil? | ||
|
|
||
| case T.cast(list_entry, T.any(FalseClass, String, T::Hash[String, T.any(String, T::Array[String])])) | ||
| when false | ||
| new needs_manual_update: true | ||
| when String | ||
| new package_name: list_entry | ||
| when Hash | ||
| package_name = list_entry["package_name"] | ||
| extra_packages = list_entry.fetch("extra_packages", []) | ||
| exclude_packages = list_entry.fetch("exclude_packages", []) | ||
| dependencies = list_entry.fetch("dependencies", []) | ||
|
|
||
| new package_name:, extra_packages:, exclude_packages:, dependencies: | ||
| end | ||
| end | ||
|
|
||
| sig { | ||
| params( | ||
| package_name: T.nilable(String), | ||
| extra_packages: T::Array[String], | ||
| exclude_packages: T::Array[String], | ||
| dependencies: T::Array[String], | ||
| needs_manual_update: T::Boolean, | ||
| defined_pypi_mapping: T::Boolean, | ||
| ).void | ||
| } | ||
| def initialize( | ||
| package_name: nil, | ||
| extra_packages: [], | ||
| exclude_packages: [], | ||
| dependencies: [], | ||
| needs_manual_update: false, | ||
| defined_pypi_mapping: true | ||
| ) | ||
| @package_name = T.let(package_name, T.nilable(String)) | ||
| @extra_packages = T.let(extra_packages, T::Array[String]) | ||
| @exclude_packages = T.let(exclude_packages, T::Array[String]) | ||
| @dependencies = T.let(dependencies, T::Array[String]) | ||
| @needs_manual_update = T.let(needs_manual_update, T::Boolean) | ||
| @defined_pypi_mapping = T.let(defined_pypi_mapping, T::Boolean) | ||
| end | ||
|
|
||
| sig { returns(T::Boolean) } | ||
| def defined_pypi_mapping? = @defined_pypi_mapping | ||
|
|
||
| sig { returns(T::Boolean) } | ||
| def needs_manual_update? = @needs_manual_update | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "pypi_packages" | ||
|
|
||
| RSpec.describe PypiPackages do | ||
| describe ".from_json_file" do | ||
| let(:tap) { Tap.fetch("homebrew", "foo") } | ||
| let(:formula_name) { "test-formula" } | ||
| let(:mappings) { nil } | ||
|
|
||
| before do | ||
| allow(tap).to receive(:pypi_formula_mappings).and_return(mappings) | ||
| end | ||
|
|
||
| context "when JSON is `nil`" do | ||
| it "returns an instance with defined_pypi_mapping: false" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs).to be_a(described_class) | ||
| expect(pkgs.defined_pypi_mapping?).to be(false) | ||
| expect(pkgs.needs_manual_update?).to be(false) | ||
| expect(pkgs.package_name).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "when JSON is an empty hash" do | ||
| let(:mappings) { {} } | ||
|
|
||
| it "returns an instance with defined_pypi_mapping: false" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs).to be_a(described_class) | ||
| expect(pkgs.defined_pypi_mapping?).to be(false) | ||
| expect(pkgs.needs_manual_update?).to be(false) | ||
| expect(pkgs.package_name).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry is `false`" do | ||
| let(:mappings) { { formula_name => false } } | ||
|
|
||
| it "returns an instance with needs_manual_update: true" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs).to be_a(described_class) | ||
| expect(pkgs.defined_pypi_mapping?).to be(true) | ||
| expect(pkgs.needs_manual_update?).to be(true) | ||
| expect(pkgs.package_name).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry is a String" do | ||
| let(:mappings) { { formula_name => "bar" } } | ||
|
|
||
| it "returns an instance with package_name set" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs.package_name).to eq("bar") | ||
| expect(pkgs.extra_packages).to eq([]) | ||
| expect(pkgs.exclude_packages).to eq([]) | ||
| expect(pkgs.dependencies).to eq([]) | ||
| expect(pkgs.defined_pypi_mapping?).to be(true) | ||
| expect(pkgs.needs_manual_update?).to be(false) | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry is `true`" do | ||
| let(:mappings) { { formula_name => true } } | ||
|
|
||
| it "raises a Sorbet type error" do | ||
| expect do | ||
| described_class.from_json_file(tap, formula_name) | ||
| end.to raise_error(TypeError, /got type TrueClass/) | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry is a Hash" do | ||
| let(:mappings) do | ||
| { | ||
| formula_name => { | ||
| "package_name" => "bar", | ||
| "extra_packages" => ["baz"], | ||
| "exclude_packages" => ["qux"], | ||
| "dependencies" => ["quux"], | ||
| }, | ||
| } | ||
| end | ||
|
|
||
| it "returns an instance with all fields populated" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs.package_name).to eq("bar") | ||
| expect(pkgs.extra_packages).to eq(["baz"]) | ||
| expect(pkgs.exclude_packages).to eq(["qux"]) | ||
| expect(pkgs.dependencies).to eq(["quux"]) | ||
| expect(pkgs.defined_pypi_mapping?).to be(true) | ||
| expect(pkgs.needs_manual_update?).to be(false) | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry hash omits optional keys" do | ||
| let(:mappings) do | ||
| { formula_name => { "package_name" => "bar" } } | ||
| end | ||
|
|
||
| it "fills missing keys with empty arrays" do | ||
| pkgs = described_class.from_json_file(tap, formula_name) | ||
| expect(pkgs.package_name).to eq("bar") | ||
| expect(pkgs.extra_packages).to eq([]) | ||
| expect(pkgs.exclude_packages).to eq([]) | ||
| expect(pkgs.dependencies).to eq([]) | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry hash uses Array for `package_name`" do | ||
| let(:mappings) do | ||
| { formula_name => { "package_name" => ["bar"] } } | ||
| end | ||
|
|
||
| it "raises a Sorbet type error" do | ||
| expect do | ||
| described_class.from_json_file(tap, formula_name) | ||
| end.to raise_error(TypeError, /got type Array/) | ||
| end | ||
| end | ||
|
|
||
| context "when mapping entry hash uses String for keys" do | ||
| let(:mappings) do | ||
| { formula_name => { "extra_packages" => "bar" } } | ||
| end | ||
|
|
||
| it "raises a Sorbet type error" do | ||
| expect do | ||
| described_class.from_json_file(tap, formula_name) | ||
| end.to raise_error(TypeError, /got type String/) | ||
| end | ||
| end | ||
|
|
||
| context "when tap is `nil`" do | ||
| it "fills missing keys with empty arrays" do | ||
| pkgs = described_class.from_json_file(nil, formula_name) | ||
| expect(pkgs.defined_pypi_mapping?).to be(false) | ||
| expect(pkgs.package_name).to be_nil | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.