File: test_helper.rb

package info (click to toggle)
gem2deb 0.10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 804 kB
  • ctags: 275
  • sloc: ruby: 4,019; perl: 80; ansic: 61; makefile: 28; sh: 15
file content (188 lines) | stat: -rw-r--r-- 5,330 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
require 'test/unit'
require 'shoulda-context'
require 'mocha/setup'
require 'fileutils'
require 'tmpdir'
require 'tempfile'

require 'gem2deb'

Gem2DebTestCase = Test::Unit::TestCase
class Gem2DebTestCase

  SUPPORTED_VERSION_NUMBERS = Gem2Deb::RUBY_CONFIG_VERSION.values.sort

  OLDER_RUBY_VERSION = Gem2Deb::SUPPORTED_RUBY_VERSIONS.keys.select { |m| m =~ /^ruby/ }.sort.first
  OLDER_RUBY_VERSION_BINARY = Gem2Deb::SUPPORTED_RUBY_VERSIONS[OLDER_RUBY_VERSION]

  VENDOR_ARCH_DIRS = {}
  Gem2Deb::SUPPORTED_RUBY_VERSIONS.keys.each do |version|
    VENDOR_ARCH_DIRS[version] =
      `#{Gem2Deb::SUPPORTED_RUBY_VERSIONS[version]} -rrbconfig -e "puts RbConfig::CONFIG['vendorarchdir']"`.strip
  end

  require 'test_helper/samples'
  include Gem2DebTestCase::Samples

  GEM2DEB_ROOT_SOURCE_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))

  FileUtils.mkdir_p(TMP_DIR)

  class << self
    def tmpdir
      @tmpdir ||= File.join(Gem2DebTestCase::TMP_DIR, name)
      FileUtils.mkdir_p(@tmpdir)
      @tmpdir
    end
    def one_time_setup_blocks
      @one_time_setup_blocks ||= []
    end
    def one_time_setup(&block)
      one_time_setup_blocks << block
    end
    def one_time_setup?
      @one_time_setup
    end
    def one_time_setup!
      unless one_time_setup?
        one_time_setup_blocks.each(&:call)
        @one_time_setup = true
      end
      setup_cleanup
    end
    def setup_cleanup
      unless $__gem2deb_tests_cleanup_installed
        at_exit do
          if ENV['GEM2DEB_TEST_DEBUG']
            puts
            puts "======================================================================="
            puts "Temporary test files left in #{Gem2DebTestCase::TMP_DIR} for inspection!"
            puts
          else
            FileUtils.rm_rf(Gem2DebTestCase::TMP_DIR)
          end
        end
      end
      $__gem2deb_tests_cleanup_installed = true
    end
  end
  def tmpdir
    self.class.tmpdir
  end

  def setup
    self.class.one_time_setup!
  end

  protected

  def unpack(tarball)
    Dir.chdir(File.dirname(tarball)) do
      system 'tar', 'xzf', File.basename(tarball)
      ret = yield
      contents(tarball).each do |f|
        FileUtils.rm_rf(f)
      end
      ret
    end
  end

  def contents(tarball)
    IO.popen("tar tzf #{tarball}").readlines.map(&:strip)
  end

  def assert_contained_in_tarball(tarball, file)
    list = contents(tarball)
    assert list.include?(file), "#{tarball} should contain #{file} (contents: #{list.inspect})"
  end

  def assert_not_contained_in_tarball(tarball, file)
    list = contents(tarball)
    assert !list.include?(file), "#{tarball} should NOT contain #{file} (contents: #{list.inspect})"
  end

  def assert_file_exists(path)
    assert File.exist?(path), "#{path} should exist"
  end

  def assert_no_file_exists(path)
    assert !File.exist?(path), "#{path} should NOT exist"
  end

  # Installation-related functions

  def installed_file_path(gem_dirname, package, path, convert_gem_name = true)
    source_package_name = convert_gem_name ? 'ruby-' + gem_dirname : gem_dirname
    File.join(self.class.tmpdir, source_package_name, 'debian', package, path)
  end

  def assert_installed(gem_dirname, package, path, convert_gem_name = true)
    assert_file_exists installed_file_path(gem_dirname, package, path, convert_gem_name)
  end

  def self.silence_stream(stream)
    orig_stream = stream.clone
    begin
      Tempfile.open('gem2deb-tests-stdoud') do |f|
        stream.reopen(f)
        yield
      end
    ensure
      stream.reopen(orig_stream)
    end
  end

  # Runs a command with the current (in-development) gem2deb environment
  # loaded. PATH, PERL5LIB and RUBYLIB environment variables are tweaked to
  # make sure that everything that comes from gem2deb has precedence over
  # system-wide installed versions.
  def self.run_command(cmd)
    if !@environment_setup && !ENV['ADTTMP']
      # setup Perl lib for debhelper7
      perl5lib = File.join(GEM2DEB_ROOT_SOURCE_DIR, 'debhelper7')

      # setup the environment
      ENV['PERL5LIB'] = perl5lib
      ENV['PATH'] = [File.join(GEM2DEB_ROOT_SOURCE_DIR, 'bin'), File.join(GEM2DEB_ROOT_SOURCE_DIR, 'test', 'bin'), ENV['PATH']].join(':')
      ENV['RUBYLIB'] = File.join(GEM2DEB_ROOT_SOURCE_DIR, 'lib')

      @environment_setup = true
    end

    @run_command_id ||= -1
    @run_command_id += 1

    # run the command
    stdout = File.join(tmpdir, 'stdout.' + self.name + '.' + @run_command_id.to_s)
    stderr = File.join(tmpdir, 'stderr.' + self.name + '.' + @run_command_id.to_s)
    error = nil
    system("#{cmd} >#{stdout} 2>#{stderr}")
    if $?.exitstatus != 0
      error = "Command [#{cmd}] failed!\n"
      error << "Standard output:\n" << File.read(stdout).lines.map { |line| "  #{line}"}.join
      error << "Standard error:\n" << File.read(stderr).lines.map { |line| "  #{line}"}.join
    end
    FileUtils.rm_f(stdout)
    FileUtils.rm_f(stderr)
    if error
      raise error
    end
  end

  # See above
  def run_command(cmd)
    self.class.run_command(cmd)
  end

  # Utility method to fake contents in debian/control in the scope of a
  # test.
  def debian_control
    @debian_control ||=
      begin
        lines = []
        File.expects(:readlines).with('debian/control').returns(lines)
        lines
      end
  end

end