Add debugging capability at runtime.
[miniexpect.git] / test-ls-version.c
1 /* miniexpect test suite
2  * Copyright (C) 2014 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /* Note this test depends on the output of 'ls --version'.  You may
20  * have to extend the regular expressions below if running on a
21  * non-Linux OS.
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <assert.h>
30
31 #include <pcre.h>
32
33 #include "miniexpect.h"
34 #include "tests.h"
35
36 int
37 main (int argc, char *argv[])
38 {
39   mexp_h *h;
40   int status, r;
41   pcre *ls_coreutils_re;
42   pcre *ls_busybox_re;
43   const int ovecsize = 12;
44   int ovector[ovecsize];
45   const char *version;
46
47   ls_coreutils_re = test_compile_re ("^ls.* ([.\\d]+)");
48   /* Busybox doesn't actually recognize the --version option, but
49    * it prints out the version string in its error message.
50    */
51   ls_busybox_re = test_compile_re ("^BusyBox v([.\\d+]) ");
52
53   h = mexp_spawnl ("ls", "ls", "--version", NULL);
54   assert (h != NULL);
55
56   switch (mexp_expect (h,
57                        (mexp_regexp[]) {
58                          { 100, ls_coreutils_re },
59                          { 101, ls_busybox_re },
60                          { 0 },
61                        }, ovector, ovecsize)) {
62   case 100:
63   case 101:
64     /* Get the matched version number. */
65     r = pcre_get_substring (h->buffer, ovector,
66                             mexp_get_pcre_error (h), 1, &version);
67     if (r < 0) {
68       fprintf (stderr, "error: PCRE error reading version substring: %d\n",
69                r);
70       exit (EXIT_FAILURE);
71     }
72     printf ("ls version = %s\n", version);
73     pcre_free_substring (version);
74     break;
75   case MEXP_EOF:
76     fprintf (stderr, "error: EOF before matching version string\n");
77     exit (EXIT_FAILURE);
78   case MEXP_TIMEOUT:
79     fprintf (stderr, "error: timeout before matching version string\n");
80     exit (EXIT_FAILURE);
81   case MEXP_ERROR:
82     perror ("mexp_expect");
83     exit (EXIT_FAILURE);
84   case MEXP_PCRE_ERROR:
85     fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h));
86     exit (EXIT_FAILURE);
87   }
88
89   status = mexp_close (h);
90   if (status != 0 && !test_is_sighup (status)) {
91     fprintf (stderr, "%s: non-zero exit status from subcommand: ", argv[0]);
92     test_diagnose (status);
93     fprintf (stderr, "\n");
94     exit (EXIT_FAILURE);
95   }
96
97   pcre_free (ls_coreutils_re);
98   pcre_free (ls_busybox_re);
99
100   exit (EXIT_SUCCESS);
101 }