2 * Copyright (C) 2012 Colin Watson.
4 * This file is part of libpipeline.
6 * libpipeline is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * libpipeline is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with libpipeline; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
29 #include "full-write.h"
31 #include "xvasprintf.h"
35 /* Include private definitions so that we can inspect redirections. */
36 #include "pipeline-private.h"
38 const char *program_name = "pump";
40 static void assert_files_equal (const char *left, const char *right)
42 pipeline *diff = pipeline_new_command_args ("diff", "-u", left, right,
44 ck_assert_int_eq (pipeline_run (diff), 0);
47 START_TEST (test_pump_connect_attaches_correctly)
49 pipeline *one = pipeline_new ();
50 pipeline *two = pipeline_new ();
51 pipeline *three = pipeline_new ();
53 pipeline_connect (one, two, three, (void *) 0);
54 ck_assert_int_eq (one->redirect_out, REDIRECT_FD);
55 ck_assert_int_le (one->want_out, 0);
56 ck_assert_ptr_eq (one->want_outfile, NULL);
57 ck_assert_ptr_eq (two->source, one);
58 ck_assert_int_eq (two->redirect_in, REDIRECT_FD);
59 ck_assert_int_le (two->want_in, 0);
60 ck_assert_ptr_eq (two->want_infile, NULL);
61 ck_assert_ptr_eq (three->source, one);
62 ck_assert_int_eq (three->redirect_in, REDIRECT_FD);
63 ck_assert_int_le (three->want_in, 0);
64 ck_assert_ptr_eq (three->want_infile, NULL);
66 pipeline_free (three);
72 static void tee_source (void *data _GL_UNUSED)
74 unsigned char buf[256];
77 for (i = 0; i < 256; ++i)
78 buf[i] = (unsigned char) i;
80 for (i = 0; i < 4096; ++i)
81 full_write (fileno (stdout), buf, 256);
84 START_TEST (test_pump_tee)
86 pipeline *source, *sink_process, *sink_function;
87 char *process_outfile, *function_outfile;
89 source = pipeline_new ();
90 pipeline_command (source, pipecmd_new_function ("source", tee_source,
92 sink_process = pipeline_new_command_args ("cat", (void *) 0);
93 process_outfile = xasprintf ("%s/process", temp_dir);
94 pipeline_want_outfile (sink_process, process_outfile);
95 sink_function = pipeline_new ();
96 pipeline_command (sink_function, pipecmd_new_passthrough ());
97 function_outfile = xasprintf ("%s/function", temp_dir);
98 pipeline_want_outfile (sink_function, function_outfile);
99 pipeline_connect (source, sink_process, sink_function, (void *) 0);
100 pipeline_pump (source, sink_process, sink_function, (void *) 0);
101 ck_assert_int_eq (pipeline_wait (source), 0);
102 ck_assert_int_eq (pipeline_wait (sink_process), 0);
103 ck_assert_int_eq (pipeline_wait (sink_function), 0);
104 assert_files_equal (process_outfile, function_outfile);
106 free (function_outfile);
107 free (process_outfile);
108 pipeline_free (sink_function);
109 pipeline_free (sink_process);
110 pipeline_free (source);
114 static Suite *pump_suite (void)
116 Suite *s = suite_create ("Pump");
118 TEST_CASE (s, pump, connect_attaches_correctly);
119 TEST_CASE_WITH_FIXTURE (s, pump, tee, temp_dir_setup,