From 8095ddfb9479b5fff4d447fab9d9eaef50bc83a3 Mon Sep 17 00:00:00 2001 From: Florian Lindner Date: Wed, 12 Jul 2023 02:47:21 +0200 Subject: [PATCH 1/4] Plugins: Improve c plugin (doc, tests, refactor) - some small refactoring (e.g. remove unnecessary dups, reduce scope of variables) - add code documentation (Doxygen) - extended README.md - add unit tests --- doc/CONTRACT.ini | 2 +- doc/news/_preparation_next_release.md | 8 +- src/plugins/c/CMakeLists.txt | 3 +- src/plugins/c/README.md | 49 +++++++++++- src/plugins/c/c.c | 105 ++++++++++++++++--------- src/plugins/c/c.h | 5 -- src/plugins/c/testmod_c.c | 109 ++++++++++++++++++++++++++ 7 files changed, 230 insertions(+), 51 deletions(-) create mode 100644 src/plugins/c/testmod_c.c diff --git a/doc/CONTRACT.ini b/doc/CONTRACT.ini index 136862a1f21..c46a36fad46 100644 --- a/doc/CONTRACT.ini +++ b/doc/CONTRACT.ini @@ -287,7 +287,7 @@ type = vector {"default", 64000}, ; will be added by the build system for KDB_DEFAULT_STORAGE and KDB_DEFAULT_RESOLVER {"recommended", 32000}, ; in case of doubt, use this plugin {"productive", 8000}, ; actively used in productive environments, not only by maintainer - {"maintained", 4000}, ; actively used and fixed by maintainer (infos/author) + {"maintained", 4000}, ; actively used and fixed by maintainer (infos/maintainer) {"reviewed", 4000}, ; actively reviewed on every change and not by maintainer {"conformant", 2000}, ; to indicate that e.g. a storage plugin fulfils all requirements of a storage plugin {"compatible", 2000}, ; to indicate it will be compatible with its later versions diff --git a/doc/news/_preparation_next_release.md b/doc/news/_preparation_next_release.md index db96da5dcc9..fa16a775ebd 100644 --- a/doc/news/_preparation_next_release.md +++ b/doc/news/_preparation_next_release.md @@ -47,7 +47,7 @@ docker run -it elektra/elektra - <> - <> - New Changetracking API -- ODBC Backend _(Florian Lindner @flo91)_ +- ODBC Backend - <> ### <> @@ -152,6 +152,12 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu - <> - <> +### c + +- Improve the c plugin: some refactoring, add documentation, extend README.md, add unit tests. _(Florian Lindner @flo91)_ +- <> +- <> + ### <> - <> diff --git a/src/plugins/c/CMakeLists.txt b/src/plugins/c/CMakeLists.txt index 598df3aa86b..cdba68d9cb4 100644 --- a/src/plugins/c/CMakeLists.txt +++ b/src/plugins/c/CMakeLists.txt @@ -2,4 +2,5 @@ include (LibAddMacros) add_plugin ( c - SOURCES c.h c.c COMPONENT libelektra${SO_VERSION}) + SOURCES c.h c.c COMPONENT libelektra${SO_VERSION} + ADD_TEST COMPONENT libelektra${SO_VERSION}) diff --git a/src/plugins/c/README.md b/src/plugins/c/README.md index 25ed89f2988..b482ac18b99 100644 --- a/src/plugins/c/README.md +++ b/src/plugins/c/README.md @@ -6,15 +6,56 @@ - infos/provides = storage/c - infos/recommends = - infos/placements = getstorage setstorage -- infos/status = maintained nodep libc writeonly preview nodoc +- infos/status = maintained reviewed unittest nodep libc writeonly preview - infos/metadata = -- infos/description = C-struct exports for Elektra +- infos/description = C code KeySet exports for Elektra ## Usage -Export Elektra’s C-structs (e.g. `ksNew(.. keyNew(`). This is -useful for generating test data, e.g.: +Export a subset of the KDB as C code which creates a KeySet (`ksNew(, keyNew(...), ...)`). +This can be useful for generating test data or help to generate code for already defined configuration data. ```sh kdb export user:/testdata c ``` + +The output can be used in C code for generating KeySets and the keys inside them. +So one use case is to extract a part of the KDB and copy the generated code +to create the exported part of the KDB programmatically, e.g. for another Elektra installation. + +It can also be useful if you develop your own application and first create the necessary +keys manually. Then you can export them from the KDB and just paste the generated code +in the right place of the codebase of your application. + +> Please note that if you use this plugin for creating a mountpoint (e.g. by calling `kdb mount c`), +> only the content written by the last `kdbSet (...)` which includes the mountpoint will be inside the file. +> If you call `kdbSet (...)` for such a mountpoint, the previous content of the file is erased. +> +> This is because the c plugin is implemented as a write-only plugin. +> It's recommended to only use it for exporting parts of the KDB by calling `kdb export `. + +## Example + +In this example, we add some keys and metakeys to the KDB and export them with the c plugin. +The output can directly be used inside C source code which uses Elektra. + +```sh +kdb set user:/tests/cplugin/key1 value1 +#> Create a new key user:/tests/cplugin/key1 with string "value1" + +kdb set user:/tests/cplugin/key2 value2 +#> Create a new key user:/tests/cplugin/key2 with string "value2" + +kdb meta-set user:/tests/cplugin/key2 metakey2.1 metaval2.1 +kdb set user:/tests/cplugin/key2 metakey2.2 metaval2.2 + +kdb set user:/tests/cplugin/key3 value3 +#> Create a new key user:/tests/cplugin/key3 with string "value3" + +bin/kdb export user:/tests/cplugin c +#> ksNew (3, +#> keyNew ("user:/tests/cplugin/key1", KEY_VALUE, "value1", KEY_END), +#> keyNew ("user:/tests/cplugin/key2", KEY_VALUE, "value2", KEY_META, "metakey2.1", "metaval2.1", KEY_META, "metakey2.2", "metaval2.2", KEY_END), +#> keyNew ("user:/tests/cplugin/key3", KEY_VALUE, "value3", KEY_END), +#> KS_END); +``` diff --git a/src/plugins/c/c.c b/src/plugins/c/c.c index d3679262f6f..d4e219c02f5 100644 --- a/src/plugins/c/c.c +++ b/src/plugins/c/c.c @@ -20,6 +20,18 @@ static const char * const escapes = "\"'\\?nrt"; static const char * const hex = "0123456789abcdef"; +/** + * @internal + * + * @brief The function escapes the string @p str and replaces non-printable characters with their HEX values. + * + * @param str The string to escape + * Please note that the given pointer points to a different memory area after the function was executed. + * New memory is allocated for the escaped string and *str points to it then. + * The memory for the old string is freed by this function. + * You must free the returned string. + * @return The escaped string. (the same pointer as @p *str is set to) + */ static char * escapeString (char ** str) { size_t size = 0; @@ -36,7 +48,7 @@ static char * escapeString (char ** str) ++size; } else - { + { /* convert to hex value, needs 4 chars */ size += 4; } } @@ -55,6 +67,7 @@ static char * escapeString (char ** str) if (e != NULL) { + /* add '\' and escaped character */ char escaped = escapes[e - toEscape]; *newCur = '\\'; ++newCur; @@ -63,11 +76,13 @@ static char * escapeString (char ** str) } else if (isprint (c)) { + /* just copy the printable character */ *newCur = c; ++newCur; } else { + /* convert the character to a HEX value */ *newCur = '\\'; ++newCur; *newCur = 'x'; @@ -85,17 +100,22 @@ static char * escapeString (char ** str) } /** - * Generate a C-Style key and stream it. * - * This keyset can be used to include as c-code for - * applikations using elektra. + * @internal + * + * @brief Generate a C-style key and stream it. + * + * The result can be included in C-code for applications using Elektra. + * + * @param key The key to work with + * @param stream The file pointer where to send the stream to * - * @param key the key object to work with - * @param stream the file pointer where to send the stream * @retval 1 on success + * @retval -1 on error + * * @ingroup stream */ -int keyGenerate (const Key * key, FILE * stream) +static int keyGenerate (const Key * key, FILE * stream) { size_t n = keyGetNameSize (key); if (n > 1) @@ -106,7 +126,7 @@ int keyGenerate (const Key * key, FILE * stream) fprintf (stream, "\tkeyNew (\"%s\"", escapeString (&nam)); elektraFree (nam); } - else if (n == 1) + else if (n == 1) /* size 1 because of \0 */ { fprintf (stream, "\tkeyNew(\"\""); } @@ -129,13 +149,15 @@ int keyGenerate (const Key * key, FILE * stream) elektraFree (str); } - const Key * meta; + /* Dup key because keyMeta() needs a non-const key */ Key * dup = keyDup (key, KEY_CP_ALL); KeySet * metaKeys = keyMeta (dup); for (elektraCursor it = 0; it < ksGetSize (metaKeys); ++it) { - meta = ksAtCursor (metaKeys, it); + const Key * meta = ksAtCursor (metaKeys, it); + + /* Dup the key-name without "meta:/" prefix (remove namespace) */ char * metaName = elektraStrDup (keyName (meta) + sizeof ("meta:/") - 1); char * metaStr = elektraStrDup (keyString (meta)); fprintf (stream, ", KEY_META, \"%s\", \"%s\"", escapeString (&metaName), escapeString (&metaStr)); @@ -150,37 +172,42 @@ int keyGenerate (const Key * key, FILE * stream) /** - * Generate a C-Style keyset and stream it. + * @internal * - * This keyset can be used to include as c-code for - * applikations using elektra. + * Generate a C-Style KeySet and stream it. + * + * The result can be included in C-code for applications using Elektra. + * + * @param ks The KeySet to work with + * @param stream The file pointer where to send the stream to * - * @param ks the keyset to work with - * @param stream the file pointer where to send the stream * @retval 1 on success + * @retval -1 on error + * * @ingroup stream */ -int ksGenerate (const KeySet * ks, FILE * stream) +static int ksGenerate (const KeySet * ks, FILE * stream) { - Key * key; - KeySet * cks = ksDup (ks); + fprintf (stream, "ksNew (%d,\n", (int) ksGetSize (ks)); - fprintf (stream, "ksNew (%d,\n", (int) ksGetSize (cks)); - - for (elektraCursor it = 0; it < ksGetSize (cks); ++it) + for (elektraCursor it = 0; it < ksGetSize (ks); ++it) { - key = ksAtCursor (cks, it); + Key * key = ksAtCursor (ks, it); keyGenerate (key, stream); fprintf (stream, ",\n"); } fprintf (stream, "\tKS_END);\n"); - ksDel (cks); return 1; } -int elektraCGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED) +/** + * @brief The get function of this plugin is only used to define the contract. + * + * @return 1 + */ +int elektraCGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey) { if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/c")) { @@ -188,7 +215,6 @@ int elektraCGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSE keyNew ("system:/elektra/modules/c/exports", KEY_END), keyNew ("system:/elektra/modules/c/exports/get", KEY_FUNC, elektraCGet, KEY_END), keyNew ("system:/elektra/modules/c/exports/set", KEY_FUNC, elektraCSet, KEY_END), - keyNew ("system:/elektra/modules/c/exports/checkconf", KEY_FUNC, elektraCCheckConf, KEY_END), #include ELEKTRA_README keyNew ("system:/elektra/modules/c/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END); ksAppend (returned, contract); @@ -196,12 +222,25 @@ int elektraCGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSE return 1; // success } - // get all keys + // no action if no contract is requested (write-only plugin) return 1; // success } -int elektraCSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED) +/** + * @brief Generate the C-code and put it in the file whose name is defined as the value of @p parentKey. + * + * @post The given KeySet and its keys are converted into C-code that can be used with Elektra + * and the generated code is written into the file defined by the string-value of parent key. + * Please note that any existing content in the given file is deleted. + * + * @param returned The KeySet for which the C-code should be generated + * @param parentKey The value of the key is the name of the file where the generated code gets written to + * + * @retval 1 on success + * @retval -1 on error + */ +int elektraCSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey) { FILE * fp = fopen (keyString (parentKey), "w"); @@ -217,18 +256,6 @@ int elektraCSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSE return 1; // success } -int elektraCCheckConf (Key * errorKey ELEKTRA_UNUSED, KeySet * conf ELEKTRA_UNUSED) -{ - // validate plugin configuration - // this function is optional - - // the return codes have the following meaning: - // 0: The configuration was OK and has not been changed - // 1: The configuration has been changed and now it is OK - // -1: The configuration was not OK and could not be fixed. An error has to be set to errorKey. - return 0; -} - Plugin * ELEKTRA_PLUGIN_EXPORT { // clang-format off diff --git a/src/plugins/c/c.h b/src/plugins/c/c.h index fbe37fbb074..57c12823d5a 100644 --- a/src/plugins/c/c.h +++ b/src/plugins/c/c.h @@ -12,13 +12,8 @@ #include - -int elektraCOpen (Plugin * handle, Key * errorKey); -int elektraCClose (Plugin * handle, Key * errorKey); int elektraCGet (Plugin * handle, KeySet * ks, Key * parentKey); int elektraCSet (Plugin * handle, KeySet * ks, Key * parentKey); -int elektraCError (Plugin * handle, KeySet * ks, Key * parentKey); -int elektraCCheckConf (Key * errorKey, KeySet * conf); Plugin * ELEKTRA_PLUGIN_EXPORT; diff --git a/src/plugins/c/testmod_c.c b/src/plugins/c/testmod_c.c new file mode 100644 index 00000000000..d289f058b69 --- /dev/null +++ b/src/plugins/c/testmod_c.c @@ -0,0 +1,109 @@ +/** + * @file + * + * @brief Tests for the c plugin + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + * + */ + +#include + +static void test_contract (void) +{ + printf ("test contract\n"); + + KeySet * conf = ksNew (0, KS_END); + KeySet * contract = ksNew (0, KS_END); + Key * parentKey = keyNew ("system:/elektra/modules/c", KEY_END); + + PLUGIN_OPEN ("c"); + succeed_if (plugin->kdbGet (plugin, contract, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Could not retrieve plugin contract"); + PLUGIN_CLOSE (); + + succeed_if (ksGetSize (contract) > 0, "Empty KeySet was returned for plugin contract"); + succeed_if (ksLookup (contract, parentKey, KDB_O_NONE), "The key with the name 'system:/elektra/modules/c' was not part of the plugin contract"); + + Key * keyExportsSet = ksLookupByName (contract, "system:/elektra/modules/c/exports/set", KDB_O_NONE); + succeed_if (keyExportsSet, "The set-function was not part of the plugin contract (key 'system:/elektra/modules/c/exports/set' not found)"); + succeed_if (keyValue (keyExportsSet), "The set function retrieved as part of the plugin contract was NULL"); +} + +static void test_set_empty (void) +{ + printf ("test set with empty KeySet\n"); + + KeySet * conf = ksNew (0, KS_END); + PLUGIN_OPEN ("c"); + + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + KeySet * ksEmpty = ksNew (0, KS_END); + succeed_if (plugin->kdbSet (plugin, ksEmpty, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write empty KeySet to file"); + ksDel (ksEmpty); + + PLUGIN_CLOSE(); +} + +static void test_set_simple (void) +{ + printf ("test set with simple KeySet\n"); + + KeySet * conf = ksNew (0, KS_END); + PLUGIN_OPEN ("c"); + + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + KeySet * ksTest = ksNew (0, KS_END); + ksAppendKey (ksTest, keyNew ("user:/tests/c/key1", KEY_VALUE, "value1", KEY_END)); + ksAppendKey (ksTest, keyNew ("user:/tests/c/key2", KEY_VALUE, "value2", KEY_END)); + ksAppendKey (ksTest, keyNew ("user:/tests/c/key3", KEY_VALUE, "value3", KEY_END)); + + succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write non-empty KeySet to file"); + ksDel (ksTest); + + PLUGIN_CLOSE(); +} + +static void test_set_meta (void) +{ + printf ("test set with simple KeySet\n"); + + KeySet * conf = ksNew (0, KS_END); + PLUGIN_OPEN ("c"); + + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + KeySet * ksTest = ksNew (0, KS_END); + + Key * k1 = keyNew ("user:/tests/c/key1", KEY_VALUE, "value1", KEY_END); + Key * k2 = keyNew ("user:/tests/c/key2", KEY_VALUE, "value2", KEY_END); + Key * k3 = keyNew ("user:/tests/c/key3", KEY_VALUE, "value3", KEY_END); + + keySetMeta (k1, "metakey 1.1", "metavalue 1.1"); + keySetMeta (k3, "metakey 3.1", "metavalue 3.1"); + keySetMeta (k3, "metakey 3.2", "metavalue 3.2"); + keySetMeta (k3, "metakey 3.3", "metavalue 3.3"); + + ksAppendKey (ksTest, k1); + ksAppendKey (ksTest, k2); + ksAppendKey (ksTest, k3); + + succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write KeySet with keys containing metadata to file"); + ksDel (ksTest); + + PLUGIN_CLOSE(); +} + + +int main (int argc, char ** argv) +{ + printf ("C PLUGIN TESTS\n"); + printf ("==================\n\n"); + init (argc, argv); + + test_contract (); + test_set_empty (); + test_set_simple (); + test_set_meta (); + + print_result ("testmod_c"); + return nbError; +} From 7494b05d10c04931cda59e469246a81e9762ecf5 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 12 Jul 2023 00:54:33 +0000 Subject: [PATCH 2/4] Restyled by clang-format --- src/plugins/c/c.c | 2 +- src/plugins/c/testmod_c.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/plugins/c/c.c b/src/plugins/c/c.c index d4e219c02f5..cf9cec472bc 100644 --- a/src/plugins/c/c.c +++ b/src/plugins/c/c.c @@ -48,7 +48,7 @@ static char * escapeString (char ** str) ++size; } else - { /* convert to hex value, needs 4 chars */ + { /* convert to hex value, needs 4 chars */ size += 4; } } diff --git a/src/plugins/c/testmod_c.c b/src/plugins/c/testmod_c.c index d289f058b69..48cfc207fe6 100644 --- a/src/plugins/c/testmod_c.c +++ b/src/plugins/c/testmod_c.c @@ -22,10 +22,12 @@ static void test_contract (void) PLUGIN_CLOSE (); succeed_if (ksGetSize (contract) > 0, "Empty KeySet was returned for plugin contract"); - succeed_if (ksLookup (contract, parentKey, KDB_O_NONE), "The key with the name 'system:/elektra/modules/c' was not part of the plugin contract"); + succeed_if (ksLookup (contract, parentKey, KDB_O_NONE), + "The key with the name 'system:/elektra/modules/c' was not part of the plugin contract"); Key * keyExportsSet = ksLookupByName (contract, "system:/elektra/modules/c/exports/set", KDB_O_NONE); - succeed_if (keyExportsSet, "The set-function was not part of the plugin contract (key 'system:/elektra/modules/c/exports/set' not found)"); + succeed_if (keyExportsSet, + "The set-function was not part of the plugin contract (key 'system:/elektra/modules/c/exports/set' not found)"); succeed_if (keyValue (keyExportsSet), "The set function retrieved as part of the plugin contract was NULL"); } @@ -36,12 +38,12 @@ static void test_set_empty (void) KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("c"); - Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename (), KEY_END); KeySet * ksEmpty = ksNew (0, KS_END); succeed_if (plugin->kdbSet (plugin, ksEmpty, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write empty KeySet to file"); ksDel (ksEmpty); - PLUGIN_CLOSE(); + PLUGIN_CLOSE (); } static void test_set_simple (void) @@ -51,16 +53,17 @@ static void test_set_simple (void) KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("c"); - Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename (), KEY_END); KeySet * ksTest = ksNew (0, KS_END); ksAppendKey (ksTest, keyNew ("user:/tests/c/key1", KEY_VALUE, "value1", KEY_END)); ksAppendKey (ksTest, keyNew ("user:/tests/c/key2", KEY_VALUE, "value2", KEY_END)); ksAppendKey (ksTest, keyNew ("user:/tests/c/key3", KEY_VALUE, "value3", KEY_END)); - succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write non-empty KeySet to file"); + succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, + "Unable to write non-empty KeySet to file"); ksDel (ksTest); - PLUGIN_CLOSE(); + PLUGIN_CLOSE (); } static void test_set_meta (void) @@ -70,7 +73,7 @@ static void test_set_meta (void) KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("c"); - Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename(), KEY_END); + Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename (), KEY_END); KeySet * ksTest = ksNew (0, KS_END); Key * k1 = keyNew ("user:/tests/c/key1", KEY_VALUE, "value1", KEY_END); @@ -86,10 +89,11 @@ static void test_set_meta (void) ksAppendKey (ksTest, k2); ksAppendKey (ksTest, k3); - succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write KeySet with keys containing metadata to file"); + succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, + "Unable to write KeySet with keys containing metadata to file"); ksDel (ksTest); - PLUGIN_CLOSE(); + PLUGIN_CLOSE (); } From a34be40e0a450a345161f1ab8a51ef1367e280b5 Mon Sep 17 00:00:00 2001 From: Florian Lindner Date: Wed, 12 Jul 2023 06:21:50 +0200 Subject: [PATCH 3/4] Plugins: fix memleaks in tests for the c plugin --- src/plugins/c/testmod_c.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/plugins/c/testmod_c.c b/src/plugins/c/testmod_c.c index 48cfc207fe6..f30d07156e9 100644 --- a/src/plugins/c/testmod_c.c +++ b/src/plugins/c/testmod_c.c @@ -24,11 +24,13 @@ static void test_contract (void) succeed_if (ksGetSize (contract) > 0, "Empty KeySet was returned for plugin contract"); succeed_if (ksLookup (contract, parentKey, KDB_O_NONE), "The key with the name 'system:/elektra/modules/c' was not part of the plugin contract"); + keyDel (parentKey); Key * keyExportsSet = ksLookupByName (contract, "system:/elektra/modules/c/exports/set", KDB_O_NONE); succeed_if (keyExportsSet, "The set-function was not part of the plugin contract (key 'system:/elektra/modules/c/exports/set' not found)"); succeed_if (keyValue (keyExportsSet), "The set function retrieved as part of the plugin contract was NULL"); + ksDel (contract); } static void test_set_empty (void) @@ -41,9 +43,10 @@ static void test_set_empty (void) Key * parentKey = keyNew ("user:/tests/c", KEY_VALUE, elektraFilename (), KEY_END); KeySet * ksEmpty = ksNew (0, KS_END); succeed_if (plugin->kdbSet (plugin, ksEmpty, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write empty KeySet to file"); - ksDel (ksEmpty); - PLUGIN_CLOSE (); + + keyDel (parentKey); + ksDel (ksEmpty); } static void test_set_simple (void) @@ -61,9 +64,10 @@ static void test_set_simple (void) succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write non-empty KeySet to file"); - ksDel (ksTest); - PLUGIN_CLOSE (); + + keyDel (parentKey); + ksDel (ksTest); } static void test_set_meta (void) @@ -91,9 +95,10 @@ static void test_set_meta (void) succeed_if (plugin->kdbSet (plugin, ksTest, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write KeySet with keys containing metadata to file"); - ksDel (ksTest); - PLUGIN_CLOSE (); + + keyDel (parentKey); + ksDel (ksTest); } From 159e559c3f16fbc7229a0dd6aa41f28bb5172ffb Mon Sep 17 00:00:00 2001 From: Maximilian Irlinger Date: Tue, 25 Jul 2023 17:37:02 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Markus Raab --- src/plugins/c/README.md | 2 +- src/plugins/c/c.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/c/README.md b/src/plugins/c/README.md index b482ac18b99..2b9bd15e01d 100644 --- a/src/plugins/c/README.md +++ b/src/plugins/c/README.md @@ -52,7 +52,7 @@ kdb set user:/tests/cplugin/key2 metakey2.2 metaval2.2 kdb set user:/tests/cplugin/key3 value3 #> Create a new key user:/tests/cplugin/key3 with string "value3" -bin/kdb export user:/tests/cplugin c +kdb export user:/tests/cplugin c #> ksNew (3, #> keyNew ("user:/tests/cplugin/key1", KEY_VALUE, "value1", KEY_END), #> keyNew ("user:/tests/cplugin/key2", KEY_VALUE, "value2", KEY_META, "metakey2.1", "metaval2.1", KEY_META, "metakey2.2", "metaval2.2", KEY_END), diff --git a/src/plugins/c/c.c b/src/plugins/c/c.c index cf9cec472bc..b760fb80c87 100644 --- a/src/plugins/c/c.c +++ b/src/plugins/c/c.c @@ -205,7 +205,7 @@ static int ksGenerate (const KeySet * ks, FILE * stream) /** * @brief The get function of this plugin is only used to define the contract. * - * @return 1 + * @retval 1 on success (always as parsing is not yet implemented) */ int elektraCGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey) {