From 921a9de90aa0acf246a84903ff24c2c9723df112 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 28 Dec 2022 17:39:20 +0100 Subject: [PATCH 1/3] wip much better testing --- conf.v | 28 ++++++++++++++++++++++++---- int_test.v | 1 + string_test.v | 35 +++++++++++++++++++++++++++++++++++ test/empty.toml | 0 test/string.toml | 1 + 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 int_test.v create mode 100644 string_test.v create mode 100644 test/empty.toml create mode 100644 test/string.toml diff --git a/conf.v b/conf.v index c3865ca..3ebc194 100644 --- a/conf.v +++ b/conf.v @@ -2,6 +2,7 @@ module conf import os import toml +import datatypes { Set } // get_env_var tries to read the contents of the given environment variable. It // looks for either `${env.prefix}${field_name.to_upper()}` or @@ -54,7 +55,7 @@ pub fn load(conf LoadConfig) !T { // This array allows us to determine later whether the variable is actually // zero or just a null'ed struct field - mut has_value := map[string]bool{} + mut has_value := Set{} // Later, this could be read from an env var as well. path := conf.default_path @@ -77,7 +78,7 @@ pub fn load(conf LoadConfig) !T { // $compile_error('Unsupported config struct field type detected.') } - has_value[field.name] = true + has_value.add(field.name) } } } @@ -97,12 +98,31 @@ pub fn load(conf LoadConfig) !T { // $compile_error('Unsupported config struct field type detected.') } - has_value[field.name] = true + has_value.add(field.name) } - if !(has_value[field.name] or { false }) { + // Finally, if there's no env var present either, we check whether the + // variable has a default value + if !has_value.exists(field.name) { + mut has_default := false + + $if field.typ is string { + has_default = res.$(field.name) != '' + } $else $if field.typ is int { + has_default = res.$(field.name) != 0 + } + + if has_default { + has_value.add(field.name) + } + } + + // If there's no value provided in any way, we notify the user with an + // error. + if !has_value.exists(field.name) { return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") } } + return res } diff --git a/int_test.v b/int_test.v new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/int_test.v @@ -0,0 +1 @@ + diff --git a/string_test.v b/string_test.v new file mode 100644 index 0000000..c498b11 --- /dev/null +++ b/string_test.v @@ -0,0 +1,35 @@ +module conf + +struct SingleConf { + some_string string +} + +struct SingleConfDefault { + some_string string = 'default' +} + +fn test_string_present_no_default() { + conf := load(default_path: 'test/string.toml')! + assert conf == SingleConf{ + some_string: 'hi' + } +} + +fn test_string_absent_no_default() { + conf := load(default_path: 'test/empty.toml') or { return } + assert false +} + +fn test_string_present_default() { + conf := load(default_path: 'test/string.toml')! + assert conf == SingleConfDefault{ + some_string: 'hi' + } +} + +fn test_string_absent_default() { + conf := load(default_path: 'test/empty.toml')! + assert conf == SingleConfDefault{ + some_string: 'default' + } +} diff --git a/test/empty.toml b/test/empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/test/string.toml b/test/string.toml new file mode 100644 index 0000000..85c27d0 --- /dev/null +++ b/test/string.toml @@ -0,0 +1 @@ +some_string = "hi" From 44aa7c9dc95cb6a47cd904621fdd212063d0d8cb Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 28 Dec 2022 17:39:20 +0100 Subject: [PATCH 2/3] test: improve testing quality --- conf.v | 28 ++++++++++++++++++++++++---- string_test.v | 35 +++++++++++++++++++++++++++++++++++ test/empty.toml | 0 test/string.toml | 1 + 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 string_test.v create mode 100644 test/empty.toml create mode 100644 test/string.toml diff --git a/conf.v b/conf.v index c3865ca..3ebc194 100644 --- a/conf.v +++ b/conf.v @@ -2,6 +2,7 @@ module conf import os import toml +import datatypes { Set } // get_env_var tries to read the contents of the given environment variable. It // looks for either `${env.prefix}${field_name.to_upper()}` or @@ -54,7 +55,7 @@ pub fn load(conf LoadConfig) !T { // This array allows us to determine later whether the variable is actually // zero or just a null'ed struct field - mut has_value := map[string]bool{} + mut has_value := Set{} // Later, this could be read from an env var as well. path := conf.default_path @@ -77,7 +78,7 @@ pub fn load(conf LoadConfig) !T { // $compile_error('Unsupported config struct field type detected.') } - has_value[field.name] = true + has_value.add(field.name) } } } @@ -97,12 +98,31 @@ pub fn load(conf LoadConfig) !T { // $compile_error('Unsupported config struct field type detected.') } - has_value[field.name] = true + has_value.add(field.name) } - if !(has_value[field.name] or { false }) { + // Finally, if there's no env var present either, we check whether the + // variable has a default value + if !has_value.exists(field.name) { + mut has_default := false + + $if field.typ is string { + has_default = res.$(field.name) != '' + } $else $if field.typ is int { + has_default = res.$(field.name) != 0 + } + + if has_default { + has_value.add(field.name) + } + } + + // If there's no value provided in any way, we notify the user with an + // error. + if !has_value.exists(field.name) { return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") } } + return res } diff --git a/string_test.v b/string_test.v new file mode 100644 index 0000000..c498b11 --- /dev/null +++ b/string_test.v @@ -0,0 +1,35 @@ +module conf + +struct SingleConf { + some_string string +} + +struct SingleConfDefault { + some_string string = 'default' +} + +fn test_string_present_no_default() { + conf := load(default_path: 'test/string.toml')! + assert conf == SingleConf{ + some_string: 'hi' + } +} + +fn test_string_absent_no_default() { + conf := load(default_path: 'test/empty.toml') or { return } + assert false +} + +fn test_string_present_default() { + conf := load(default_path: 'test/string.toml')! + assert conf == SingleConfDefault{ + some_string: 'hi' + } +} + +fn test_string_absent_default() { + conf := load(default_path: 'test/empty.toml')! + assert conf == SingleConfDefault{ + some_string: 'default' + } +} diff --git a/test/empty.toml b/test/empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/test/string.toml b/test/string.toml new file mode 100644 index 0000000..85c27d0 --- /dev/null +++ b/test/string.toml @@ -0,0 +1 @@ +some_string = "hi" From 997e9611eb7a397d8bccb35b9a2545d735f7625c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 28 Dec 2022 18:13:20 +0100 Subject: [PATCH 3/3] test: also test env vars --- conf.v | 37 ++++++++++++++--------------- string_test.v | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/conf.v b/conf.v index 3ebc194..c7a0e0d 100644 --- a/conf.v +++ b/conf.v @@ -4,16 +4,25 @@ import os import toml import datatypes { Set } +[params] +pub struct LoadConfig { + prefix string + file_suffix string = '_FILE' + default_path string + // Allows overwriting + env map[string]string = os.environ() +} + // get_env_var tries to read the contents of the given environment variable. It // looks for either `${env.prefix}${field_name.to_upper()}` or // `${env.prefix}${field_name.to_upper()}${env.file_suffix}`, returning the // contents of the file instead if the latter. If both or neither exist, the // function returns an error. -fn get_env_var(prefix string, field_name string, file_suffix string) !string { - env_var_name := '$prefix$field_name.to_upper()' - env_file_name := '$prefix$field_name.to_upper()$file_suffix' - env_var := os.getenv(env_var_name) - env_file := os.getenv(env_file_name) +fn (ld LoadConfig) get_env_var(field_name string) !string { + env_var_name := '$ld.prefix$field_name.to_upper()' + env_file_name := '$ld.prefix$field_name.to_upper()$ld.file_suffix' + env_var := ld.env[env_var_name] or { '' } + env_file := ld.env[env_file_name] or { '' } // If both are missing, we return an empty string if env_var == '' && env_file == '' { @@ -39,18 +48,11 @@ fn get_env_var(prefix string, field_name string, file_suffix string) !string { } } -[params] -pub struct LoadConfig { - prefix string - file_suffix string = '_FILE' - default_path string -} - // load attempts to create an object of type T from the given path to a toml // file & environment variables. For each field, it will select either a value // given from an environment variable, a value defined in the config file or a // configured default if present, in that order. -pub fn load(conf LoadConfig) !T { +pub fn load(ld LoadConfig) !T { mut res := T{} // This array allows us to determine later whether the variable is actually @@ -58,7 +60,7 @@ pub fn load(conf LoadConfig) !T { mut has_value := Set{} // Later, this could be read from an env var as well. - path := conf.default_path + path := ld.default_path if os.exists(path) { // We don't use reflect here because reflect also sets any fields not @@ -84,7 +86,7 @@ pub fn load(conf LoadConfig) !T { } $for field in T.fields { - env_value := get_env_var(conf.prefix, field.name, conf.file_suffix)! + env_value := ld.get_env_var(field.name)! // The value of an env var will always take precedence over the toml // file. @@ -117,12 +119,11 @@ pub fn load(conf LoadConfig) !T { } } - // If there's no value provided in any way, we notify the user with an - // error. + // If there's no value provided in any way, we notify the user with an + // error. if !has_value.exists(field.name) { return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") } } - return res } diff --git a/string_test.v b/string_test.v index c498b11..b91cff5 100644 --- a/string_test.v +++ b/string_test.v @@ -8,6 +8,14 @@ struct SingleConfDefault { some_string string = 'default' } +const env = { + 'SOME_STRING': 'env' +} + +const prefix_env = { + 'TEST_SOME_STRING': 'env' +} + fn test_string_present_no_default() { conf := load(default_path: 'test/string.toml')! assert conf == SingleConf{ @@ -15,11 +23,35 @@ fn test_string_present_no_default() { } } +fn test_string_present_no_default_env() { + conf := load(default_path: 'test/string.toml', env: .env)! + assert conf == SingleConf{ + some_string: 'env' + } + + conf2 := load(default_path: 'test/string.toml', env: .prefix_env, prefix: 'TEST_')! + assert conf2 == SingleConf{ + some_string: 'env' + } +} + fn test_string_absent_no_default() { conf := load(default_path: 'test/empty.toml') or { return } assert false } +fn test_string_absent_no_default_env() { + conf := load(default_path: 'test/string.toml', env: .env)! + assert conf == SingleConf{ + some_string: 'env' + } + + conf2 := load(default_path: 'test/string.toml', env: .prefix_env, prefix: 'TEST_')! + assert conf2 == SingleConf{ + some_string: 'env' + } +} + fn test_string_present_default() { conf := load(default_path: 'test/string.toml')! assert conf == SingleConfDefault{ @@ -27,9 +59,41 @@ fn test_string_present_default() { } } +fn test_string_present_default_env() { + conf := load(default_path: 'test/string.toml', env: .env)! + assert conf == SingleConfDefault{ + some_string: 'env' + } + + conf2 := load( + default_path: 'test/string.toml' + env: .prefix_env + prefix: 'TEST_' + )! + assert conf2 == SingleConfDefault{ + some_string: 'env' + } +} + fn test_string_absent_default() { conf := load(default_path: 'test/empty.toml')! assert conf == SingleConfDefault{ some_string: 'default' } } + +fn test_string_absent_default_env() { + conf := load(default_path: 'test/empty.toml', env: .env)! + assert conf == SingleConfDefault{ + some_string: 'env' + } + + conf2 := load( + default_path: 'test/empty.toml' + env: .prefix_env + prefix: 'TEST_' + )! + assert conf2 == SingleConfDefault{ + some_string: 'env' + } +}