/* TODO: Investigate if it's worth to add this functionality as part of * the public API. I.e. a function to easily run a test case body in a * subprocess. */ void run_h_tc(atf_tc_t *tc, const char *outname, const char *errname, const char *resname) { atf_fs_path_t outpath, errpath; atf_process_stream_t outb, errb; atf_process_child_t child; atf_process_status_t status; RE(atf_fs_path_init_fmt(&outpath, outname)); RE(atf_fs_path_init_fmt(&errpath, errname)); struct run_h_tc_data data = { tc, resname }; RE(atf_process_stream_init_redirect_path(&outb, &outpath)); RE(atf_process_stream_init_redirect_path(&errb, &errpath)); RE(atf_process_fork(&child, run_h_tc_child, &outb, &errb, &data)); atf_process_stream_fini(&errb); atf_process_stream_fini(&outb); RE(atf_process_child_wait(&child, &status)); ATF_CHECK(atf_process_status_exited(&status)); atf_process_status_fini(&status); atf_fs_path_fini(&errpath); atf_fs_path_fini(&outpath); }
impl::stream_redirect_path::stream_redirect_path(const fs::path& p) { atf_error_t err = atf_process_stream_init_redirect_path(&m_sb, p.c_path()); if (atf_is_error(err)) throw_atf_error(err); m_inited = true; }
static atf_error_t init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb) { atf_error_t err; if (path == NULL) err = atf_process_stream_init_inherit(sb); else err = atf_process_stream_init_redirect_path(sb, path); return err; }
ATF_TC_BODY (stream_init_redirect_path, tc) { atf_process_stream_t sb; atf_fs_path_t path; RE (atf_fs_path_init_fmt (&path, "foo")); RE (atf_process_stream_init_redirect_path (&sb, &path)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_redirect_path); atf_process_stream_fini (&sb); atf_fs_path_fini (&path); }
static void redirect_path_stream_init (void *v) { struct redirect_path_stream *s = v; switch (s->m_base.m_type) { case stdout_type: RE (atf_fs_path_init_fmt (&s->m_path, "stdout")); break; case stderr_type: RE (atf_fs_path_init_fmt (&s->m_path, "stderr")); break; default: UNREACHABLE; } s->m_base.m_sb_ptr = &s->m_base.m_sb; RE (atf_process_stream_init_redirect_path (&s->m_base.m_sb, &s->m_path)); }
ATF_TC_BODY (exec_list, tc) { atf_fs_path_t process_helpers; atf_list_t argv; atf_process_status_t status; RE (atf_list_init (&argv)); get_process_helpers_path (tc, true, &process_helpers); atf_list_append (&argv, strdup (atf_fs_path_cstring (&process_helpers)), true); atf_list_append (&argv, strdup ("echo"), true); atf_list_append (&argv, strdup ("test-message"), true); { atf_fs_path_t outpath; atf_process_stream_t outsb; RE (atf_fs_path_init_fmt (&outpath, "stdout")); RE (atf_process_stream_init_redirect_path (&outsb, &outpath)); RE (atf_process_exec_list (&status, &process_helpers, &argv, &outsb, NULL)); atf_process_stream_fini (&outsb); atf_fs_path_fini (&outpath); } atf_list_fini (&argv); ATF_CHECK (atf_process_status_exited (&status)); ATF_CHECK_EQ (atf_process_status_exitstatus (&status), EXIT_SUCCESS); { int fd = open ("stdout", O_RDONLY); ATF_CHECK (fd != -1); check_line (fd, "test-message"); close (fd); } atf_process_status_fini (&status); atf_fs_path_fini (&process_helpers); }