#!/usr/bin/ruby -EUTF-8

# Copyright © 2011, Lucas Nussbaum <lucas@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'rbconfig'
require 'fileutils'
require 'optparse'
require 'shellwords'

require 'gem2deb/test_runner'

options = {}

if ENV['GEM2DEB_TEST_RUNNER']
  opts = Shellwords.split(ENV['GEM2DEB_TEST_RUNNER'])
  ARGV.unshift(*opts)
end

optparse =  OptionParser.new do |opts|

  opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS]"
  opts.separator 'Options:'

  opts.on('--autopkgtest', 'Runs tests against the installed package') do
    options[:autopkgtest] = true
  end

  opts.on('-c', '--check-dependencies', 'Check dependencies') do
    options[:check_dependencies] = true
  end

  opts.on(
    '-b', '--check-bundler',
    "Check loading the package under bundler"
  ) do
    options[:check_bundler] = true
  end

end

optparse.parse!

runner = Gem2Deb::TestRunner.detect!
options.each do |opt,value|
  runner.send("#{opt}=", value)
end
runner.run_tests

__END__
=head1 NAME

gem2deb-test-runner - runs test suite contained in Debian Ruby packages

=head1 SYNOPSIS

B<gem2deb-test-runner> [B<OPTIONS>]

=head1 DESCRIPTION

B<gem2deb-test-runner> runs the tests shipped inside a source Debian Ruby
package. The way the tests are run is configured in one of the three files:
I<debian/ruby-test-files.yaml>, I<debian/ruby-tests.rake>,
I<debian/ruby-tests.rb>. See the B<FILES> section in B<dh_ruby>(1) for details.

If called without argument in the root of the source package after the package
is built and installed under debian/I<package_name>, then the tests will be run
using the files of the package installed under debian/I<package_name>. This call
is part of the B<dh_ruby>(1) sequence when building a Ruby package with gem2deb.

If the option B<--autopkgtest> is used, the package needs to be installed on
the system. B<gem2deb-test-runner> will not try to load files under debian/ and
will move away temporarily the lib/ and ext/ directory to ensure the test
suite is run against the installed package. This is used in the context of
automatic as-installed package testing, through the autopkgtest framework.

=head1 OPTIONS

=over

=item B<--autopkgtest>

Run the tests against the installed package for automatic as-installed package
testing. Useful in conjunction with B<adt-run>(1).

=item B<-c>, B<--check-dependencies>

Before running the tests, checks whether all dependencies of the package, as
declared in the Rubygems metadata, are present. Makes the program exit with a
non-zero status code (i.e. fails) if they aren't.

=item B<-b>, B<--check-bundler>

Check that the package can be correctly loaded by bundler. This tests that the
package can be loaded properly by bundler with the following two types of
Gemfiles. One lists the package as a top-level dependency:

  gem "foo"

This will be tested by calling `B<ruby -rbundler/setup>`.

The other type includes the package in a group:

  group :test do
    gem "foo"
  end

That will be tested by calling `B<ruby -rbundler -e 'Bundler.require(:test)'>`.

=back

=head1 ENVIRONMENT

=over

=item GEM2DEB_TEST_RUNNER

Used to pass options to gem2deb-test-runner via the environment. For example,
to make gem2deb-test-runner check dependencies during package build, you can
add the following to I<debian/rules>:

=over

export GEM2DEB_TEST_RUNNER = --check-dependencies

=back

=back

=head1 EXIT STATUS

=over

=item

0 if tests pass.

=item

1 if tests fail.

=item

77 if B<--autopkgtest> was passed and B<gem2deb-test-runner> cannot determine
how to run the test suite.

=back

=head1 SEE ALSO

L<B<dh_ruby>>(1), L<B<gem2deb>>(1)
