Add tests for config
I have started writing tests for the config functions. This has resulted in a few changes to the config code (tests working I guess) I have also added a special "all" config file which (as the name suggests) runs all test suites In the makefile I have added the compiled test files to the clean target and added targets for building and running tests
This commit is contained in:
parent
c046ac37a9
commit
e42705280c
4 changed files with 159 additions and 8 deletions
13
Makefile
13
Makefile
|
@ -2,7 +2,9 @@
|
|||
|
||||
CFILES = $(wildcard src/*.c)
|
||||
OBJFILES = $(CFILES:.c=.o)
|
||||
OUT = proxy
|
||||
TESTFILES = $(wildcard tests/*.c)
|
||||
TESTOUT = $(TESTFILES:.c=)
|
||||
OUT = yaip
|
||||
CFLAGS = -Wall
|
||||
LDLIBS = -lsqlite3
|
||||
CC = gcc
|
||||
|
@ -22,7 +24,14 @@ $(OUT): $(OBJFILES)
|
|||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $^
|
||||
|
||||
tests/%.test: tests/%.test.c tests/munit/munit.c
|
||||
$(CC) $? -o $@
|
||||
|
||||
|
||||
test-%: tests/%.test
|
||||
$<
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(OBJFILES) $(OUT)
|
||||
rm -f $(OBJFILES) $(OUT) $(TESTOUT)
|
||||
|
|
13
src/config.c
13
src/config.c
|
@ -14,18 +14,19 @@ static bool path_exists(const char *path) {
|
|||
*/
|
||||
static char* resolveTilde(const char *path) {
|
||||
static glob_t globbuf;
|
||||
static char *ret = NULL;
|
||||
|
||||
int res = glob(path, GLOB_TILDE, NULL, &globbuf);
|
||||
/* no match, or many wildcard matches are bad */
|
||||
if (res == GLOB_NOMATCH)
|
||||
return strdup(path);
|
||||
ret = strdup(path);
|
||||
else if (res != 0) {
|
||||
printf("glob() failed\n");
|
||||
return NULL;
|
||||
} else {
|
||||
return strdup(globbuf.gl_pathv[0]);
|
||||
ret = strdup(globbuf.gl_pathv[0]);
|
||||
}
|
||||
globfree(&globbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -36,7 +37,7 @@ Config* configDefaults(){
|
|||
conf->database = "proxy.sqlite";
|
||||
conf->port = 8080;
|
||||
conf->localConfig = "proxy.conf";
|
||||
conf->userConfig = getDefaultUserConfigLoc();
|
||||
//conf->userConfig = getDefaultUserConfigLoc();
|
||||
|
||||
return conf;
|
||||
}
|
||||
|
@ -51,7 +52,7 @@ char* getConfigDir(){
|
|||
|
||||
char* getDefaultUserConfigLoc(){
|
||||
char *configDir = getConfigDir();
|
||||
char *configFile[strlen(configDir) + 11];
|
||||
char configFile[strlen(configDir) + 11];
|
||||
|
||||
memset(configFile, '\0', strlen(configDir) + 11);
|
||||
|
||||
|
@ -59,6 +60,6 @@ char* getDefaultUserConfigLoc(){
|
|||
|
||||
strcat( configFile, configDir );
|
||||
|
||||
return configFile;
|
||||
return strdup(configFile);
|
||||
}
|
||||
|
||||
|
|
28
tests/all.test.c
Normal file
28
tests/all.test.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
// This stops the main definintion in the sub suites
|
||||
#define MAINTEST
|
||||
|
||||
#include "munit/munit.h"
|
||||
#include "config.test.c"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
|
||||
MunitSuite all_suites[] = {
|
||||
config_test_suite,
|
||||
{ NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE }
|
||||
};
|
||||
|
||||
MunitSuite all_test_suite = {
|
||||
"", /* name */
|
||||
NULL, /* tests */
|
||||
all_suites, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
};
|
||||
|
||||
return munit_suite_main(&all_test_suite, NULL, argc, argv);
|
||||
}
|
113
tests/config.test.c
Normal file
113
tests/config.test.c
Normal file
|
@ -0,0 +1,113 @@
|
|||
#include "munit/munit.h"
|
||||
#include "../src/config.c"
|
||||
#include <unistd.h> //This has getcwd
|
||||
#include <stdlib.h> //This has getenv
|
||||
|
||||
/*
|
||||
* Ensures that path_exists returns true on current working dir (should to exist)
|
||||
*/
|
||||
MunitResult path_that_exists(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
char cwd[500] = {'\0'};
|
||||
getcwd(cwd,500);
|
||||
munit_assert_true( path_exists( cwd ) );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult path_that_doesnt_exist(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
char cwd[500] = {'\0'};
|
||||
getcwd(cwd,500);
|
||||
strcat( cwd, "thisshoudldnotexist" );
|
||||
munit_assert_false( path_exists( cwd ) );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult tilde_resolves_correctly(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
munit_assert_string_equal( getenv("HOME"), resolveTilde("~") );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
|
||||
MunitResult check_config_dir_with_xdg(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
char directory[] = "/testing/xdg/directory";
|
||||
setenv( "XDG_CONFIG_HOME",directory, 1 );
|
||||
munit_assert_string_equal( directory, getConfigDir() );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult check_config_dir_without_xdg(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
unsetenv( "XDG_CONFIG_HOME" );
|
||||
char dir[500] = {'\0'};
|
||||
strcpy( dir, getenv("HOME") );
|
||||
strcat( dir, "/.config" );
|
||||
munit_assert_string_equal( dir, getConfigDir() );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
|
||||
MunitTest tests[] = {
|
||||
{
|
||||
"/path_exists/yes", /* name */
|
||||
path_that_exists, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
},
|
||||
{
|
||||
"/path_exists/no", /* name */
|
||||
path_that_doesnt_exist, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
},
|
||||
{
|
||||
"/TildeResolves", /* name */
|
||||
tilde_resolves_correctly, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
},
|
||||
{
|
||||
"/ConfigDir/WithXDG", /* name */
|
||||
check_config_dir_with_xdg, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
},
|
||||
{
|
||||
"/ConfigDir/WithoutXDG", /* name */
|
||||
check_config_dir_without_xdg, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
},
|
||||
/* Mark the end of the array with an entry where the test
|
||||
* function is NULL */
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
};
|
||||
|
||||
MunitSuite config_test_suite = {
|
||||
"/config", /* name */
|
||||
tests, /* tests */
|
||||
NULL, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
};
|
||||
|
||||
#ifndef MAINTEST
|
||||
#define MAINTEST
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
return munit_suite_main(&config_test_suite, NULL, argc, argv);
|
||||
}
|
||||
|
||||
#endif /* ifndef MAINTEST */
|
Loading…
Add table
Add a link
Reference in a new issue