Add/Update translations
[gmpc-dynamic-playlist.git] / tests / fixture_mpd.c
blob9283feb8914448b7b2664a645542ed8e66f0b23a
1 #include "fixture_mpd.h"
2 #include <libmpd/libmpd.h>
3 #include <sys/wait.h>
4 #include "../src/defaults.h"
6 #define HOST "localhost"
7 #define PORT 1904
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))
17 failed = TRUE;
18 else if(l_out != NULL && g_pattern_match_simple("*Failed*", l_out))
19 failed = TRUE;
20 else if(l_err != NULL && g_pattern_match_simple("*Failed*", l_err))
21 failed = TRUE;
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;
33 gint result_code = 0;
34 GError* err = NULL;
36 g_spawn_sync(NULL, l_argv, NULL, 0, NULL, NULL, &std_out, &std_err, &result_code, &err);
37 if(err == NULL)
38 err = check_std(std_out, std_err, result_code);
40 if(std_out != NULL)
41 g_free(std_out);
42 if(std_err != NULL)
43 g_free(std_err);
45 g_usleep(G_USEC_PER_SEC / WAIT_FRACTION);
46 return err;
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);
57 gchar* argv[3];
58 argv[0] = MPD_BINARY;
59 argv[1] = (gchar*) l_config;
60 argv[2] = NULL;
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);
71 gchar* argv[4];
72 argv[0] = MPD_BINARY;
73 argv[1] = "--kill";
74 argv[2] = (gchar*) l_config;
75 argv[3] = NULL;
77 GError* err = spawn(argv);
78 if(!l_try)
79 g_assert_no_error(err);
80 else if(err != NULL)
81 g_error_free(err);
84 void fake_mpd_free(const gchar* l_config)
86 g_assert(connection != NULL);
88 g_assert_cmpint(mpd_disconnect(connection), ==, MPD_OK);
89 mpd_free(connection);
90 connection = NULL;
92 fake_mpd_kill(l_config, FALSE);
95 /* vim:set ts=4 sw=4: */