#!/usr/bin/ruby

# 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(
    '-d', '--check-dependencies-only',
    "Check dependencies, and don't run tests"
  ) do
    options[:check_dependencies] = true
    options[:check_dependencies_only] = 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.

If you want to check dependencies during the build, you can add something like
this to debian/rules:

=item B<-d>, B<--check-dependencies-only>

Exit after checking dependencies, i.e. only check dependencies, and don't run
any tests. Implies B<--check-dependencies>.

=over

export GEM2DEB_TEST_RUNNER = --check-dependencies

=back

=back

=head1 ENVIRONMENT

=over

=item GEM2DEB_TEST_RUNNER

Used to pass options to gem2deb-test-runner via the environment.

=back

=head1 SEE ALSO

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