File: test-gem-file-contents

package info (click to toggle)
ruby-sqlite3 2.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 684 kB
  • sloc: ruby: 4,827; ansic: 1,868; sh: 91; makefile: 7
file content (205 lines) | stat: -rwxr-xr-x 6,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#! /usr/bin/env ruby
#
#  this script is intended to run as part of the CI test suite.
#
#  it inspects the contents of a gem file -- both the files and the gemspec -- to ensure we're
#  packaging what we expect, and that we're not packaging anything we don't expect.
#
#  this file isn't in the `test/` subdirectory because it's intended to be run standalone against a
#  built gem file (and not against the source code or behavior of the gem itself).
#
require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "minitest"
  gem "minitest-reporters"
end

require "yaml"

def usage_and_exit(message = nil)
  puts "ERROR: #{message}" if message
  puts "USAGE: #{File.basename(__FILE__)} <gemfile> [options]"
  exit(1)
end

usage_and_exit if ARGV.include?("-h")
usage_and_exit unless (gemfile = ARGV[0])
usage_and_exit("#{gemfile} does not exist") unless File.file?(gemfile)
usage_and_exit("#{gemfile} is not a gem") unless /\.gem$/.match?(gemfile)
gemfile = File.expand_path(gemfile)

gemfile_contents = Dir.mktmpdir do |dir|
  Dir.chdir(dir) do
    unless system("tar -xf #{gemfile} data.tar.gz")
      raise "could not unpack gem #{gemfile}"
    end

    `tar -ztf data.tar.gz`.split("\n")
  end
end

gemspec = Dir.mktmpdir do |dir|
  Dir.chdir(dir) do
    unless system("tar -xf #{gemfile} metadata.gz")
      raise "could not unpack gem #{gemfile}"
    end

    YAML.unsafe_load(`gunzip -c metadata.gz`)
  end
end

if ARGV.include?("-v")
  puts "---------- gemfile contents ----------"
  puts gemfile_contents
  puts
  puts "---------- gemspec ----------"
  puts gemspec.to_ruby
  puts
end

require "minitest/autorun"
require "minitest/reporters"
Minitest::Reporters.use!([Minitest::Reporters::SpecReporter.new])

puts "Testing '#{gemfile}' (#{gemspec.platform})"
describe File.basename(gemfile) do
  let(:supported_ruby_versions) { ["3.2", "3.3", "3.4", "4.0"] }

  describe "setup" do
    it "gemfile contains some files" do
      actual = gemfile_contents.length
      assert_operator(actual, :>, 10, "expected gemfile to contain more than #{actual} files")
    end

    it "gemspec is a Gem::Specification" do
      assert_equal(Gem::Specification, gemspec.class)
    end
  end

  describe "all platforms" do
    ["lib"].each do |dir|
      it "contains every ruby file in #{dir}/" do
        expected = `git ls-files #{dir}`.split("\n").grep(/\.rb$/).sort
        skip "looks like this isn't a git repository" if expected.empty?
        actual = gemfile_contents.select { |f| f.start_with?("#{dir}/") }.grep(/\.rb$/).sort
        assert_equal(expected, actual)
      end
    end

    ["test"].each do |dir|
      it "does not contain files from #{dir}/" do
        actual = gemfile_contents.select { |f| f.start_with?("#{dir}/") }.grep(/\.rb$/)
        assert_empty(actual)
      end
    end

    it "does not contain the Gemfile" do
      refute_includes(gemfile_contents, "Gemfile")
    end
  end

  if gemspec.platform == Gem::Platform::RUBY
    describe "ruby platform" do
      it "depends on mini_portile2" do
        assert(gemspec.dependencies.find { |d| d.name == "mini_portile2" })
      end

      it "contains extension C and header files" do
        assert_equal(6, gemfile_contents.count { |f| File.fnmatch?("ext/**/*.c", f) })
        assert_equal(7, gemfile_contents.count { |f| File.fnmatch?("ext/**/*.h", f) })
      end

      it "includes C files in extra_rdoc_files" do
        assert_equal(6, gemspec.extra_rdoc_files.count { |f| File.fnmatch?("ext/**/*.c", f) })
      end

      it "contains the port files" do
        dependencies = YAML.load_file(File.join(__dir__, "..", "dependencies.yml"), symbolize_names: true)
        sqlite_tarball = File.basename(dependencies[:sqlite3][:files].first[:url])
        actual_ports = gemfile_contents.grep(%r{^ports/})

        assert_equal(["ports/archives/#{sqlite_tarball}"], actual_ports)
      end

      it "contains the patch files" do
        assert_equal(Dir.glob("patches/*.patch").length, gemfile_contents.count { |f| File.fnmatch?("patches/*", f) })
      end

      it "sets metadata for msys2" do
        refute_nil(gemspec.metadata["msys2_mingw_dependencies"])
      end

      it "sets required_ruby_version appropriately" do
        supported_ruby_versions.each do |v|
          assert(
            gemspec.required_ruby_version.satisfied_by?(Gem::Version.new(v)),
            "required_ruby_version='#{gemspec.required_ruby_version}' should support ruby #{v}"
          )
        end
      end
    end
  end

  if gemspec.platform.is_a?(Gem::Platform) && gemspec.platform.cpu
    describe "native platform" do
      it "does not depend on mini_portile2" do
        refute(gemspec.dependencies.find { |d| d.name == "mini_portile2" })
      end

      it "contains extension C and header files" do
        assert_equal(6, gemfile_contents.count { |f| File.fnmatch?("ext/**/*.c", f) })
        assert_equal(7, gemfile_contents.count { |f| File.fnmatch?("ext/**/*.h", f) })
      end

      it "includes C files in extra_rdoc_files" do
        assert_equal(6, gemspec.extra_rdoc_files.count { |f| File.fnmatch?("ext/**/*.c", f) })
      end

      it "does not contain the port files" do
        assert_empty(gemfile_contents.grep(%r{^ports/}))
      end

      it "does not contain the patch files" do
        assert_empty(gemfile_contents.grep(%r{^patches/}))
      end

      it "contains expected shared library files " do
        supported_ruby_versions.each do |version|
          actual = gemfile_contents.find do |p|
            File.fnmatch?("lib/sqlite3/#{version}/sqlite3_native.{so,bundle}", p, File::FNM_EXTGLOB)
          end
          assert(actual, "expected to find shared library file for ruby #{version}")
        end

        actual = gemfile_contents.find do |p|
          File.fnmatch?("lib/sqlite3/sqlite3_native.{so,bundle}", p, File::FNM_EXTGLOB)
        end
        refute(actual, "did not expect to find shared library file in lib/sqlite3")

        actual = gemfile_contents.find_all do |p|
          File.fnmatch?("lib/sqlite3/**/*.{so,bundle}", p, File::FNM_EXTGLOB)
        end
        assert_equal(
          supported_ruby_versions.length,
          actual.length,
          "did not expect extra shared library files"
        )
      end

      it "sets required_ruby_version appropriately" do
        supported_ruby_versions.each do |v|
          assert(
            gemspec.required_ruby_version.satisfied_by?(Gem::Version.new(v)),
            "required_ruby_version='#{gemspec.required_ruby_version}' should support ruby #{v}"
          )
        end
      end

      it "does not set metadata for msys2" do
        assert_nil(gemspec.metadata["msys2_mingw_dependencies"])
      end
    end
  end
end