summaryrefslogtreecommitdiffstats
path: root/test/lilv_test_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/lilv_test_utils.c')
-rw-r--r--test/lilv_test_utils.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/test/lilv_test_utils.c b/test/lilv_test_utils.c
new file mode 100644
index 0000000..6137c27
--- /dev/null
+++ b/test/lilv_test_utils.c
@@ -0,0 +1,184 @@
+/*
+ Copyright 2020 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#define _POSIX_C_SOURCE 200809L /* for setenv */
+
+#include "lilv_test_utils.h"
+
+#include "../src/lilv_internal.h"
+
+#include "lilv/lilv.h"
+
+#ifdef _WIN32
+# include <direct.h>
+# include <io.h>
+# define mkdir(path, flags) _mkdir(path)
+#else
+# include <unistd.h>
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+LilvTestEnv*
+lilv_test_env_new(void)
+{
+ LilvWorld* world = lilv_world_new();
+ if (!world) {
+ return NULL;
+ }
+
+ LilvTestEnv* env = (LilvTestEnv*)calloc(1, sizeof(LilvTestEnv));
+
+ env->world = world;
+ env->plugin1_uri = lilv_new_uri(world, "http://example.org/plug");
+ env->plugin2_uri = lilv_new_uri(world, "http://example.org/foobar");
+
+ // Set custom LV2_PATH in build directory to only use test data
+ char* test_path = lilv_realpath(LILV_TEST_DIR);
+ char* lv2_path = lilv_strjoin(test_path, "/test_lv2_path", NULL);
+ LilvNode* path = lilv_new_string(world, lv2_path);
+ lilv_world_set_option(world, LILV_OPTION_LV2_PATH, path);
+ free(lv2_path);
+ free(test_path);
+ lilv_node_free(path);
+
+ return env;
+}
+
+void
+lilv_test_env_free(LilvTestEnv* env)
+{
+ free(env->test_content_path);
+ free(env->test_manifest_path);
+ free(env->test_bundle_uri);
+ free(env->test_bundle_path);
+ lilv_node_free(env->plugin2_uri);
+ lilv_node_free(env->plugin1_uri);
+ lilv_world_free(env->world);
+ free(env);
+}
+
+int
+create_bundle(LilvTestEnv* env, const char* manifest, const char* plugin)
+{
+ {
+ static const char* const bundle_path = "/test_lv2_path/lilv-test.lv2";
+
+ char* const test_path = lilv_realpath(LILV_TEST_DIR);
+ env->test_bundle_path = lilv_strjoin(test_path, bundle_path, NULL);
+ lilv_free(test_path);
+ }
+
+ if (lilv_mkdir_p(env->test_bundle_path)) {
+ fprintf(stderr,
+ "Failed to create directory '%s' (%s)\n",
+ env->test_bundle_path,
+ strerror(errno));
+ return 1;
+ }
+
+ SerdNode s = serd_node_new_file_uri((const uint8_t*)env->test_bundle_path,
+ NULL,
+ NULL,
+ true);
+
+ env->test_bundle_uri = lilv_strjoin((const char*)s.buf, "/", NULL);
+ env->test_manifest_path =
+ lilv_strjoin(env->test_bundle_path, "/manifest.ttl", NULL);
+ env->test_content_path =
+ lilv_strjoin(env->test_bundle_path, "/plugin.ttl", NULL);
+
+ serd_node_free(&s);
+
+ FILE* const manifest_file = fopen(env->test_manifest_path, "w");
+ if (!manifest_file) {
+ return 2;
+ }
+
+ FILE* const plugin_file = fopen(env->test_content_path, "w");
+ if (!plugin_file) {
+ fclose(manifest_file);
+ return 3;
+ }
+
+ const size_t manifest_len = strlen(manifest);
+ const size_t plugin_len = strlen(plugin);
+ size_t n_written = 0;
+
+ n_written += fwrite(manifest, 1, manifest_len, manifest_file);
+ n_written += fwrite(plugin, 1, plugin_len, plugin_file);
+
+ fclose(manifest_file);
+ fclose(plugin_file);
+
+ return n_written == manifest_len + plugin_len ? 0 : 4;
+}
+
+int
+start_bundle(LilvTestEnv* env, const char* manifest, const char* content)
+{
+ if (create_bundle(env, manifest, content)) {
+ return 1;
+ }
+
+ lilv_world_load_all(env->world);
+ return 0;
+}
+
+void
+delete_bundle(LilvTestEnv* env)
+{
+ if (env->test_content_path) {
+ unlink(env->test_content_path);
+ }
+
+ if (env->test_manifest_path) {
+ unlink(env->test_manifest_path);
+ }
+
+ if (env->test_bundle_path) {
+ remove(env->test_bundle_path);
+ }
+
+ free(env->test_content_path);
+ free(env->test_manifest_path);
+ free(env->test_bundle_uri);
+ free(env->test_bundle_path);
+
+ env->test_content_path = NULL;
+ env->test_manifest_path = NULL;
+ env->test_bundle_uri = NULL;
+ env->test_bundle_path = NULL;
+}
+
+void
+set_env(const char* name, const char* value)
+{
+#ifdef _WIN32
+ // setenv on Windows does not modify the current process' environment
+ const size_t len = strlen(name) + 1 + strlen(value) + 1;
+ char* str = (char*)calloc(1, len);
+ snprintf(str, len, "%s=%s", name, value);
+ putenv(str);
+ free(str);
+#else
+ setenv(name, value, 1);
+#endif
+}