aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt2
-rw-r--r--tests/test_artifactory.c12
-rw-r--r--tests/test_conda.c6
-rw-r--r--tests/test_environment.c2
-rw-r--r--tests/test_junitxml.c4
-rw-r--r--tests/test_multiprocessing.c4
-rw-r--r--tests/test_recipe.c2
-rw-r--r--tests/test_relocation.c5
-rw-r--r--tests/test_str.c12
-rw-r--r--tests/test_system.c2
-rw-r--r--tests/test_template.c49
-rw-r--r--tests/test_tests.c52
-rw-r--r--tests/test_utils.c24
-rw-r--r--tests/test_wheel.c275
-rw-r--r--tests/test_wheelinfo.c91
15 files changed, 416 insertions, 126 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 08ef833..dd68231 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -44,7 +44,7 @@ foreach(source_file ${source_files})
add_test(${test_executable} ${test_executable})
set_tests_properties(${test_executable}
PROPERTIES
- TIMEOUT 240)
+ TIMEOUT 600)
set_tests_properties(${test_executable}
PROPERTIES
SKIP_RETURN_CODE 127)
diff --git a/tests/test_artifactory.c b/tests/test_artifactory.c
index 202a67c..fadc9f1 100644
--- a/tests/test_artifactory.c
+++ b/tests/test_artifactory.c
@@ -15,14 +15,14 @@ const char *gbuild_num = "1";
static int jfrog_cli_rt_build_delete(struct JFRT_Auth *auth, char *build_name, char *build_num) {
char cmd[STASIS_BUFSIZ];
memset(cmd, 0, sizeof(cmd));
- snprintf(cmd, sizeof(cmd) - 1, "--build \"%s/%s\"", build_name, build_num);
+ snprintf(cmd, sizeof(cmd), "--build \"%s/%s\"", build_name, build_num);
return jfrog_cli(auth, "rt", "delete", cmd);
}
static int jfrog_cli_rt_delete(struct JFRT_Auth *auth, char *pattern) {
char cmd[STASIS_BUFSIZ];
memset(cmd, 0, sizeof(cmd));
- snprintf(cmd, sizeof(cmd) - 1, "\"%s\"", pattern);
+ snprintf(cmd, sizeof(cmd), "\"%s\"", pattern);
return jfrog_cli(auth, "rt", "delete", cmd);
}
@@ -60,7 +60,7 @@ void test_jfrog_cli_rt_download() {
char *filename = "empty_file_upload.txt";
char path[PATH_MAX] = {0};
- sprintf(path, "%s/%s", getenv("STASIS_JF_REPO"), filename);
+ snprintf(path, sizeof(path), "%s/%s", getenv("STASIS_JF_REPO"), filename);
STASIS_ASSERT(jfrog_cli_rt_download(&gauth, &dl, filename, ".") == 0, "jf download failed");
STASIS_ASSERT(jfrog_cli_rt_delete(&gauth, path) == 0, "jf delete test artifact failed");
}
@@ -93,15 +93,15 @@ int main(int argc, char *argv[]) {
}
char path[PATH_MAX] = {0};
- sprintf(path, "%s/bin:%s", ctx.storage.tools_dir, getenv("PATH"));
+ snprintf(path, sizeof(path), "%s/bin:%s", ctx.storage.tools_dir, getenv("PATH"));
setenv("PATH", path, 1);
// The default config contains the URL information to download jfrog-cli
char cfg_path[PATH_MAX] = {0};
if (strstr(sysconfdir, "..")) {
- sprintf(cfg_path, "%s/%s/stasis.ini", basedir, sysconfdir);
+ snprintf(cfg_path, sizeof(cfg_path), "%s/%s/stasis.ini", basedir, sysconfdir);
} else {
- sprintf(cfg_path, "%s/stasis.ini", sysconfdir);
+ snprintf(cfg_path, sizeof(cfg_path), "%s/stasis.ini", sysconfdir);
}
ctx._stasis_ini_fp.cfg = ini_open(cfg_path);
if (!ctx._stasis_ini_fp.cfg) {
diff --git a/tests/test_conda.c b/tests/test_conda.c
index 4d437e4..9f0e718 100644
--- a/tests/test_conda.c
+++ b/tests/test_conda.c
@@ -20,7 +20,7 @@ void test_micromamba() {
struct testcase tc[] = {
{.mminfo = {.micromamba_prefix = mm_prefix, .conda_prefix = c_prefix}, .cmd = "info", .result = 0},
{.mminfo = {.micromamba_prefix = mm_prefix, .conda_prefix = c_prefix}, .cmd = "env list", .result = 0},
- {.mminfo = {.micromamba_prefix = mm_prefix, .conda_prefix = c_prefix}, .cmd = "run python -V", .result = 0},
+ {.mminfo = {.micromamba_prefix = mm_prefix, .conda_prefix = c_prefix}, .cmd = "run python3 -V", .result = 0},
{.mminfo = {.micromamba_prefix = mm_prefix, .conda_prefix = c_prefix}, .cmd = "no_such_option", .result = 109},
};
@@ -40,7 +40,7 @@ struct Delivery ctx;
void test_conda_installation() {
char *install_url = calloc(255, sizeof(install_url));
- delivery_get_conda_installer_url(&ctx, install_url);
+ delivery_get_conda_installer_url(&ctx, install_url, PATH_MAX);
delivery_get_conda_installer(&ctx, install_url);
delivery_install_conda(ctx.conda.installer_path, ctx.storage.conda_install_prefix);
@@ -92,7 +92,7 @@ void test_conda_exec() {
void test_python_exec() {
const char *python_system_path = find_program("python3");
char python_path[PATH_MAX];
- sprintf(python_path, "%s/bin/python3", ctx.storage.conda_install_prefix);
+ snprintf(python_path, sizeof(python_path), "%s/bin/python3", ctx.storage.conda_install_prefix);
STASIS_ASSERT(strcmp(python_path, python_system_path ? python_system_path : "/not/found") == 0, "conda is not configured correctly.");
STASIS_ASSERT(python_exec("-V") == 0, "python is broken");
diff --git a/tests/test_environment.c b/tests/test_environment.c
index 4f36883..9a503c0 100644
--- a/tests/test_environment.c
+++ b/tests/test_environment.c
@@ -58,7 +58,7 @@ void test_runtime() {
char output_truth[BUFSIZ] = {0};
char *your_path = runtime_get(env, "PATH");
- sprintf(output_truth, "Your PATH is '%s'.", your_path);
+ snprintf(output_truth, sizeof(output_truth), "Your PATH is '%s'.", your_path);
guard_free(your_path);
char *output_expanded = runtime_expand_var(env, "Your PATH is '${PATH}'.");
diff --git a/tests/test_junitxml.c b/tests/test_junitxml.c
index 362cb32..0bbbefb 100644
--- a/tests/test_junitxml.c
+++ b/tests/test_junitxml.c
@@ -4,7 +4,7 @@
void test_junitxml_testsuite_read() {
struct JUNIT_Testsuite *testsuite;
char datafile[PATH_MAX] = {0};
- snprintf(datafile, sizeof(datafile) - 1, "%s/result.xml", TEST_DATA_DIR);
+ snprintf(datafile, sizeof(datafile), "%s/result.xml", TEST_DATA_DIR);
STASIS_ASSERT_FATAL((testsuite = junitxml_testsuite_read(datafile)) != NULL, "failed to load testsuite data");
STASIS_ASSERT(testsuite->name != NULL, "Test suite must be named");
@@ -48,7 +48,7 @@ void test_junitxml_testsuite_read() {
void test_junitxml_testsuite_read_error() {
struct JUNIT_Testsuite *testsuite;
char datafile[PATH_MAX] = {0};
- snprintf(datafile, sizeof(datafile) - 1, "%s/result_error.xml", TEST_DATA_DIR);
+ snprintf(datafile, sizeof(datafile), "%s/result_error.xml", TEST_DATA_DIR);
STASIS_ASSERT_FATAL((testsuite = junitxml_testsuite_read(datafile)) != NULL, "failed to load testsuite data");
STASIS_ASSERT(testsuite->name != NULL, "test suite must be named");
diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c
index 3a462f1..767a9e0 100644
--- a/tests/test_multiprocessing.c
+++ b/tests/test_multiprocessing.c
@@ -65,7 +65,7 @@ void test_mp_task() {
for (size_t i = 0; i < sizeof(commands) / sizeof(*commands); i++) {
struct MultiProcessingTask *task;
char task_name[100] = {0};
- sprintf(task_name, "mytask%zu", i);
+ snprintf(task_name, sizeof(task_name), "mytask%zu", i);
STASIS_ASSERT_FATAL((task = mp_pool_task(pool, task_name, NULL, commands[i])) != NULL, "Task should not be NULL");
STASIS_ASSERT(task->pid == MP_POOL_PID_UNUSED, "PID should be non-zero at this point");
STASIS_ASSERT(task->parent_pid == MP_POOL_PID_UNUSED, "Parent PID should be non-zero");
@@ -137,7 +137,7 @@ void test_mp_fail_fast() {
for (size_t i = 0; i < sizeof(commands_ff) / sizeof(*commands_ff); i++) {
char *command = commands_ff[i];
char taskname[100] = {0};
- snprintf(taskname, sizeof(taskname) - 1, "task_%03zu", i);
+ snprintf(taskname, sizeof(taskname), "task_%03zu", i);
STASIS_ASSERT(mp_pool_task(p, taskname, NULL, (char *) command) != NULL, "Failed to queue task");
}
diff --git a/tests/test_recipe.c b/tests/test_recipe.c
index fc7cc78..3ea21ce 100644
--- a/tests/test_recipe.c
+++ b/tests/test_recipe.c
@@ -4,7 +4,7 @@
static void make_local_recipe(const char *localdir) {
char path[PATH_MAX] = {0};
- sprintf(path, "./%s", localdir);
+ snprintf(path, sizeof(path), "./%s", localdir);
mkdir(path, 0755);
if (!pushd(path)) {
touch("meta.yaml");
diff --git a/tests/test_relocation.c b/tests/test_relocation.c
index a6c33f2..69142dc 100644
--- a/tests/test_relocation.c
+++ b/tests/test_relocation.c
@@ -14,9 +14,12 @@ void test_replace_text() {
const char *target = targets[i];
const char *expected = targets[i + 1];
char input[BUFSIZ] = {0};
- strcpy(input, test_string);
+ strncpy(input, test_string, sizeof(input) - 1);
+ printf("input: %s\n", input);
+ printf("target: %s\n", target);
STASIS_ASSERT(replace_text(input, target, "^^^", 0) == 0, "string replacement failed");
+ printf("result: %s\n\n", input);
STASIS_ASSERT(strcmp(input, expected) == 0, "unexpected replacement");
}
diff --git a/tests/test_str.c b/tests/test_str.c
index a98a34d..aac5d71 100644
--- a/tests/test_str.c
+++ b/tests/test_str.c
@@ -37,7 +37,7 @@ void test_tolower_s() {
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char input[100] = {0};
- strcpy(input, tc[i].data);
+ strncpy(input, tc[i].data, sizeof(input) - 1);
tolower_s(input);
STASIS_ASSERT(strcmp(input, tc[i].expected) == 0, "unexpected result");
}
@@ -317,9 +317,8 @@ void test_lstrip() {
STASIS_ASSERT(lstrip(NULL) == NULL, "incorrect return type");
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char *buf = calloc(255, sizeof(*buf));
- char *result;
- strcpy(buf, tc[i].data);
- result = lstrip(buf);
+ strncpy(buf, tc[i].data, 254);
+ char *result = lstrip(buf);
STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-left");
guard_free(buf);
}
@@ -342,9 +341,8 @@ void test_strip() {
STASIS_ASSERT(strip(NULL) == NULL, "incorrect return type");
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char *buf = calloc(255, sizeof(*buf));
- char *result;
- strcpy(buf, tc[i].data);
- result = strip(buf);
+ strncpy(buf, tc[i].data, 254);
+ char *result = strip(buf);
STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-right");
guard_free(buf);
}
diff --git a/tests/test_system.c b/tests/test_system.c
index 2271e13..9e4a862 100644
--- a/tests/test_system.c
+++ b/tests/test_system.c
@@ -54,7 +54,7 @@ void test_shell_safe_verify_restrictions() {
char cmd[PATH_MAX] = {0};
memset(&proc, 0, sizeof(proc));
- sprintf(cmd, "true%c false", invalid_chars[i]);
+ snprintf(cmd, sizeof(cmd), "true%c false", invalid_chars[i]);
shell_safe(&proc, cmd);
STASIS_ASSERT(proc.returncode == -1, "expected a negative result due to intentional error");
}
diff --git a/tests/test_template.c b/tests/test_template.c
index 596c2b7..e8f0c1d 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -10,8 +10,9 @@ static int adder(struct tplfunc_frame *frame, void *result) {
int a = (int) strtol(frame->argv[0].t_char_ptr, NULL, 10);
int b = (int) strtol(frame->argv[1].t_char_ptr, NULL, 10);
char **ptr = (char **) result;
- *ptr = calloc(100, sizeof(*ptr));
- sprintf(*ptr, "%d", a + b);
+ const size_t sz = 100;
+ *ptr = calloc(sz, sizeof(*ptr));
+ snprintf(*ptr, sz, "%d", a + b);
return 0;
}
@@ -19,8 +20,9 @@ static int subtractor(struct tplfunc_frame *frame, void *result) {
int a = (int) strtol(frame->argv[0].t_char_ptr, NULL, 10);
int b = (int) strtol(frame->argv[1].t_char_ptr, NULL, 10);
char **ptr = (char **) result;
- *ptr = calloc(100, sizeof(*ptr));
- sprintf(*ptr, "%d", a - b);
+ const size_t sz = 100;
+ *ptr = calloc(sz, sizeof(*ptr));
+ snprintf(*ptr, sz, "%d", a - b);
return 0;
}
@@ -28,8 +30,9 @@ static int multiplier(struct tplfunc_frame *frame, void *result) {
int a = (int) strtol(frame->argv[0].t_char_ptr, NULL, 10);
int b = (int) strtol(frame->argv[1].t_char_ptr, NULL, 10);
char **ptr = (char **) result;
- *ptr = calloc(100, sizeof(*ptr));
- sprintf(*ptr, "%d", a * b);
+ const size_t sz = 100;
+ *ptr = calloc(sz, sizeof(*ptr));
+ snprintf(*ptr, sz, "%d", a * b);
return 0;
}
@@ -37,8 +40,9 @@ static int divider(struct tplfunc_frame *frame, void *result) {
int a = (int) strtol(frame->argv[0].t_char_ptr, NULL, 10);
int b = (int) strtol(frame->argv[1].t_char_ptr, NULL, 10);
char **ptr = (char **) result;
- *ptr = calloc(100, sizeof(*ptr));
- sprintf(*ptr, "%d", a / b);
+ size_t sz = 100;
+ *ptr = calloc(sz, sizeof(*ptr));
+ snprintf(*ptr, sz, "%d", a / b);
return 0;
}
@@ -57,6 +61,19 @@ void test_tpl_workflow() {
STASIS_ASSERT(strcmp(result, "Hello environment!") == 0, "environment variable content mismatch");
guard_free(result);
unsetenv("HELLO");
+
+ const char *message_file = "message.txt";
+ char message_fmt[] = "They wanted a {{ hello_message }} "
+ "So we gave them a {{ hello_message }}";
+ const char *message_expected = "They wanted a Hello world! "
+ "So we gave them a Hello world!";
+ const int state = tpl_render_to_file(message_fmt, message_file);
+ STASIS_ASSERT_FATAL(state == 0, "failed to write rendered string to file");
+ char *message_contents = stasis_testing_read_ascii(message_file);
+ STASIS_ASSERT(strcmp(message_contents, message_expected) == 0, "message in file does not match original message");
+ guard_free(message_contents);
+ remove(message_file);
+
guard_free(data);
}
@@ -68,6 +85,8 @@ void test_tpl_register() {
STASIS_ASSERT(tpl_pool_used == (used_before_register + 1), "tpl_register did not increment allocation counter");
STASIS_ASSERT(tpl_pool[used_before_register] != NULL, "register did not allocate a tpl_item record in the pool");
+ const char *message = tpl_getval("hello_message");
+ STASIS_ASSERT(strcmp(message, "Hello world!") == 0, "stored message corrupt");
free(data);
}
@@ -92,25 +111,25 @@ void test_tpl_register_func() {
char *result = NULL;
result = tpl_render("{{ func:add(0,3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "add: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:add(1,2) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "add: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:sub(6,3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "sub: was not 3");
guard_free(result);
result = tpl_render("{{ func:sub(4,1) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "sub: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:mul(1, 3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "mul: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:div(6,2) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "div: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:div(3,1) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "div: Answer was not 3");
guard_free(result);
}
diff --git a/tests/test_tests.c b/tests/test_tests.c
new file mode 100644
index 0000000..0f6d7ca
--- /dev/null
+++ b/tests/test_tests.c
@@ -0,0 +1,52 @@
+#include "delivery.h"
+#include "testing.h"
+
+static struct Test *mock_test(const int ident) {
+ struct Test *test = test_init();
+ if (asprintf(&test->name, "test_%d", ident) < 0) {
+ return NULL;
+ }
+ return test;
+}
+
+void test_tests() {
+ const int initial = TEST_NUM_ALLOC_INITIAL;
+ const int balloon = initial * 10;
+ struct Tests *tests = tests_init(initial);
+ STASIS_ASSERT_FATAL(tests != NULL, "tests structure allocation failed");
+ STASIS_ASSERT(tests->num_alloc == (size_t) initial, "incorrect number of records initialized");
+ STASIS_ASSERT(tests->num_used == 0, "incorrect number of records used");
+
+ for (int i = 0; i < balloon; i++) {
+ struct Test *test = mock_test(i);
+ if (!test) {
+ SYSERROR("unable to allocate memory for test %d", i);
+ return;
+ }
+ tests_add(tests, test);
+ }
+
+ size_t errors = 0;
+ for (int i = 0; i < initial * 10; i++) {
+ struct Test *test = tests->test[i];
+ if (!test) {
+ errors++;
+ continue;
+ }
+ if (!test->name) {
+ errors++;
+ }
+ }
+ STASIS_ASSERT(errors == 0, "no errors should be detected in test->name member");
+
+ tests_free(&tests);
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ STASIS_TEST_FUNC *tests[] = {
+ test_tests,
+ };
+ STASIS_TEST_RUN(tests);
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file
diff --git a/tests/test_utils.c b/tests/test_utils.c
index cfe79e0..7361139 100644
--- a/tests/test_utils.c
+++ b/tests/test_utils.c
@@ -65,7 +65,7 @@ void test_fix_tox_conf() {
if (fp) {
fprintf(fp, "%s", data);
fclose(fp);
- STASIS_ASSERT(fix_tox_conf(filename, &result) == 0, "fix_tox_conf failed");
+ STASIS_ASSERT(fix_tox_conf(filename, &result, PATH_MAX) == 0, "fix_tox_conf failed");
} else {
STASIS_ASSERT(false, "writing mock tox.ini failed");
}
@@ -129,7 +129,7 @@ void test_isempty_dir() {
STASIS_ASSERT(isempty_dir(dname) > 0, "empty directory went undetected");
char path[PATH_MAX];
- sprintf(path, "%s/file.txt", dname);
+ snprintf(path, sizeof(path), "%s/file.txt", dname);
touch(path);
STASIS_ASSERT(isempty_dir(dname) == 0, "populated directory went undetected");
@@ -147,7 +147,9 @@ void test_xmkstemp() {
char buf[100] = {0};
tempfp = fopen(tempfile, "r");
- fgets(buf, sizeof(buf) - 1, tempfp);
+ const char *line = fgets(buf, sizeof(buf) - 1, tempfp);
+ STASIS_ASSERT_FATAL(line != NULL, "file should contain data written earlier");
+ STASIS_ASSERT(strcmp(line, buf) == 0, "file should contain the correct data");
fclose(tempfp);
STASIS_ASSERT(strcmp(buf, data) == 0, "data written to temp file is incorrect");
@@ -193,7 +195,7 @@ void test_git_clone_and_describe() {
// initialize a bare repo so we can clone it
char init_cmd[PATH_MAX];
- sprintf(init_cmd, "git init --bare %s", repo_git);
+ snprintf(init_cmd, sizeof(init_cmd), "git init --bare %s", repo_git);
system(init_cmd);
// clone the bare repo
@@ -308,7 +310,7 @@ void test_path_dirname() {
const char *input = data[i];
const char *expected = data[i + 1];
char tmp[PATH_MAX] = {0};
- strcpy(tmp, input);
+ strncpy(tmp, input, sizeof(tmp) - 1);
char *result = path_dirname(tmp);
STASIS_ASSERT(strcmp(expected, result) == 0, NULL);
@@ -329,8 +331,7 @@ void test_path_basename() {
}
void test_expandpath() {
- char *home;
-
+ char *home = NULL;
const char *homes[] = {
"HOME",
"USERPROFILE",
@@ -341,10 +342,11 @@ void test_expandpath() {
break;
}
}
+ STASIS_ASSERT_FATAL(home != NULL, "cannot expand without knowing the user's home directory path");
char path[PATH_MAX] = {0};
- strcat(path, "~");
- strcat(path, DIR_SEP);
+ strncat(path, "~", sizeof(path) - strlen(path) - 1);
+ strncat(path, DIR_SEP, sizeof(path) - strlen(path) - 1);
char *expanded = expandpath(path);
STASIS_ASSERT(startswith(expanded, home) > 0, expanded);
@@ -366,8 +368,8 @@ void test_rmtree() {
for (size_t i = 0; i < sizeof(tree) / sizeof(*tree); i++) {
char path[PATH_MAX];
char mockfile[PATH_MAX + 10];
- sprintf(path, "%s/%s", root, tree[i]);
- sprintf(mockfile, "%s/%zu.txt", path, i);
+ snprintf(path, sizeof(path), "%s/%s", root, tree[i]);
+ snprintf(mockfile, sizeof(mockfile), "%s/%zu.txt", path, i);
if (mkdir(path, 0755)) {
perror(path);
STASIS_ASSERT(false, NULL);
diff --git a/tests/test_wheel.c b/tests/test_wheel.c
index 6818b22..531e4b7 100644
--- a/tests/test_wheel.c
+++ b/tests/test_wheel.c
@@ -1,91 +1,216 @@
+#include "delivery.h"
#include "testing.h"
+#include "str.h"
#include "wheel.h"
-void test_get_wheel_file() {
- struct testcase {
- const char *filename;
- struct Wheel expected;
- };
- struct testcase tc[] = {
- {
- // Test for "build tags"
- .filename = "btpackage-1.2.3-mytag-py2.py3-none-any.whl",
- .expected = {
- .file_name = "btpackage-1.2.3-mytag-py2.py3-none-any.whl",
- .version = "1.2.3",
- .distribution = "btpackage",
- .build_tag = "mytag",
- .platform_tag = "any",
- .python_tag = "py2.py3",
- .abi_tag = "none",
- .path_name = ".",
- }
- },
- {
- // Test for universal package format
- .filename = "anypackage-1.2.3-py2.py3-none-any.whl",
- .expected = {
- .file_name = "anypackage-1.2.3-py2.py3-none-any.whl",
- .version = "1.2.3",
- .distribution = "anypackage",
- .build_tag = NULL,
- .platform_tag = "any",
- .python_tag = "py2.py3",
- .abi_tag = "none",
- .path_name = ".",
- }
- },
- {
- // Test for binary package format
- .filename = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl",
- .expected = {
- .file_name = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl",
- .version = "1.2.3",
- .distribution = "binpackage",
- .build_tag = NULL,
- .platform_tag = "linux_x86_64",
- .python_tag = "cp311",
- .abi_tag = "cp311",
- .path_name = ".",
- }
- },
- };
+char cwd_start[PATH_MAX];
+char cwd_workspace[PATH_MAX];
+int conda_is_installed = 0;
+static char conda_prefix[PATH_MAX] = {0};
+struct Delivery ctx;
+static const char *testpkg_filename = "testpkg/dist/testpkg-1.0.0-py3-none-any.whl";
+
+
+static void test_wheel_package() {
+ const char *filename = testpkg_filename;
+ struct Wheel *wheel = NULL;
+ int state = wheel_package(&wheel, filename);
+ STASIS_ASSERT(state != WHEEL_PACKAGE_E_ALLOC, "Cannot fail to allocate memory for package structure");
+ STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET, "Cannot fail to parse wheel");
+ STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_METADATA, "Cannot fail to read wheel metadata");
+ STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_RECORDS, "Cannot fail reading wheel path records");
+ STASIS_ASSERT(state != WHEEL_PACKAGE_E_GET_ENTRY_POINT, "Cannot fail reading wheel entry points");
+ STASIS_ASSERT(state == WHEEL_PACKAGE_E_SUCCESS, "Wheel file should be usable");
+ STASIS_ASSERT(wheel != NULL, "wheel cannot be NULL");
+ STASIS_ASSERT(wheel != NULL, "wheel_package failed to initialize wheel struct");
+ STASIS_ASSERT(wheel->record != NULL, "Record cannot be NULL");
+ STASIS_ASSERT(wheel->num_record > 0, "Record count cannot be zero");
+ STASIS_ASSERT(wheel->tag != NULL, "Package tag list cannot be NULL");
+ STASIS_ASSERT(wheel->generator != NULL, "Generator field cannot be NULL");
+ STASIS_ASSERT(wheel->top_level != NULL, "Top level directory name cannot be NULL");
+ STASIS_ASSERT(wheel->wheel_version != NULL, "Wheel version cannot be NULL");
+ STASIS_ASSERT(wheel->metadata != NULL, "Metadata cannot be NULL");
+ STASIS_ASSERT(wheel->metadata->name != NULL, "Metadata::name cannot be NULL");
+ STASIS_ASSERT(wheel->metadata->version != NULL, "Metadata::version cannot be NULL");
+ STASIS_ASSERT(wheel->metadata->metadata_version != NULL, "Metadata::version (of metadata) cannot be NULL");
+
+ // Implied test against key/id getters. If wheel_show_info segfaults, that functionality is broken.
+ STASIS_ASSERT(wheel_show_info(wheel) == 0, "wheel_show_info should never fail. Enum(s) might be out of sync with META_*_KEYS array(s)");
+
+ // Get data from DIST
+ const struct WheelValue dist_version = wheel_get_value_by_name(wheel, WHEEL_FROM_DIST, "Wheel-Version");
+ STASIS_ASSERT(dist_version.type == WHEELVAL_STR, "Wheel dist version value must be a string");
+ STASIS_ASSERT_FATAL(dist_version.data != NULL, "Wheel dist version value must not be NULL");
+ STASIS_ASSERT(dist_version.count, "Wheel value must be populated");
+
+ // Get data from METADATA
+ const struct WheelValue meta_name = wheel_get_value_by_name(wheel, WHEEL_FROM_METADATA, "Metadata-Version");
+ STASIS_ASSERT(meta_name.type == WHEELVAL_STR, "Wheel metadata version value must be a string");
+ STASIS_ASSERT_FATAL(meta_name.data != NULL, "Wheel metadata version value must not be NULL");
+ STASIS_ASSERT(meta_name.count, "Wheel metadata version value must be populated");
+
+ wheel_package_free(&wheel);
+ STASIS_ASSERT(wheel == NULL, "wheel struct should be NULL after free");
+}
+
+static void mock_python_package() {
+ const char *pyproject_toml_data = "[build-system]\n"
+ "requires = [\"setuptools >= 77.0.3\"]\n"
+ "build-backend = \"setuptools.build_meta\"\n"
+ "\n"
+ "[project]\n"
+ "name = \"testpkg\"\n"
+ "version = \"1.0.0\"\n"
+ "authors = [{name = \"STASIS Team\", email = \"stasis@not-a-real-domain.tld\"}]\n"
+ "description = \"A STASIS test package\"\n"
+ "readme = \"README.md\"\n"
+ "license = \"BSD-3-Clause\"\n"
+ "classifiers = [\"Programming Language :: Python :: 3\"]\n"
+ "\n"
+ "[project.urls]\n"
+ "Homepage = \"https://not-a-real-address.tld\"\n"
+ "Documentation = \"https://not-a-real-address.tld/docs\"\n"
+ "Repository = \"https://not-a-real-address.tld/repo.git\"\n"
+ "Issues = \"https://not-a-real-address.tld/tracker\"\n"
+ "Changelog = \"https://not-a-real-address.tld/changes\"\n";
+ const char *readme = "# testpkg\n\nThis is a test package, for testing.\n";
- struct Wheel *doesnotexist = get_wheel_info("doesnotexist", "doesnotexist-0.0.1-py2.py3-none-any.whl", (char *[]) {"not", NULL}, WHEEL_MATCH_ANY);
- STASIS_ASSERT(doesnotexist == NULL, "returned non-NULL on error");
-
- for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
- struct testcase *test = &tc[i];
- struct Wheel *wheel = get_wheel_info(".", test->expected.distribution, (char *[]) {(char *) test->expected.version, NULL}, WHEEL_MATCH_ANY);
- STASIS_ASSERT(wheel != NULL, "result should not be NULL!");
- STASIS_ASSERT(wheel->file_name && strcmp(wheel->file_name, test->expected.file_name) == 0, "mismatched file name");
- STASIS_ASSERT(wheel->version && strcmp(wheel->version, test->expected.version) == 0, "mismatched version");
- STASIS_ASSERT(wheel->distribution && strcmp(wheel->distribution, test->expected.distribution) == 0, "mismatched distribution (package name)");
- STASIS_ASSERT(wheel->platform_tag && strcmp(wheel->platform_tag, test->expected.platform_tag) == 0, "mismatched platform tag ([platform]_[architecture])");
- STASIS_ASSERT(wheel->python_tag && strcmp(wheel->python_tag, test->expected.python_tag) == 0, "mismatched python tag (python version)");
- STASIS_ASSERT(wheel->abi_tag && strcmp(wheel->abi_tag, test->expected.abi_tag) == 0, "mismatched abi tag (python compatibility version)");
- if (wheel->build_tag) {
- STASIS_ASSERT(strcmp(wheel->build_tag, test->expected.build_tag) == 0,
- "mismatched build tag (optional arbitrary string)");
- }
- wheel_free(&wheel);
+ mkdir("testpkg", 0755);
+ mkdir("testpkg/src", 0755);
+ mkdir("testpkg/src/testpkg", 0755);
+ if (touch("testpkg/src/testpkg/__init__.py")) {
+ fprintf(stderr, "unable to write __init__.py");
+ exit(1);
+ }
+ if (touch("testpkg/README.md")) {
+ fprintf(stderr, "unable to write README.md");
+ exit(1);
+ }
+ if (stasis_testing_write_ascii("testpkg/pyproject.toml", pyproject_toml_data)) {
+ perror("unable to write pyproject.toml");
+ exit(1);
+ }
+ if (stasis_testing_write_ascii("testpkg/README.md", readme)) {
+ perror("unable to write readme");
+ exit(1);
+ }
+ if (pip_exec("install build")) {
+ fprintf(stderr, "unable to install build tool using pip\n");
+ exit(1);
+ }
+ if (python_exec("-m build -w ./testpkg")) {
+ fprintf(stderr, "unable build test package");
+ exit(1);
}
}
int main(int argc, char *argv[]) {
STASIS_TEST_BEGIN_MAIN();
STASIS_TEST_FUNC *tests[] = {
- test_get_wheel_file,
+ test_wheel_package,
};
- // Create mock package directories, and files
- mkdir("binpackage", 0755);
- touch("binpackage/binpackage-1.2.3-cp311-cp311-linux_x86_64.whl");
- mkdir("anypackage", 0755);
- touch("anypackage/anypackage-1.2.3-py2.py3-none-any.whl");
- mkdir("btpackage", 0755);
- touch("btpackage/btpackage-1.2.3-mytag-py2.py3-none-any.whl");
+ char ws[] = "workspace_XXXXXX";
+ if (!mkdtemp(ws)) {
+ perror("mkdtemp");
+ exit(1);
+ }
+ getcwd(cwd_start, sizeof(cwd_start) - 1);
+ mkdir(ws, 0755);
+ chdir(ws);
+ getcwd(cwd_workspace, sizeof(cwd_workspace) - 1);
+
+ snprintf(conda_prefix, strlen(cwd_workspace) + strlen("conda") + 2, "%s/conda", cwd_workspace);
+
+ const char *mockinidata = "[meta]\n"
+ "name = mock\n"
+ "version = 1.0.0\n"
+ "rc = 1\n"
+ "mission = generic\n"
+ "python = 3.11\n"
+ "[conda]\n"
+ "installer_name = Miniforge3\n"
+ "installer_version = 24.3.0-0\n"
+ "installer_platform = {{env:STASIS_CONDA_PLATFORM}}\n"
+ "installer_arch = {{env:STASIS_CONDA_ARCH}}\n"
+ "installer_baseurl = https://github.com/conda-forge/miniforge/releases/download/24.3.0-0\n";
+ stasis_testing_write_ascii("mock.ini", mockinidata);
+ struct INIFILE *ini = ini_open("mock.ini");
+ ctx._stasis_ini_fp.delivery = ini;
+ ctx._stasis_ini_fp.delivery_path = realpath("mock.ini", NULL);
+
+ const char *sysconfdir = getenv("STASIS_SYSCONFDIR");
+ globals.sysconfdir = strdup(sysconfdir ? sysconfdir : STASIS_SYSCONFDIR);
+ ctx.storage.root = strdup(cwd_workspace);
+ char *cfgfile = join((char *[]) {globals.sysconfdir, "stasis.ini", NULL}, "/");
+ if (!cfgfile) {
+ perror("unable to create path to global config");
+ exit(1);
+ }
+
+ ctx._stasis_ini_fp.cfg = ini_open(cfgfile);
+ if (!ctx._stasis_ini_fp.cfg) {
+ fprintf(stderr, "unable to open config file, %s\n", cfgfile);
+ exit(1);
+ }
+ ctx._stasis_ini_fp.cfg_path = realpath(cfgfile, NULL);
+ if (!ctx._stasis_ini_fp.cfg_path) {
+ fprintf(stderr, "unable to determine absolute path of config, %s\n", cfgfile);
+ exit(1);
+ }
+ guard_free(cfgfile);
+
+ setenv("LANG", "C", 1);
+ if (bootstrap_build_info(&ctx)) {
+ fprintf(stderr, "bootstrap_build_info failed\n");
+ exit(1);
+ }
+ if (delivery_init(&ctx, INI_READ_RENDER)) {
+ fprintf(stderr, "delivery_init failed\n");
+ exit(1);
+ }
+
+ char *install_url = calloc(255, sizeof(install_url));
+ delivery_get_conda_installer_url(&ctx, install_url, PATH_MAX);
+ delivery_get_conda_installer(&ctx, install_url);
+ delivery_install_conda(ctx.conda.installer_path, ctx.storage.conda_install_prefix);
+ guard_free(install_url);
+
+ if (conda_activate(ctx.storage.conda_install_prefix, "base")) {
+ fprintf(stderr, "conda_activate failed\n");
+ exit(1);
+ }
+ if (conda_exec("install -y boa conda-build")) {
+ fprintf(stderr, "conda_exec failed\n");
+ exit(1);
+ }
+ if (conda_setup_headless()) {
+ fprintf(stderr, "conda_setup_headless failed\n");
+ exit(1);
+ }
+ if (conda_env_create("testpkg", ctx.meta.python, NULL)) {
+ fprintf(stderr, "conda_env_create failed\n");
+ exit(1);
+ }
+ if (conda_activate(ctx.storage.conda_install_prefix, "testpkg")) {
+ fprintf(stderr, "conda_activate failed\n");
+ exit(1);
+ }
+
+ mock_python_package();
STASIS_TEST_RUN(tests);
+
+ if (chdir(cwd_start) < 0) {
+ fprintf(stderr, "chdir failed\n");
+ exit(1);
+ }
+ if (rmtree(cwd_workspace)) {
+ perror(cwd_workspace);
+ }
+ delivery_free(&ctx);
+ globals_free();
+
STASIS_TEST_END_MAIN();
+
} \ No newline at end of file
diff --git a/tests/test_wheelinfo.c b/tests/test_wheelinfo.c
new file mode 100644
index 0000000..1abbeac
--- /dev/null
+++ b/tests/test_wheelinfo.c
@@ -0,0 +1,91 @@
+#include "testing.h"
+#include "wheelinfo.h"
+
+void test_wheelinfo_get() {
+ struct testcase {
+ const char *filename;
+ struct WheelInfo expected;
+ };
+ struct testcase tc[] = {
+ {
+ // Test for "build tags"
+ .filename = "btpackage-1.2.3-mytag-py2.py3-none-any.whl",
+ .expected = {
+ .file_name = "btpackage-1.2.3-mytag-py2.py3-none-any.whl",
+ .version = "1.2.3",
+ .distribution = "btpackage",
+ .build_tag = "mytag",
+ .platform_tag = "any",
+ .python_tag = "py2.py3",
+ .abi_tag = "none",
+ .path_name = ".",
+ }
+ },
+ {
+ // Test for universal package format
+ .filename = "anypackage-1.2.3-py2.py3-none-any.whl",
+ .expected = {
+ .file_name = "anypackage-1.2.3-py2.py3-none-any.whl",
+ .version = "1.2.3",
+ .distribution = "anypackage",
+ .build_tag = NULL,
+ .platform_tag = "any",
+ .python_tag = "py2.py3",
+ .abi_tag = "none",
+ .path_name = ".",
+ }
+ },
+ {
+ // Test for binary package format
+ .filename = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl",
+ .expected = {
+ .file_name = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl",
+ .version = "1.2.3",
+ .distribution = "binpackage",
+ .build_tag = NULL,
+ .platform_tag = "linux_x86_64",
+ .python_tag = "cp311",
+ .abi_tag = "cp311",
+ .path_name = ".",
+ }
+ },
+ };
+
+ struct WheelInfo *doesnotexist = wheelinfo_get("doesnotexist", "doesnotexist-0.0.1-py2.py3-none-any.whl", (char *[]) {"not", NULL}, WHEEL_MATCH_ANY);
+ STASIS_ASSERT(doesnotexist == NULL, "returned non-NULL on error");
+
+ for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
+ struct testcase *test = &tc[i];
+ struct WheelInfo *wheel = wheelinfo_get(".", test->expected.distribution, (char *[]) {(char *) test->expected.version, NULL}, WHEEL_MATCH_ANY);
+ STASIS_ASSERT(wheel != NULL, "result should not be NULL!");
+ STASIS_ASSERT(wheel->file_name && strcmp(wheel->file_name, test->expected.file_name) == 0, "mismatched file name");
+ STASIS_ASSERT(wheel->version && strcmp(wheel->version, test->expected.version) == 0, "mismatched version");
+ STASIS_ASSERT(wheel->distribution && strcmp(wheel->distribution, test->expected.distribution) == 0, "mismatched distribution (package name)");
+ STASIS_ASSERT(wheel->platform_tag && strcmp(wheel->platform_tag, test->expected.platform_tag) == 0, "mismatched platform tag ([platform]_[architecture])");
+ STASIS_ASSERT(wheel->python_tag && strcmp(wheel->python_tag, test->expected.python_tag) == 0, "mismatched python tag (python version)");
+ STASIS_ASSERT(wheel->abi_tag && strcmp(wheel->abi_tag, test->expected.abi_tag) == 0, "mismatched abi tag (python compatibility version)");
+ if (wheel->build_tag) {
+ STASIS_ASSERT(strcmp(wheel->build_tag, test->expected.build_tag) == 0,
+ "mismatched build tag (optional arbitrary string)");
+ }
+ wheelinfo_free(&wheel);
+ }
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ STASIS_TEST_FUNC *tests[] = {
+ test_wheelinfo_get,
+ };
+
+ // Create mock package directories, and files
+ mkdir("binpackage", 0755);
+ touch("binpackage/binpackage-1.2.3-cp311-cp311-linux_x86_64.whl");
+ mkdir("anypackage", 0755);
+ touch("anypackage/anypackage-1.2.3-py2.py3-none-any.whl");
+ mkdir("btpackage", 0755);
+ touch("btpackage/btpackage-1.2.3-mytag-py2.py3-none-any.whl");
+
+ STASIS_TEST_RUN(tests);
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file