This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
testmod_python.c
148 lines (115 loc) · 5.01 KB
/
testmod_python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbmacros.h>
#include <stdlib.h>
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <tests_plugin.h>
static void init_env (void)
{
setenv ("PYTHONDONTWRITEBYTECODE", "1", 1);
}
// test simple variable passing
static void test_variable_passing (void)
{
printf ("Testing simple variable passing...\n");
KeySet * conf = ksNew (1, keyNew ("user:/script", KEY_VALUE, srcdir_file ("python/python_plugin.py"), KEY_END),
keyNew ("user:/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user:/print", KEY_END),
keyNew ("user:/python/path", KEY_VALUE, ".", KEY_END), KS_END);
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user:/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
exit_if_fail (ksGetSize (ks) == 1, "keyset size is still 0");
succeed_if_same_string (keyName (ksAtCursor (ks, 0)), "user:/from_python");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
// test loading python twice
static void test_two_scripts (void)
{
printf ("Testing loading of two active python plugins...\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf = ksNew (2, keyNew ("user:/script", KEY_VALUE, srcdir_file ("python/python_plugin.py"), KEY_END),
keyNew ("user:/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user:/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user:/print", KEY_END), KS_END);
KeySet * conf2 = ksNew (2, keyNew ("user:/script", KEY_VALUE, srcdir_file ("python/python_plugin2.py"), KEY_END),
keyNew ("user:/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user:/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user:/print", KEY_END), KS_END);
Key * errorKey = keyNew ("/", KEY_END);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (output_warnings (errorKey), "warnings in kdbOpen");
succeed_if (output_error (errorKey), "errors in kdbOpen");
exit_if_fail (plugin != NULL, "unable to load python plugin");
keyDel (errorKey);
Key * errorKey2 = keyNew ("/", KEY_END);
Plugin * plugin2 = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf2, errorKey2);
succeed_if (output_warnings (errorKey2), "warnings in kdbOpen");
succeed_if (output_error (errorKey2), "errors in kdbOpen");
exit_if_fail (plugin2 != NULL, "unable to load python plugin again");
keyDel (errorKey2);
elektraPluginClose (plugin2, 0);
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
// simple return value test
static void test_fail (void)
{
printf ("Testing return values from python functions...\n");
KeySet * conf = ksNew (2, keyNew ("user:/script", KEY_VALUE, srcdir_file ("python/python_plugin_fail.py"), KEY_END),
keyNew ("user:/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user:/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user:/print", KEY_END), KS_END);
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user:/tests/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == -1, "call to kdbGet didn't fail");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == -1, "call to kdbSet didn't fail");
succeed_if (plugin->kdbError (plugin, ks, parentKey) == -1, "call to kdbError didn't fail");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
// test script with wrong class name
static void test_wrong (void)
{
printf ("Testing python script with wrong class name...\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf = ksNew (2, keyNew ("user:/script", KEY_VALUE, srcdir_file ("python/python_plugin_wrong.py"), KEY_END),
keyNew ("user:/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user:/python/path", KEY_VALUE, ".", KEY_END),
keyNew ("user:/print", KEY_END), KS_END);
Key * errorKey = keyNew ("/", KEY_END);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (!output_warnings (errorKey), "we expect some warnings");
succeed_if (!output_error (errorKey), "we expect some errors");
succeed_if (plugin == NULL, "python plugin shouldn't be loadable");
keyDel (errorKey);
elektraModulesClose (modules, 0);
ksDel (modules);
}
int main (int argc, char ** argv)
{
printf ("PYTHON TESTS\n");
printf ("==================\n\n");
init (argc, argv);
init_env ();
test_variable_passing ();
test_two_scripts ();
printf ("\n");
printf ("========================================================================\n");
printf ("NOTE: The following errors are intended. We're testing error conditions!\n");
printf ("========================================================================\n");
test_fail ();
test_wrong ();
print_result ("test_python");
return nbError;
}