]> pd.if.org Git - zpackage/commitdiff
have ctap prove read from stdin
authorNathan Wagner <nw@hydaspes.if.org>
Tue, 21 Aug 2018 11:20:40 +0000 (11:20 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Tue, 21 Aug 2018 11:20:40 +0000 (11:20 +0000)
t/ctap/prove.c

index 8ff8a6410ded3fb02a77bee17e1ad5aa3d30c853..1d870355b55602b191caad08e2dfb3e2bbfa0ea1 100644 (file)
@@ -21,6 +21,7 @@ struct result {
 
 struct testrun {
        char *name;
+       int readstdin;
        int version;
        int plan;
        int ran;
@@ -60,31 +61,35 @@ int runone(struct testrun *run) {
        size_t len = 0;
        int written = 0;
        
-       if (pipe(pipefd) == -1) {
-               perror("pipe");
-               exit(EXIT_FAILURE);
-               }
-
-       cpid = fork();
-       if (cpid == -1) {
-               perror("fork");
-               exit(EXIT_FAILURE);
-       }
+       if (!run->readstdin) {
+               if (pipe(pipefd) == -1) {
+                       perror("pipe");
+                       exit(EXIT_FAILURE);
+               }
 
-       if (cpid == 0) {
-               /* child */
-               close(pipefd[0]); /* close read end */
-               close(1); /* close stdout */
-               dup(pipefd[1]); /* set stdout to the write end of the pipe */
-               execlp(run->name, run->name, NULL);
-               /* only gets here if exec failed */
-               printf("Bail Out! exec %s failed: %s\n", run->name, strerror(errno));
-               exit(EXIT_FAILURE);
-       }
+               cpid = fork();
+               if (cpid == -1) {
+                       perror("fork");
+                       exit(EXIT_FAILURE);
+               }
 
-       close(pipefd[1]);
-       /* parent continue on */
-       tap = fdopen(pipefd[0], "r");
+               if (cpid == 0) {
+                       /* child */
+                       close(pipefd[0]); /* close read end */
+                       close(1); /* close stdout */
+                       dup(pipefd[1]); /* set stdout to the write end of the pipe */
+                       execlp(run->name, run->name, NULL);
+                       /* only gets here if exec failed */
+                       printf("Bail Out! exec %s failed: %s\n", run->name, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+
+               close(pipefd[1]);
+               /* parent continue on */
+               tap = fdopen(pipefd[0], "r");
+       } else {
+               tap = stdin;
+       }
 
        regex_t plan, diagnostic, bail, test, directive, version;
        regmatch_t match[4];
@@ -218,7 +223,9 @@ int runone(struct testrun *run) {
        regfree(&test);
 
        free(line);
-       fclose(tap);
+       if (!run->readstdin) {
+               fclose(tap);
+       }
        wait(NULL);
 
        if (run->ran < run->plan) {
@@ -251,6 +258,8 @@ int main(int ac, char *av[]) {
        struct testrun clear = { 0 };
        struct testrun *runs;
        int opt;
+       char *stdinname = "<stdin>";
+       int stdinnamelen = 0;
 
        /*
         * -j json output
@@ -266,7 +275,7 @@ int main(int ac, char *av[]) {
 
        /* TODO -jj turn off everything else? */
        /* TODO -cC color escapes */
-       while ((opt = getopt(ac, av, "jJpPiIsSdD")) != -1) {
+       while ((opt = getopt(ac, av, "jJpPiIsSdDn:")) != -1) {
                switch (opt) {
                        case 'j': json = 1; break;
                        case 'J': json = 0; break;
@@ -278,25 +287,39 @@ int main(int ac, char *av[]) {
                        case 'S': summary = 0; break;
                        case 'd': detail = 1; break;
                        case 'D': detail = 0; break;
+                       case 'n': stdinname = optarg;
+                                 break;
                        default: /* '?' */
                                fprintf(stderr, "Usage: %s [-jpisd] file...\n",
                                                av[0]);
                                exit(EXIT_FAILURE);
                }
        }
+       stdinnamelen = strlen(stdinname);
 
        runs = malloc(ac * sizeof *runs);
 
        for (i=optind; i<ac; i++) {
-               len = strlen(av[i]);
-               maxnamelen = GREATER(len, maxnamelen);
+               if (!strcmp(av[i], "-")) {
+                       maxnamelen = GREATER(stdinnamelen, maxnamelen);
+               } else {
+                       len = strlen(av[i]);
+                       maxnamelen = GREATER(len, maxnamelen);
+               }
        }
 
        for (i=optind; i<ac; i++) {
                run = clear;
-               run.name = av[i];
                run.interactive = interactive;
 
+               if (!strcmp(av[i], "-")) {
+                       run.readstdin = 1;
+                       run.name = stdinname;
+               } else {
+                       run.readstdin = 0;
+                       run.name = av[i];
+               }
+
                if (progress) {
                        printf("%s ", run.name);
                        len = strlen(run.name);