Skip to content

Commit

Permalink
tests: Start with getting the tests working. Incorporate some feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
nouwaarom committed Mar 27, 2021
1 parent c063070 commit 4f9d33a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CC ?= gcc
CC ?= cc
PREFIX ?= /usr/local

BINS = clib clib-install clib-search clib-init clib-configure clib-build clib-update clib-upgrade clib-uninstall
Expand All @@ -12,7 +12,7 @@ RM = rm -f
MKDIR = mkdir -p

SRC = $(wildcard src/*.c)
COMMON_SRC = $(wildcard src/common/*.c src/registry/*.c src/repository/*)
COMMON_SRC = $(wildcard src/common/*.c src/registry/*.c src/repository/*.c)
ALL_SRC = $(wildcard src/*.c src/*.h src/common/*.c src/common/*.h src/registry/*.c src/registry/*.h src/repository/*.h src/repository/*.c test/package/*.c test/cache/*.c)
SDEPS = $(wildcard deps/*/*.c)
ODEPS = $(SDEPS:.c=.o)
Expand All @@ -21,7 +21,8 @@ OBJS = $(DEPS:.c=.o)

export CC

CFLAGS += -std=gnu17 -Ideps -Isrc/common -Isrc/repository -Isrc/registry -g -Wall -Werror=return-type -Wno-unused-function $(shell curl-config --cflags)
CFLAGS += -std=gnu17 -Ideps -Isrc/common -Isrc/repository -Isrc/registry
CFLAGS += -g -Wall -Werror=return-type -Wno-unused-function $(shell curl-config --cflags)

ifdef STATIC
CFLAGS += -DCURL_STATICLIB $(shell deps/curl/bin/curl-config --cflags)
Expand Down
7 changes: 5 additions & 2 deletions src/clib-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ static int install_package(const char *slug) {
}
}

