From 8bed39700e4be64495dd935b8402be2dab4de2d2 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Tue, 28 Dec 2021 01:34:50 +0000 Subject: [PATCH] Adds a test for defaults and fixes bug relating to it --- src/config.c | 4 ++-- tests/config.test.c | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/config.c b/src/config.c index a36293e..d981152 100644 --- a/src/config.c +++ b/src/config.c @@ -37,7 +37,7 @@ Config* configDefaults(){ conf->database = "proxy.sqlite"; conf->port = 8080; conf->localConfig = "proxy.conf"; - //conf->userConfig = getDefaultUserConfigLoc(); + conf->userConfig = getDefaultUserConfigLoc(); return conf; } @@ -58,7 +58,7 @@ char* getDefaultUserConfigLoc(){ strcpy( configFile, configDir ); - strcat( configFile, configDir ); + strcat( configFile, "/proxy.conf" ); return strdup(configFile); } diff --git a/tests/config.test.c b/tests/config.test.c index 1cebe4d..ec077ad 100644 --- a/tests/config.test.c +++ b/tests/config.test.c @@ -6,7 +6,7 @@ /* * Ensures that path_exists returns true on current working dir (should to exist) */ -MunitResult path_that_exists(const MunitParameter params[], +MunitResult pathThatExists(const MunitParameter params[], void* user_data_or_fixture){ char cwd[500] = {'\0'}; getcwd(cwd,500); @@ -14,7 +14,7 @@ MunitResult path_that_exists(const MunitParameter params[], return MUNIT_OK; } -MunitResult path_that_doesnt_exist(const MunitParameter params[], +MunitResult pathThatDoesntExist(const MunitParameter params[], void* user_data_or_fixture){ char cwd[500] = {'\0'}; getcwd(cwd,500); @@ -23,14 +23,14 @@ MunitResult path_that_doesnt_exist(const MunitParameter params[], return MUNIT_OK; } -MunitResult tilde_resolves_correctly(const MunitParameter params[], +MunitResult tildeResolvesCorrectly(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[], +MunitResult checkConfigDirWithXdg(const MunitParameter params[], void* user_data_or_fixture){ char directory[] = "/testing/xdg/directory"; setenv( "XDG_CONFIG_HOME",directory, 1 ); @@ -38,7 +38,7 @@ MunitResult check_config_dir_with_xdg(const MunitParameter params[], return MUNIT_OK; } -MunitResult check_config_dir_without_xdg(const MunitParameter params[], +MunitResult checkConfigDirWithoutXdg(const MunitParameter params[], void* user_data_or_fixture){ unsetenv( "XDG_CONFIG_HOME" ); char dir[500] = {'\0'}; @@ -48,11 +48,25 @@ MunitResult check_config_dir_without_xdg(const MunitParameter params[], return MUNIT_OK; } +MunitResult checkDefaults(const MunitParameter params[], + void* user_data_or_fixture){ + + char directory[] = "/testing/xdg/directory"; + char file[] = "/testing/xdg/directory/proxy.conf"; + setenv( "XDG_CONFIG_HOME",directory, 1 ); + Config *conf = configDefaults(); + + munit_assert_string_equal(conf->database, "proxy.sqlite"); + munit_assert_int(conf->port, ==, 8080); + munit_assert_string_equal(conf->localConfig, "proxy.conf"); + munit_assert_string_equal(conf->userConfig, file); + return MUNIT_OK; +} MunitTest tests[] = { { "/path_exists/yes", /* name */ - path_that_exists, /* test */ + pathThatExists, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ @@ -60,7 +74,7 @@ MunitTest tests[] = { }, { "/path_exists/no", /* name */ - path_that_doesnt_exist, /* test */ + pathThatDoesntExist, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ @@ -68,7 +82,7 @@ MunitTest tests[] = { }, { "/TildeResolves", /* name */ - tilde_resolves_correctly, /* test */ + tildeResolvesCorrectly, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ @@ -76,7 +90,7 @@ MunitTest tests[] = { }, { "/ConfigDir/WithXDG", /* name */ - check_config_dir_with_xdg, /* test */ + checkConfigDirWithoutXdg, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ @@ -84,7 +98,15 @@ MunitTest tests[] = { }, { "/ConfigDir/WithoutXDG", /* name */ - check_config_dir_without_xdg, /* test */ + checkConfigDirWithoutXdg, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + { + "/ConfigDir/Defaults", /* name */ + checkDefaults, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */