1 #include "fixture_mpd.h"
2 #include <libmpd/libmpd.h>
4 #include "../src/defaults.h"
6 #define HOST "localhost"
8 #define WAIT_FRACTION 20
10 MpdObj
* connection
= NULL
;
12 static GError
* check_std(gchar
* l_out
, gchar
* l_err
, gint l_result_code
)
14 gboolean failed
= FALSE
;
16 if(!WIFEXITED(l_result_code
))
18 else if(l_out
!= NULL
&& g_pattern_match_simple("*Failed*", l_out
))
20 else if(l_err
!= NULL
&& g_pattern_match_simple("*Failed*", l_err
))
23 return failed
? g_error_new(G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
, "stdout: %s | stderr: %s", l_out
, l_err
) : NULL
;
26 static GError
* spawn(gchar
** l_argv
)
28 g_assert(l_argv
!= NULL
);
29 g_usleep(G_USEC_PER_SEC
/ WAIT_FRACTION
);
31 gchar
* std_out
= NULL
;
32 gchar
* std_err
= NULL
;
36 g_spawn_sync(NULL
, l_argv
, NULL
, 0, NULL
, NULL
, &std_out
, &std_err
, &result_code
, &err
);
38 err
= check_std(std_out
, std_err
, result_code
);
45 g_usleep(G_USEC_PER_SEC
/ WAIT_FRACTION
);
49 void fake_mpd_init(const gchar
* l_config
)
51 g_assert(l_config
!= NULL
);
52 g_assert(connection
== NULL
);
54 /* try to kill previous mpd */
55 fake_mpd_kill(l_config
, TRUE
);
59 argv
[1] = (gchar
*) l_config
;
62 g_assert_no_error(spawn(argv
));
63 connection
= mpd_new(HOST
, PORT
, NULL
);
64 g_assert_cmpint(mpd_connect(connection
), ==, MPD_OK
);
67 void fake_mpd_kill(const gchar
* l_config
, gboolean l_try
)
69 g_assert(l_config
!= NULL
);
74 argv
[2] = (gchar
*) l_config
;
77 GError
* err
= spawn(argv
);
79 g_assert_no_error(err
);
84 void fake_mpd_free(const gchar
* l_config
)
86 g_assert(connection
!= NULL
);
88 g_assert_cmpint(mpd_disconnect(connection
), ==, MPD_OK
);
92 fake_mpd_kill(l_config
, FALSE
);
95 /* vim:set ts=4 sw=4: */