diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-08-13 15:34:57 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-13 15:34:57 -0400 |
commit | 4550b38cd0db6b89c913af0b3444dfd3f1beba36 (patch) | |
tree | c097c85ba0ea737e3080862b22adbf2a157a16be | |
parent | 95672b2e7a6cc0c07306893d5bb0b80ee3570f7a (diff) | |
download | stasis-4550b38cd0db6b89c913af0b3444dfd3f1beba36.tar.gz |
Add test_recipe.c (#25)
* Add test_recipe.c
* Unit test for recipe functions
* Add error cases
* Add missing explicit members in testcase array definition
-rw-r--r-- | tests/test_recipe.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/test_recipe.c b/tests/test_recipe.c new file mode 100644 index 0000000..8e2c470 --- /dev/null +++ b/tests/test_recipe.c @@ -0,0 +1,100 @@ +#include "testing.h" + +static void make_local_recipe(const char *localdir) { + char path[PATH_MAX] = {0}; + sprintf(path, "./%s", localdir); + mkdir(path, 0755); + if (!pushd(path)) { + touch("meta.yaml"); + touch("build.sh"); + system("git init ."); + system("git config --local user.name test"); + system("git config --local user.email test@test.tld"); + system("git config --local commit.gpgsign false"); + system("git add meta.yaml build.sh"); + system("git commit -m \"initial commit\""); + popd(); + } else { + SYSERROR("failed to enter directory: %s (%s)", localdir, strerror(errno)); + } +} + +void test_recipe_clone() { + struct testcase { + char *recipe_dir; + char *url; + char *gitref; + int expect_type; + int expect_return; + }; + struct testcase tc[] = { + {.recipe_dir = "recipe_condaforge", + .url = "https://github.com/conda-forge/fitsverify-feedstock", + .gitref = "HEAD", + .expect_type = RECIPE_TYPE_CONDA_FORGE, + .expect_return = 0}, + {.recipe_dir = "recipe_astroconda", + .url = "https://github.com/astroconda/astroconda-contrib", + .gitref = "HEAD", + .expect_type = RECIPE_TYPE_ASTROCONDA, + .expect_return = 0}, + {.recipe_dir = "recipe_generic", + .url = "local_repo", + .gitref = "HEAD", + .expect_type = RECIPE_TYPE_GENERIC, + .expect_return = 0}, + {.recipe_dir = "recipe_unknown", + .url = "https://github.com/astroconda/firewatch", + .gitref = "HEAD", + .expect_type = RECIPE_TYPE_UNKNOWN, + .expect_return = 0}, + {.recipe_dir = "recipe_broken", + .url = "123_BAD_BAD_BAD_456", + .gitref = "HEAD", + .expect_type = RECIPE_TYPE_UNKNOWN, + .expect_return = 128}, + }; + for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) { + struct testcase *test = &tc[i]; + if (test->recipe_dir && strcmp(test->recipe_dir, "/") != 0 && access(test->recipe_dir, F_OK) == 0) { + rmtree(test->recipe_dir); + } + if (strstr(test->recipe_dir, "_generic")) { + rmtree("local_repo"); + make_local_recipe("local_repo"); + } + + char *result_path = NULL; + int result = 0; + + // Clone the repository + result = recipe_clone(test->recipe_dir, test->url, test->gitref, &result_path); + // Ensure git didn't return an error + STASIS_ASSERT(result == test->expect_return, "failed while cloning recipe"); + // Ensure a path to the repository was returned in the result argument + STASIS_ASSERT(result_path != NULL, "result path should not be NULL"); + // Verify the repository was detected as the correct recipe type + STASIS_ASSERT(recipe_get_type(result_path) == test->expect_type, "repository detected as the wrong type"); + + if (test->expect_return == 0) { + // Verify the result path exists + STASIS_ASSERT(result_path && access(result_path, F_OK) == 0, "result path should be a valid directory"); + } else { + // Verify the result path does not exist + STASIS_ASSERT(result_path && access(result_path, F_OK) != 0, "result path should not exist"); + } + } + +} + +int main(int argc, char *argv[]) { + STASIS_TEST_BEGIN_MAIN(); + STASIS_TEST_FUNC *tests[] = { + test_recipe_clone, + }; + mkdir("workspace", 0755); + pushd("workspace"); + STASIS_TEST_RUN(tests); + popd(); + STASIS_TEST_END_MAIN(); +}
\ No newline at end of file |