registry_package_ptr_t package_info = registry_manger_find_package(registries, slug);
char* author = clib_package_parse_author(slug);
char* name = clib_package_parse_name(slug);
char* package_id = clib_package_get_id(author, name);
registry_package_ptr_t package_info = registry_manger_find_package(registries, package_id);
if (!package_info) {
debug(&debugger, "Package %s not found in any registry.", slug);
return -1;
Expand Down Expand Up @@ -432,7 +435,7 @@ int main(int argc, char *argv[]) {
root_package = clib_package_load_local_manifest(0);

repository_init(secrets); // The repository requires the secrets for authentication.
registries = registry_manager_init_registries(root_package->registries, secrets);
registries = registry_manager_init_registries(root_package ? root_package->registries : NULL, secrets);
registry_manager_fetch_registries(registries);

clib_package_installer_init(registries, secrets);
Expand Down
2 changes: 1 addition & 1 deletion src/common/clib-package-installer.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
list_node_t *source;
repository_file_handle_t *handles = malloc(pkg->src->len * sizeof(repository_file_handle_t));
while ((source = list_iterator_next(iterator))) {
handles[i] = repository_download_package_file(pkg->url, clib_package_get_id(pkg->author, pkg->name), pkg->version, source->val, pkg_dir);
handles[i] = repository_download_package_file(pkg->url, clib_package_get_id(pkg->author, pkg->repo_name), pkg->version, source->val, pkg_dir);

if (handles[i] == NULL) {
list_iterator_destroy(iterator);
Expand Down
5 changes: 4 additions & 1 deletion src/common/clib-secrets.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct clib_secret_handle {

clib_secrets_t clib_secrets_load_from_file(const char *file) {
if (-1 == fs_exists(file)) {
logger_error("error", "Secrets file %s does not exist.", file);
logger_warn("warning", "Secrets file %s does not exist.", file);
return NULL;
}

Expand Down Expand Up @@ -66,6 +66,9 @@ clib_secrets_t clib_secrets_load_from_file(const char *file) {
}

char *clib_secret_find_for_hostname(clib_secrets_t secrets, const char *hostname) {
if (secrets == NULL) {
return NULL;
}
list_iterator_t *iterator = list_iterator_new(secrets->secrets, LIST_HEAD);
list_node_t *node;
while ((node = list_iterator_next(iterator))) {
Expand Down
27 changes: 15 additions & 12 deletions src/registry/registry-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
registries_t registry_manager_init_registries(list_t* registry_urls, clib_secrets_t secrets) {
list_t* registries = list_new();

// Add all the registries that were provided.
list_iterator_t *registry_iterator = list_iterator_new(registry_urls, LIST_HEAD);
list_node_t *node;
while ((node = list_iterator_next(registry_iterator))) {
char* url = node->val;
url_data_t *parsed = url_parse(url);
char* hostname = strdup(parsed->hostname);
url_free(parsed);
char* secret = clib_secret_find_for_hostname(secrets, hostname);
registry_ptr_t registry = registry_create(url, secret);
list_rpush(registries, list_node_new(registry));
if (registry_urls != NULL) {
// Add all the registries that were provided.
list_iterator_t *registry_iterator = list_iterator_new(registry_urls, LIST_HEAD);
list_node_t *node;
while ((node = list_iterator_next(registry_iterator))) {
char *url = node->val;
url_data_t *parsed = url_parse(url);
char *secret = clib_secret_find_for_hostname(secrets, parsed->hostname);
url_free(parsed);
registry_ptr_t registry = registry_create(url, secret);
if (registry != NULL) {
list_rpush(registries, list_node_new(registry));
}
}
list_iterator_destroy(registry_iterator);
}
list_iterator_destroy(registry_iterator);

// And add the default registry.
registry_ptr_t registry = registry_create(CLIB_WIKI_URL, NULL);
Expand Down
15 changes: 9 additions & 6 deletions src/registry/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "list/list.h"
#include "registry-internal.h"
#include "url/url.h"
#include <logger/logger.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -56,21 +57,22 @@ registry_ptr_t registry_create(const char *url, const char *secret) {
registry_ptr_t registry = malloc(sizeof(struct registry_t));
registry->url = strdup(url);
registry->secret = strdup(secret);
registry->packages = NULL;
url_data_t *parsed = url_parse(url);
registry->hostname = strdup(parsed->hostname);
url_free(parsed);

if (strstr(url, "github.com") != NULL) {
if (strstr(registry->hostname, "github.com") != NULL) {
registry->type = REGISTRY_TYPE_GITHUB;
} else if (strstr(url, "gitlab") != NULL) {
} else if (strstr(registry->hostname, "gitlab") != NULL) {
registry->type = REGISTRY_TYPE_GITLAB;
} else {
logger_error("error", "Registry type (%s) not supported, currently github.com, gitlab.com and self-hosted gitlab are supported.", registry->url);
registry_free(registry);

return NULL;
}

url_data_t *parsed = url_parse(url);
registry->hostname = strdup(parsed->hostname);
url_free(parsed);

return registry;
}

Expand Down Expand Up @@ -112,6 +114,7 @@ bool registry_fetch(registry_ptr_t registry) {
return false;
}

logger_error("error", "Fetching package list from (%s) failed.", registry->url);
registry->packages = list_new();
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions test/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ ACTUAL=$(clib help install)
EXPECTED=$(clib install --help)

[ "$ACTUAL" = "$EXPECTED" ] || {
echo >&2 "\`help install\` should ouput clib-install --help"
echo >&2 "\`help install\` should output clib-install --help"
exit 1
}

ACTUAL=$(clib help search)
EXPECTED=$(clib search --help)

[ "$ACTUAL" = "$EXPECTED" ] || {
echo >&2 "\`help search\` should ouput clib-search --help"
echo >&2 "\`help search\` should output clib-search --help"
exit 1
}

0 comments on commit 4f9d33a

Please sign in to comment.