Skip to content

Commit 4f9d33a

Browse files
committed
tests: Start with getting the tests working. Incorporate some feedback.
1 parent c063070 commit 4f9d33a

File tree

7 files changed

+40
-27
lines changed

7 files changed

+40
-27
lines changed

Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CC ?= gcc
1+
CC ?= cc
22
PREFIX ?= /usr/local
33

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

1414
SRC = $(wildcard src/*.c)
15-
COMMON_SRC = $(wildcard src/common/*.c src/registry/*.c src/repository/*)
15+
COMMON_SRC = $(wildcard src/common/*.c src/registry/*.c src/repository/*.c)
1616
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)
1717
SDEPS = $(wildcard deps/*/*.c)
1818
ODEPS = $(SDEPS:.c=.o)
@@ -21,7 +21,8 @@ OBJS = $(DEPS:.c=.o)
2121

2222
export CC
2323

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

2627
ifdef STATIC
2728
CFLAGS += -DCURL_STATICLIB $(shell deps/curl/bin/curl-config --cflags)

src/clib-install.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ static int install_package(const char *slug) {
278278
}
279279
}
280280

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

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

438441
clib_package_installer_init(registries, secrets);

src/common/clib-package-installer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
482482
list_node_t *source;
483483
repository_file_handle_t *handles = malloc(pkg->src->len * sizeof(repository_file_handle_t));
484484
while ((source = list_iterator_next(iterator))) {
485-
handles[i] = repository_download_package_file(pkg->url, clib_package_get_id(pkg->author, pkg->name), pkg->version, source->val, pkg_dir);
485+
handles[i] = repository_download_package_file(pkg->url, clib_package_get_id(pkg->author, pkg->repo_name), pkg->version, source->val, pkg_dir);
486486

487487
if (handles[i] == NULL) {
488488
list_iterator_destroy(iterator);

src/common/clib-secrets.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct clib_secret_handle {
1818

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

@@ -66,6 +66,9 @@ clib_secrets_t clib_secrets_load_from_file(const char *file) {
6666
}
6767

6868
char *clib_secret_find_for_hostname(clib_secrets_t secrets, const char *hostname) {
69+
if (secrets == NULL) {
70+
return NULL;
71+
}
6972
list_iterator_t *iterator = list_iterator_new(secrets->secrets, LIST_HEAD);
7073
list_node_t *node;
7174
while ((node = list_iterator_next(iterator))) {

src/registry/registry-manager.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
registries_t registry_manager_init_registries(list_t* registry_urls, clib_secrets_t secrets) {
1414
list_t* registries = list_new();
1515

16-
// Add all the registries that were provided.
17-
list_iterator_t *registry_iterator = list_iterator_new(registry_urls, LIST_HEAD);
18-
list_node_t *node;
19-
while ((node = list_iterator_next(registry_iterator))) {
20-
char* url = node->val;
21-
url_data_t *parsed = url_parse(url);
22-
char* hostname = strdup(parsed->hostname);
23-
url_free(parsed);
24-
char* secret = clib_secret_find_for_hostname(secrets, hostname);
25-
registry_ptr_t registry = registry_create(url, secret);
26-
list_rpush(registries, list_node_new(registry));
16+
if (registry_urls != NULL) {
17+
// Add all the registries that were provided.
18+
list_iterator_t *registry_iterator = list_iterator_new(registry_urls, LIST_HEAD);
19+
list_node_t *node;
20+
while ((node = list_iterator_next(registry_iterator))) {
21+
char *url = node->val;
22+
url_data_t *parsed = url_parse(url);
23+
char *secret = clib_secret_find_for_hostname(secrets, parsed->hostname);
24+
url_free(parsed);
25+
registry_ptr_t registry = registry_create(url, secret);
26+
if (registry != NULL) {
27+
list_rpush(registries, list_node_new(registry));
28+
}
29+
}
30+
list_iterator_destroy(registry_iterator);
2731
}
28-
list_iterator_destroy(registry_iterator);
2932

3033
// And add the default registry.
3134
registry_ptr_t registry = registry_create(CLIB_WIKI_URL, NULL);

src/registry/registry.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "list/list.h"
1212
#include "registry-internal.h"
1313
#include "url/url.h"
14+
#include <logger/logger.h>
1415
#include <stdlib.h>
1516
#include <string.h>
1617

@@ -56,21 +57,22 @@ registry_ptr_t registry_create(const char *url, const char *secret) {
5657
registry_ptr_t registry = malloc(sizeof(struct registry_t));
5758
registry->url = strdup(url);
5859
registry->secret = strdup(secret);
60+
registry->packages = NULL;
61+
url_data_t *parsed = url_parse(url);
62+
registry->hostname = strdup(parsed->hostname);
63+
url_free(parsed);
5964

60-
if (strstr(url, "github.com") != NULL) {
65+
if (strstr(registry->hostname, "github.com") != NULL) {
6166
registry->type = REGISTRY_TYPE_GITHUB;
62-
} else if (strstr(url, "gitlab") != NULL) {
67+
} else if (strstr(registry->hostname, "gitlab") != NULL) {
6368
registry->type = REGISTRY_TYPE_GITLAB;
6469
} else {
70+
logger_error("error", "Registry type (%s) not supported, currently github.com, gitlab.com and self-hosted gitlab are supported.", registry->url);
6571
registry_free(registry);
6672

6773
return NULL;
6874
}
6975

70-
url_data_t *parsed = url_parse(url);
71-
registry->hostname = strdup(parsed->hostname);
72-
url_free(parsed);
73-
7476
return registry;
7577
}
7678

@@ -112,6 +114,7 @@ bool registry_fetch(registry_ptr_t registry) {
112114
return false;
113115
}
114116

117+
logger_error("error", "Fetching package list from (%s) failed.", registry->url);
115118
registry->packages = list_new();
116119
return false;
117120
}

test/help.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ ACTUAL=$(clib help install)
1010
EXPECTED=$(clib install --help)
1111

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

1717
ACTUAL=$(clib help search)
1818
EXPECTED=$(clib search --help)
1919

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

0 commit comments

Comments
 (0)