-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for parsing requirements
- Loading branch information
Showing
6 changed files
with
209 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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,36 @@ | ||
# Copyright (C) 2025 Ruby-GNOME Project Team | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
module RubyGemsRequirementsSystem | ||
Requirement = Struct.new(:packages, :system_packages) do | ||
def satisfied? | ||
packages.any? do |package| | ||
installed?(package) | ||
end | ||
end | ||
|
||
private | ||
def installed?(package) | ||
package_config = PKGConfig.package_config(package.id) | ||
begin | ||
package_config.cflags | ||
rescue PackageConfig::NotFoundError | ||
return false | ||
end | ||
|
||
package.satisfied?(package_config.version) | ||
end | ||
end | ||
end |
This file contains 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,75 @@ | ||
# Copyright (C) 2025 Ruby-GNOME Project Team | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
require_relative "package" | ||
require_relative "requirement" | ||
|
||
module RubyGemsRequirementsSystem | ||
class RequirementsParser | ||
def initialize(gemspec_requirements, platform) | ||
@gemspec_requirements = gemspec_requirements | ||
@platform = platform | ||
end | ||
|
||
def parse | ||
all_packages_set = {} | ||
requirements = {} | ||
@gemspec_requirements.each do |gemspec_requirement| | ||
components = gemspec_requirement.split(/: +/, 4) | ||
next unless components.size == 4 | ||
|
||
id, raw_packages, platform, system_package = components | ||
next unless id == "system" | ||
|
||
packages = parse_packages(raw_packages) | ||
next if packages.empty? | ||
|
||
all_packages_set[packages] = true | ||
|
||
next unless @platform.target?(platform) | ||
requirements[packages] ||= [] | ||
requirements[packages] << system_package | ||
end | ||
(all_packages_set.keys - requirements.keys).each do |not_used_packages| | ||
system_packages = @platform.default_system_packages(not_used_packages) | ||
next if system_packages.nil? | ||
requirements[not_used_packages] = system_packages | ||
end | ||
requirements.collect do |packages, system_packages| | ||
Requirement.new(packages, system_packages) | ||
end | ||
end | ||
|
||
private | ||
def parse_packages(raw_packages) | ||
packages = raw_packages.split(/\s*\|\s*/).collect do |raw_package| | ||
Package.parse(raw_package) | ||
end | ||
# Ignore this requirement if any invalid package is included. | ||
# We must not report an error for this because | ||
# Gem::Specification#requirements is a free form | ||
# configuration. So there are configuration values that use | ||
# "system: ..." but not for this plugin. We can report a | ||
# warning instead. | ||
packages.each do |package| | ||
unless package.valid? | ||
# TODO: Report a warning | ||
return [] | ||
end | ||
end | ||
packages | ||
end | ||
end | ||
end |
This file contains 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 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,94 @@ | ||
# Copyright (C) 2025 Ruby-GNOME Project Team | ||
# | ||
# This library is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
require_relative "helper" | ||
|
||
class TestRequiremtsParser < Test::Unit::TestCase | ||
Package = RubyGemsRequirementsSystem::Package | ||
Platform = RubyGemsRequirementsSystem::Platform | ||
Requirement = RubyGemsRequirementsSystem::Requirement | ||
RequirementsParser = RubyGemsRequirementsSystem::RequirementsParser | ||
|
||
def setup | ||
@platform = Platform::Ubuntu.new | ||
end | ||
|
||
def parse(gemspec_requirements) | ||
RequirementsParser.new(gemspec_requirements, @platform).parse | ||
end | ||
|
||
def test_minimal | ||
gemspec_requirements = [ | ||
"system: cairo: debian: libcairo2-dev", | ||
] | ||
assert_equal([ | ||
Requirement.new([Package.new("cairo")], | ||
["libcairo2-dev"]), | ||
], | ||
parse(gemspec_requirements)) | ||
end | ||
|
||
def test_required_version | ||
gemspec_requirements = [ | ||
"system: cairo >= 1.2.0: debian: libcairo2-dev", | ||
] | ||
assert_equal([ | ||
Requirement.new([Package.new("cairo", ">=", "1.2.0")], | ||
["libcairo2-dev"]), | ||
], | ||
parse(gemspec_requirements)) | ||
end | ||
|
||
def test_or_dependency | ||
gemspec_requirements = [ | ||
"system: mysqlclient|libmariadb: debian: libmariadb-dev", | ||
"system: mysqlclient|libmariadb: ubuntu: libmysqlclient-dev", | ||
] | ||
assert_equal([ | ||
Requirement.new([ | ||
Package.new("mysqlclient"), | ||
Package.new("libmariadb"), | ||
], | ||
[ | ||
"libmariadb-dev", | ||
"libmysqlclient-dev", | ||
]), | ||
], | ||
parse(gemspec_requirements)) | ||
end | ||
|
||
def test_https | ||
deb_url = "https://packages.groonga.org/%{distribution}/groonga-apt-source-latest-%{code_name}.deb" | ||
gemspec_requirements = [ | ||
"system: groonga: debian: #{deb_url}", | ||
] | ||
assert_equal([ | ||
Requirement.new([Package.new("groonga")], | ||
[deb_url]), | ||
], | ||
parse(gemspec_requirements)) | ||
end | ||
|
||
def test_ppa | ||
gemspec_requirements = [ | ||
"system: groonga: ubuntu: ppa:groonga/ppa", | ||
] | ||
assert_equal([ | ||
Requirement.new([Package.new("groonga")], | ||
["ppa:groonga/ppa"]), | ||
], | ||
parse(gemspec_requirements)) | ||
end | ||
end |