Blame support/support_capture_subprocess_check.c

Packit 6c4009
/* Verify capture output from a subprocess.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <support/capture_subprocess.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_context (const char *context, bool *failed)
Packit 6c4009
{
Packit 6c4009
  if (*failed)
Packit 6c4009
    /* Do not duplicate message.  */
Packit 6c4009
    return;
Packit 6c4009
  support_record_failure ();
Packit 6c4009
  printf ("error: subprocess failed: %s\n", context);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_capture_subprocess_check (struct support_capture_subprocess *proc,
Packit 6c4009
                                  const char *context, int status,
Packit 6c4009
                                  int allowed)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY ((allowed & sc_allow_none)
Packit 6c4009
               || (allowed & sc_allow_stdout)
Packit 6c4009
               || (allowed & sc_allow_stderr));
Packit 6c4009
  TEST_VERIFY (!((allowed & sc_allow_none)
Packit 6c4009
                 && ((allowed & sc_allow_stdout)
Packit 6c4009
                     || (allowed & sc_allow_stderr))));
Packit 6c4009
Packit 6c4009
  bool failed = false;
Packit 6c4009
  if (proc->status != status)
Packit 6c4009
    {
Packit 6c4009
      print_context (context, &failed);
Packit 6c4009
      printf ("error:   expected exit status: %d\n", status);
Packit 6c4009
      printf ("error:   actual exit status:   %d\n", proc->status);
Packit 6c4009
    }
Packit 6c4009
  if (!(allowed & sc_allow_stdout) && proc->out.length != 0)
Packit 6c4009
    {
Packit 6c4009
      print_context (context, &failed);
Packit 6c4009
      printf ("error:   unexpected output from subprocess\n");
Packit 6c4009
      fwrite (proc->out.buffer, proc->out.length, 1, stdout);
Packit 6c4009
      puts ("\n");
Packit 6c4009
    }
Packit 6c4009
  if (!(allowed & sc_allow_stderr) && proc->err.length != 0)
Packit 6c4009
    {
Packit 6c4009
      print_context (context, &failed);
Packit 6c4009
      printf ("error:   unexpected error output from subprocess\n");
Packit 6c4009
      fwrite (proc->err.buffer, proc->err.length, 1, stdout);
Packit 6c4009
      puts ("\n");
Packit 6c4009
    }
Packit 6c4009
}