diff --git a/lib/nokogiri/version/info.rb b/lib/nokogiri/version/info.rb index aacc92f8445..4755d7478de 100644 --- a/lib/nokogiri/version/info.rb +++ b/lib/nokogiri/version/info.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true require "singleton" +require "shellwords" module Nokogiri class VersionInfo # :nodoc: @@ -72,9 +73,21 @@ def warnings end def to_hash + header_directory = File.expand_path(File.join(File.dirname(__FILE__), "../../../ext/nokogiri")) {}.tap do |vi| vi["warnings"] = [] - vi["nokogiri"] = Nokogiri::VERSION + vi["nokogiri"] = {}.tap do |nokogiri| + nokogiri["version"] = Nokogiri::VERSION + + unless jruby? + cppflags = ["-I#{header_directory.shellescape}"] + if libxml2_using_packaged? + cppflags << "-I#{File.join(header_directory, "include").shellescape}" + cppflags << "-I#{File.join(header_directory, "include/libxml2").shellescape}" + end + nokogiri["cppflags"] = cppflags + end + end vi["ruby"] = {}.tap do |ruby| ruby["version"] = ::RUBY_VERSION ruby["platform"] = ::RUBY_PLATFORM @@ -92,7 +105,7 @@ def to_hash libxml["patches"] = Nokogiri::LIBXML2_PATCHES # this is for nokogumbo and shouldn't be forever - libxml["libxml2_path"] = File.expand_path(File.join(File.dirname(__FILE__), "../../../ext/nokogiri")) + libxml["libxml2_path"] = header_directory else libxml["source"] = "system" end diff --git a/scripts/test-gem-installation b/scripts/test-gem-installation index a5b3e6b4e8e..6212004515f 100755 --- a/scripts/test-gem-installation +++ b/scripts/test-gem-installation @@ -50,6 +50,8 @@ describe gemspec.full_name do let(:nokogiri_header_files) { ["nokogiri.h", "xml_document.h", "xml_node.h"] } let(:packaged_library_header_files) { ["libxml2/libxml/tree.h", "libxslt/xslt.h", "libexslt/exslt.h"] } + let(:headers_dirs) { Nokogiri::VERSION_INFO["nokogiri"]["cppflags"].map { |f| f.gsub(/^-I/, "") } } + it "loads the same version as the spec we've loaded" do assert_equal(Nokogiri::VERSION, gemspec.version.to_s) end @@ -59,6 +61,12 @@ describe gemspec.full_name do nokogiri_header_files.each do |header| assert(File.file?(File.join(nokogiri_ext_dir, header)), "expected #{header} to be installed in #{nokogiri_ext_dir}") + + found = false + headers_dirs.each do |header_dir| + found = true if File.file?(File.join(header_dir, "nokogiri.h")) + end + assert(found, "expected to find nokogiri.h in one of: #{headers_dirs.inspect}") end end @@ -76,16 +84,28 @@ describe gemspec.full_name do describe "library" do describe "packaged" do - it "declares where headers are installed" do + describe "for nokogumbo" do # this is for nokogumbo and shouldn't be forever - assert_equal(nokogiri_ext_dir, Nokogiri::VERSION_INFO["libxml"]["libxml2_path"], - "expected Nokogiri::VERSION_INFO to point to #{nokogiri_ext_dir}") + it "declares where headers are installed" do + assert_equal(nokogiri_ext_dir, Nokogiri::VERSION_INFO["libxml"]["libxml2_path"], + "expected Nokogiri::VERSION_INFO to point to #{nokogiri_ext_dir}") + end + + it "installs packaged libraries' headers" do + packaged_library_header_files.each do |header| + assert(File.file?(File.join(nokogiri_include_dir, header)), + "expected #{header} to be installed in #{nokogiri_include_dir}") + end + end end - it "installs packaged libraries' headers" do + it "points to packaged libraries' headers" do packaged_library_header_files.each do |header| - assert(File.file?(File.join(nokogiri_include_dir, header)), - "expected #{header} to be installed in #{nokogiri_include_dir}") + found = false + headers_dirs.each do |header_dir| + found = true if File.file?(File.join(header_dir, header)) + end + assert(found, "expected to find #{header} in one of: #{headers_dirs.inspect}") end end end if Nokogiri::VersionInfo.instance.libxml2_using_packaged? diff --git a/test/test_version.rb b/test/test_version.rb index d79907c2c0d..edba2bec60a 100644 --- a/test/test_version.rb +++ b/test/test_version.rb @@ -13,6 +13,15 @@ module TestVersionInfoTests def test_version_info_basics assert_match(VERSION_MATCH, Nokogiri::VERSION) + assert_equal(Nokogiri::VERSION, Nokogiri::VERSION_INFO["nokogiri"]["version"]) + + if jruby? + refute(Nokogiri::VERSION_INFO["nokogiri"].has_key?("cppflags"), "did not expect cppflags") + else + # cppflags are more fully tested in scripts/test-gem-installation + assert_kind_of(Array, Nokogiri::VERSION_INFO["nokogiri"]["cppflags"], "expected cppflags to be an array") + end + assert_equal(::RUBY_VERSION, Nokogiri::VERSION_INFO["ruby"]["version"]) assert_equal(::RUBY_PLATFORM, Nokogiri::VERSION_INFO["ruby"]["platform"]) assert_equal(::Gem::Platform.local.to_s, Nokogiri::VERSION_INFO["ruby"]["gem_platform"])