summaryrefslogtreecommitdiff
path: root/dh_elpa_test
blob: 2b0074eb99ab152b56654fc064575b057433ae70 (plain)
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
#!/usr/bin/perl

=head1 NAME

dh_elpa_test - run ELPA package testsuites

=cut

use strict;
use warnings;

=head1 SYNOPSIS

B<dh_elpa_test> [S<I<debhelper options>>]  [S<I<pkg-file>>]

=head1 DESCRIPTION

B<dh_elpa_test> is a debhelper program that is responsible for running
the testsuites of ELPA packages, when those test suites use ERT or
buttercup(1).  dh_auto_test is rarely suitable.

Testing with buttercup(1) will be activated if the package
build-depends on elpa-buttercup.  Testing with ERT will be activated
if ERT test files can be found.

=head1 ENVIRONMENT VARIABLES

B<dh_elpa_test> will respond to the following variables set, for
example, at the top of your debian/rules file.  Do not surround
variable values with double-quotation marks.  E.g.

=over 4

    export DH_ELPA_TEST_BUTTERCUP_LOAD_PATH=lib,other_lib

=back

=over 4

=item B<DH_ELPA_TEST_DISABLE>

If this variable is set, dh_elpa(1) will not invoke B<dh_elpa_test>.

=item B<DH_ELPA_TEST_BUTTERCUP_LOAD_PATH>

A comma-separated list of directories to add to the load-path when
buttercup(1) invokes Emacs.  Will be passed to buttercup(1) with its
B<-L> command line argument.

=item B<DH_ELPA_TEST_BUTTERCUP_PATTERNS>

A comma-separated list of Emacs regular expressions (not containing
any commas) jointly matching all and only the files containing
Buttercup tests that you wish to run.  If this variable is not
defined, all tests that can be found will be run.  Will be passed to
buttercup(1) with its B<-p> command line argument.

=item B<DH_ELPA_TEST_ERT_EXCLUDE>

A comma-separated list of file globs matching files containing ERT
tests that should not be run.

=item B<DH_ELPA_TEST_ERT_HELPER>

The name of a *.el file containing Emacs Lisp code that will run the
ERT test suite.  When this variable is not defined, B<dh_elpa_test>
calls the function B<(ert-run-tests-batch-and-exit)>.

Note that this is not the way to load test helpers that do needed work
but don't actually run the tests.  For that, you can use something like

=over 4

    export DH_ELPA_TEST_ERT_EVAL=(load-file "test-helper.el")

=back

=item B<DH_ELPA_TEST_ERT_EVAL>

Emacs Lisp code to be run prior to running ERT tests by the Emacs
instance spawned by B<dh_elpa_test> to run those tests.

=item B<DH_ELPA_TEST_ERT_LOAD_PATH>

A comma-separated list of directories to add to the load-path when
B<dh_elpa_test> invokes Emacs to run ERT tests.

=back

=cut

use Text::Glob qw{ match_glob };
use Array::Utils qw{ array_minus };

use Debian::Debhelper::Dh_Lib;
use Debian::Control;

if (get_buildoption("nocheck")) {
	exit 0;
}

my $control = Debian::Control->new();
$control->read("debian/control");

# these are from dh_elpa
# TODO have dh_elpa export them so we can import them, instead of copy/paste
my $dhelpadir="/usr/share/emacs/site-lisp/elpa";
my $elpadir="/usr/share/emacs/site-lisp/elpa-src";

# ---- Buttercup

if ($control->source->Build_Depends->has( "elpa-buttercup" )) {
    my @args = qw{ buttercup -L . };
    if (defined $ENV{'DH_ELPA_TEST_BUTTERCUP_LOAD_PATH'}) {
        foreach my $dir (split(',', $ENV{'DH_ELPA_TEST_BUTTERCUP_LOAD_PATH'})) {
            push @args, ('-L', "$dir");
        }
    }
    if (defined $ENV{'DH_ELPA_TEST_BUTTERCUP_PATTERNS'}) {
        foreach my $pattern (split(',', $ENV{'DH_ELPA_TEST_BUTTERCUP_PATTERNS'})) {
            push @args, ('-p', "$pattern");
        }
    }
    print_and_doit(@args);
}

# ---- ERT

$Text::Glob::strict_wildcard_slash = 0;
my @ert_files = map { s|^\./||r }
  split("\n", `grep -lr "ert-deftest" . --exclude-dir=.pc --exclude-dir=debian`);
if (defined $ENV{'DH_ELPA_TEST_ERT_EXCLUDE'}) {
    foreach my $glob (split(',', $ENV{'DH_ELPA_TEST_ERT_EXCLUDE'})) {
        if ( my @matches = match_glob( $glob, @ert_files ) ) {
            @ert_files = array_minus( @ert_files, @matches );
        }
    }
}

if (@ert_files) {
    my @args = qw{ emacs -batch -Q -l package };
    push @args, ("--eval", "(add-to-list 'package-directory-list \"$dhelpadir\")");
    push @args, ("--eval", "(add-to-list 'package-directory-list \"$elpadir\")");
    push @args, ("-f", "package-initialize");

    # work around the fact that s-el and dash-el are not packaged with
    # dh_elpa so aren't in $dhelpadir
    push @args, ("-L", "/usr/share/emacs/site-lisp/s-el")
      if ($control->source->Build_Depends->has( "s-el" ));
    push @args, ("-L", "/usr/share/emacs/site-lisp/dash-el")
      if ($control->source->Build_Depends->has( "dash-el" ));

    # add the user's load-path entries
    if (defined $ENV{'DH_ELPA_TEST_ERT_LOAD_PATH'}) {
        foreach my $dir (split(',', $ENV{'DH_ELPA_TEST_ERT_LOAD_PATH'})) {
            push @args, ('-L', "$dir");
        }
    }

    # make some guesses about where stuff that needs to be in
    # load-path will be
    push @args, ("-L", ".");
    push @args, ("-L", "test") if ( -d "test" );
    push @args, ("-L", "tests") if ( -d "tests" );
    # TODO maybe we should just add all dirs containing files in @ert_files?

    # now finish adding the user's stuff
    push @args, ("--eval", "$ENV{'DH_ELPA_TEST_ERT_EVAL'}")
      if (defined $ENV{'DH_ELPA_TEST_ERT_EVAL'});
    foreach my $ert_file (@ert_files) {
        push @args, ("-l", "$ert_file");
    }
    if (defined $ENV{'DH_ELPA_TEST_ERT_HELPER'}) {
        push @args, ("-l", "$ENV{'DH_ELPA_TEST_ERT_HELPER'}");
    } else {
        push @args, ("--eval", "(ert-run-tests-batch-and-exit)");
    }

    print_and_doit(@args);
}

=head1 EXAMPLES

Here is an example of using the helper in a dh(1) style debian/rules

=over 4

    #!/usr/bin/make -f

    export DH_ELPA_TEST_BUTTERCUP_LOAD_PATH=lib,other_lib

    %:
        dh $@ --with elpa

=back

=cut