Skip to content

Commit

Permalink
Separate production from test configuration
Browse files Browse the repository at this point in the history
Existing setup could allow production values to override testing ones.
dspinellis committed Nov 24, 2023
1 parent a954f90 commit 4f90260
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -230,3 +230,13 @@ read_config(config_t *config)
// .aicliconfig
ini_checked_parse(hidden_config_name, config_handler, config);
}

/*
* Read the configuration file from the specified file path into config.
* This allows testing the config handler.
*/
void
read_file_config(config_t *config, const char *file_path)
{
ini_checked_parse(file_path, config_handler, config);
}
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -102,4 +102,6 @@ typedef struct {
} config_t;

void read_config(config_t *config);
void read_file_config(config_t *config, const char *file_path);

uaprompt_t prompt_find(config_t * config, const char *program_name);
8 changes: 5 additions & 3 deletions src/config_test.c
Original file line number Diff line number Diff line change
@@ -23,12 +23,14 @@
#include "CuTest.h"
#include "config.h"

static const char *LOCAL_CONFIG = "ai-cli-config";

void
test_read_config(CuTest* tc)
{
static config_t config;

read_config(&config);
read_file_config(&config, LOCAL_CONFIG);

CuAssertIntEquals(tc, 3, config.prompt_context);
CuAssertPtrNotNull(tc, config.openai_endpoint);
@@ -48,7 +50,7 @@ test_read_overloaded_config(CuTest* tc)
static config_t config;

putenv("AI_CLI_binding_vi=A");
read_config(&config);
read_file_config(&config, LOCAL_CONFIG);
CuAssertStrEquals(tc, "A", config.binding_vi);
}

@@ -59,7 +61,7 @@ test_read_env_added_config(CuTest* tc)
static config_t config;

putenv("AI_CLI_general_logfile=foo.log");
read_config(&config);
read_file_config(&config, LOCAL_CONFIG);
CuAssertStrEquals(tc, "foo.log", config.general_logfile);
}

0 comments on commit 4f90260

Please sign in to comment.