Debian Codegraph @

scripts/test-gem-file-contents

raw
1 #! /usr/bin/env ruby
2 # frozen_string_literal: true
3
4 #
5 # this script is intended to run as part of the CI test suite.
6 #
7 # it inspects the contents of a nokogiri gem file -- both the files and the gemspec -- to ensure
8 # we're packaging what we expect, and that we're not packaging anything we don't expect.
9 #
10 # this file isn't in the `test/` subdirectory because it's intended to be run standalone against a
11 # built gem file (and not against the source code or behavior of the gem itself).
12 #
13
14 require "bundler/inline"
15
16 gemfile do
17 source "https://rubygems.org"
18 gem "minitest"
19 end
20
21 require "yaml"
22
23 def usage_and_exit(message = nil)
24 puts "ERROR: #{message}" if message
25 puts "USAGE: #{File.basename(__FILE__)} <gemfile> [options]"
26 exit(1)
27 end
28
29 usage_and_exit if ARGV.include?("-h")
30 usage_and_exit unless (gemfile = ARGV[0])
31 usage_and_exit("#{gemfile} does not exist") unless File.file?(gemfile)
32 usage_and_exit("#{gemfile} is not a gem") unless /\.gem$/.match?(gemfile)
33 gemfile = File.expand_path(gemfile)
34
35 gemfile_contents = Dir.mktmpdir do |dir|
36 Dir.chdir(dir) do
37 unless system("tar -xf #{gemfile} data.tar.gz")
38 raise "could not unpack gem #{gemfile}"
39 end
40
41 %x(tar -ztf data.tar.gz).split("\n")
42 end
43 end
44
45 gemspec = Dir.mktmpdir do |dir|
46 Dir.chdir(dir) do
47 unless system("tar -xf #{gemfile} metadata.gz")
48 raise "could not unpack gem #{gemfile}"
49 end
50
51 YAML.safe_load(
52 %x(gunzip -c metadata.gz),
53 permitted_classes: [Gem::Specification, Gem::Version, Gem::Dependency, Gem::Requirement, Time, Symbol],
54 )
55 end
56 end
57
58 if ARGV.include?("-v")
59 puts "---------- gemfile contents ----------"
60 puts gemfile_contents
61 puts
62 puts "---------- gemspec ----------"
63 puts gemspec.to_ruby
64 puts
65 end
66
67 require "minitest/autorun"
68
69 puts "Testing '#{gemfile}' (#{gemspec.platform})"
70 describe File.basename(gemfile) do
71 let(:cross_rubies_path) { File.join(File.dirname(__FILE__), "..", ".cross_rubies") }
72
73 let(:platform_supported_ruby_versions) do
74 File.read(cross_rubies_path).split("\n").filter_map do |line|
75 ver, plat = line.split(":")
76 next if plat != gemspec.platform.to_s
77
78 ver.split(".").take(2).join(".") # ugh
79 end.uniq.sort
80 end
81
82 let(:all_supported_ruby_versions) do
83 File.read(cross_rubies_path).split("\n").map do |line|
84 ver, _ = line.split(":")
85 ver.split(".").take(2).join(".") # ugh
86 end.uniq.sort
87 end
88
89 describe "setup" do
90 it "gemfile contains some files" do
91 actual = gemfile_contents.length
92 assert_operator(actual, :>, 60, "expected gemfile to contain more than #{actual} files")
93 end
94
95 it "gemspec is a Gem::Specification" do
96 assert_equal(Gem::Specification, gemspec.class)
97 end
98 end
99
100 describe "all platforms" do
101 it "contains every ruby file in lib/" do
102 expected = %x(git ls-files lib).split("\n").grep(/\.rb$/).sort
103 skip "looks like this isn't a git repository" if expected.empty?
104 actual = gemfile_contents.grep(%r{^lib/}).grep(/\.rb$/).sort
105 assert_equal(expected, actual)
106 end
107 end
108
109 describe "ruby platform" do
110 it "depends on mini_portile2" do
111 assert(gemspec.dependencies.find { |d| d.name == "mini_portile2" })
112 end
113
114 it "contains ext/nokogiri C and header files" do
115 assert_operator(gemfile_contents.grep(%r{^ext/nokogiri/.*\.c}).length, :>, 20)
116 assert_operator(gemfile_contents.grep(%r{^ext/nokogiri/.*\.h}).length, :>, 0)
117 end
118
119 it "includes C files in extra_rdoc_files" do
120 assert_operator(gemspec.extra_rdoc_files.grep(%r{ext/nokogiri/.*\.c$}).length, :>, 10)
121 end
122
123 it "contains the port files" do
124 actual_ports = gemfile_contents.grep(%r{^ports/})
125 assert_equal(
126 1,
127 actual_ports.grep(/libxml2-\d+\.\d+\.\d+\.tar\.[gx]z/).length,
128 "expected #{actual_ports} to include libxml2",
129 )
130 assert_equal(
131 1,
132 actual_ports.grep(/libxslt-\d+\.\d+\.\d+\.tar\.[gx]z/).length,
133 "expected #{actual_ports} to include libxslt",
134 )
135 assert_equal(2, actual_ports.length)
136 end
137
138 it "contains the patch files" do
139 assert_operator(gemfile_contents.grep(%r{^patches/}).length, :>, 0)
140 end
141
142 it "does not contain packaged libraries' header files" do
143 # these files are present after installation if the packaged libraries are used
144 assert_empty(gemfile_contents.grep(%r{^ext/nokogiri/include/}))
145 end
146
147 it "contains the gumbo parser source code" do
148 assert_includes(gemfile_contents, "gumbo-parser/src/Makefile")
149 assert_operator(gemfile_contents.grep(%r{^gumbo-parser/src/.*\.c}).length, :>, 10)
150 assert_operator(gemfile_contents.grep(%r{^gumbo-parser/src/.*\.h}).length, :>, 10)
151 end
152
153 it "does not contain java files" do
154 assert_empty(gemfile_contents.grep(%r{^ext/java/}))
155 assert_empty(gemfile_contents.grep(/.*\.jar$/))
156 end
157 end if gemspec.platform == Gem::Platform::RUBY
158
159 describe "native platform" do
160 it "does not depend on mini_portile2" do
161 refute(gemspec.dependencies.find { |d| d.name == "mini_portile2" })
162 end
163
164 it "contains ext/nokogiri C and header files" do
165 assert_operator(gemfile_contents.grep(%r{^ext/nokogiri/.*\.c}).length, :>, 20)
166 assert_operator(gemfile_contents.grep(%r{^ext/nokogiri/.*\.h}).length, :>, 20)
167 end
168
169 it "includes C files in extra_rdoc_files" do
170 assert_operator(gemspec.extra_rdoc_files.grep(%r{ext/nokogiri/.*\.c$}).length, :>, 10)
171 end
172
173 it "does not contain the port files" do
174 assert_empty(gemfile_contents.grep(%r{^ports/}))
175 end
176
177 it "does not contain the patch files" do
178 assert_empty(gemfile_contents.grep(%r{^patches/}))
179 end
180
181 it "contains packaged libraries' header files" do
182 assert_includes(gemfile_contents, "ext/nokogiri/include/libxml2/libxml/tree.h")
183 assert_includes(gemfile_contents, "ext/nokogiri/include/libxslt/xslt.h")
184 assert_includes(gemfile_contents, "ext/nokogiri/include/libexslt/exslt.h")
185 end
186
187 it "does not contain the gumbo parser source code" do
188 assert_empty(gemfile_contents.grep(%r{^gumbo-parser/src/}))
189 end
190
191 it "does not contain java files" do
192 assert_empty(gemfile_contents.grep(%r{^ext/java/}))
193 assert_empty(gemfile_contents.grep(/.*\.jar$/))
194 end
195
196 it "contains expected shared library files " do
197 platform_supported_ruby_versions.each do |version|
198 actual = gemfile_contents.find do |p|
199 File.fnmatch?("lib/nokogiri/#{version}/nokogiri.{so,bundle}", p, File::FNM_EXTGLOB)
200 end
201 assert(actual, "expected to find shared library file for ruby #{version}")
202 end
203
204 actual = gemfile_contents.find do |p|
205 File.fnmatch?("lib/nokogiri/nokogiri.{so,bundle}", p, File::FNM_EXTGLOB)
206 end
207 refute(actual, "did not expect to find shared library file in lib/nokogiri")
208
209 actual = gemfile_contents.find_all do |p|
210 File.fnmatch?("lib/nokogiri/**/*.{so,bundle}", p, File::FNM_EXTGLOB)
211 end
212 assert_equal(
213 platform_supported_ruby_versions.length,
214 actual.length,
215 "did not expect extra shared library files",
216 )
217 end
218
219 it "sets required_ruby_version appropriately" do
220 unsupported_versions = all_supported_ruby_versions - platform_supported_ruby_versions
221 platform_supported_ruby_versions.each do |v|
222 assert(
223 gemspec.required_ruby_version.satisfied_by?(Gem::Version.new(v)),
224 "required_ruby_version='#{gemspec.required_ruby_version}' should support ruby #{v}",
225 )
226 end
227 unsupported_versions.each do |v|
228 refute(
229 gemspec.required_ruby_version.satisfied_by?(Gem::Version.new(v)),
230 "required_ruby_version='#{gemspec.required_ruby_version}' should not support ruby #{v}",
231 )
232 end
233 end
234 end if gemspec.platform.is_a?(Gem::Platform) && gemspec.platform.cpu
235
236 describe "java platform" do
237 it "does not depend on mini_portile2" do
238 refute(gemspec.dependencies.find { |d| d.name == "mini_portile2" })
239 end
240
241 it "contains ext/nokogiri C files" do
242 # NOTE: we keep the C files because they have docstrings and Java files don't
243 assert_operator(gemfile_contents.grep(%r{^ext/nokogiri/.*\.c}).length, :>, 20)
244 end
245
246 it "does not contain ext/nokogiri header files" do
247 assert_empty(gemfile_contents.grep(%r{^ext/nokogiri/.*\.h}))
248 end
249
250 it "includes C files in extra_rdoc_files" do
251 assert_operator(gemspec.extra_rdoc_files.grep(%r{ext/nokogiri/.*\.c$}).length, :>, 10)
252 end
253
254 it "does not contain the port files" do
255 assert_empty(gemfile_contents.grep(%r{^ports/}))
256 end
257
258 it "does not contain the patch files" do
259 assert_empty(gemfile_contents.grep(%r{^patches/}))
260 end
261
262 it "does not contain packaged libraries' header files" do
263 assert_empty(gemfile_contents.grep(%r{^ext/nokogiri/include/}))
264 end
265
266 it "does not contain the gumbo parser source code" do
267 assert_empty(gemfile_contents.grep(%r{^gumbo-parser/src/}))
268 end
269
270 it "contains java source files" do
271 assert_operator(gemfile_contents.grep(%r{^ext/java/.*\.java}).length, :>, 20)
272 end
273
274 it "contains the java jar files" do
275 assert_includes(gemfile_contents, "lib/nokogiri/nokogiri.jar")
276
277 actual_jars = gemfile_contents.grep(/.*\.jar$/)
278 expected_jars = [
279 "isorelax",
280 "jing",
281 "neko-htmlunit",
282 "nekodtd",
283 "serializer",
284 "xalan",
285 "xercesImpl",
286 "xml-apis",
287 ]
288 expected_jars.each do |name|
289 assert_equal(
290 1,
291 actual_jars.count { |j| File.fnmatch?("lib/nokogiri/jruby/**/#{name}-*.jar", j) },
292 "expected to contain '#{name}-*.jar'",
293 )
294 end
295 end
296 end if gemspec.platform == Gem::Platform.new("java")
297 end