diff --git a/.gitignore b/.gitignore index 433be5c..dc906be 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ tests/cache/4/* .meta build/luasrcdiet* + + +.vscode/* \ No newline at end of file diff --git a/src/am.lua b/src/am.lua index 4048798..592ba80 100644 --- a/src/am.lua +++ b/src/am.lua @@ -100,6 +100,33 @@ function am.__parse_base_args(args, options) return am.parse_args(_interface.new("base"), args, options) end +---Configures ami cache location +---@param cache string +function am.configure_cache(cache) + if type(cache) == "string" then + am.options.CACHE_DIR = cache + else + if cache ~= nil then + log_warn("Invalid cache directory: " .. tostring(cache)) + end + + local custom_cache_path = true + local cache_path = os.getenv("AMI_CACHE") + if not cache_path then + cache_path = "/var/cache/ami" + custom_cache_path = false + end + am.options.CACHE_DIR = cache_path + + --fallback to local dir in case we have no access to global one + if not fs.safe_write_file(path.combine(tostring(am.options.CACHE_DIR), ".ami-test-access"), "") then + local log = custom_cache_path and log_error or log_debug + log("Access to '" .. am.options.CACHE_DIR .. "' denied! Using local '.ami-cache' directory.") + am.options.CACHE_DIR = ".ami-cache" + end + end +end + ---@class AmiPrintHelpOptions ---#DES am.print_help() diff --git a/src/ami.lua b/src/ami.lua index 6d77e38..d949d66 100644 --- a/src/ami.lua +++ b/src/ami.lua @@ -39,28 +39,7 @@ if _parsedOptions.path then end end -if type(_parsedOptions.cache) == "string" then - am.options.CACHE_DIR = _parsedOptions.cache -else - if _parsedOptions.cache ~= nil then - log_warn("Invalid cache directory: " .. tostring(_parsedOptions.cache)) - end - - local custom_cache_path = true - local cache_path = os.getenv("AMI_CACHE") - if not cache_path then - cache_path = "/var/cache/ami" - custom_cache_path = false - end - am.options.CACHE_DIR = cache_path - - --fallback to local dir in case we have no access to global one - if not fs.safe_write_file(path.combine(tostring(am.options.CACHE_DIR), ".ami-test-access"), "") then - local log = custom_cache_path and log_error or log_debug - log("Access to '" .. am.options.CACHE_DIR .. "' denied! Using local '.ami-cache' directory.") - am.options.CACHE_DIR = ".ami-cache" - end -end +am.configure_cache(_parsedOptions.cache --[[ @as string ]]) am.cache.init() if _parsedOptions["cache-timeout"] then @@ -93,7 +72,7 @@ if _parsedOptions["no-integrity-checks"] then end if _parsedOptions["base"] then - if type(_parsedOptions["base"]) ~= "string" then + if type(_parsedOptions["base"]) ~= "string" then log_error("Invalid base interface: " .. tostring(_parsedOptions["base"])) os.exit(EXIT_INVALID_AMI_BASE_INTERFACE) end @@ -127,7 +106,7 @@ if _parsedOptions["dry-run"] then local _ok, _appConfig = hjson.safe_parse(_parsedOptions["dry-run-config"]) if _ok then -- model is valid json am.app.__set(_appConfig) - else -- model is not valid json fallback to path + else -- model is not valid json fallback to path am.app.load_configuration(tostring(_parsedOptions["dry-run-config"])) end end diff --git a/tests/test/am.lua b/tests/test/am.lua index d734a7e..89c18b3 100644 --- a/tests/test/am.lua +++ b/tests/test/am.lua @@ -139,9 +139,11 @@ test["parse_args"] = function() am.__set_interface(_interface) local _args = { "test", "-to=randomOption" } - test.assert(hash.sha256sum(hjson.stringify({ am.parse_args(_args) }, { invalidObjectsAsType = true, indent = false, sortKeys = true }), true) == "39e8e5febeee2a65653b97914971cf0269ba34ce8a801851f10ec9be3d7992a1") + test.assert(hash.sha256sum(hjson.stringify({ am.parse_args(_args) }, { invalidObjectsAsType = true, indent = false, sortKeys = true }), true) == + "39e8e5febeee2a65653b97914971cf0269ba34ce8a801851f10ec9be3d7992a1") local _args = { "test", "-to=randomOption", "test2", "--test3=xxx" } - test.assert(hash.sha256sum(hjson.stringify({ am.parse_args(_args) }, { invalidObjectsAsType = true, indent = false, sortKeys = true }), true) == "173e8397066e26357a14d99eb49de241dc52e2862ea7f403d4ab1fce2ab1262b") + test.assert(hash.sha256sum(hjson.stringify({ am.parse_args(_args) }, { invalidObjectsAsType = true, indent = false, sortKeys = true }), true) == + "173e8397066e26357a14d99eb49de241dc52e2862ea7f403d4ab1fce2ab1262b") local _args = { "-to=randomOption", "test2", "--test3=xxx" } local _errorHit = false @@ -206,6 +208,79 @@ test["print_help"] = function() print = _originalPrint end + +test["configure_cache"] = function() + local _originalOsGetEnv = os.getenv + local _originalSafeWriteFile = fs.safe_write_file + local _originalLogWarn = log_warn + local _originalLogError = log_error + local _originalLogDebug = log_debug + + fs.safe_write_file = function(file_path, _) + if file_path == "/var/cache/ami/.ami-test-access" then + return true -- Simulating access to global cache directory + end + return true + end + + local log_messages = {} + log_warn = function(msg) + table.insert(log_messages, "WARN: " .. msg) + end + + log_debug = function(msg) + table.insert(log_messages, "DEBUG: " .. msg) + end + + -- Test Case 1: Valid cache directory + am.configure_cache("/custom/cache/path") + test.assert(am.options.CACHE_DIR == "/custom/cache/path") + + os.getenv = function(var) + if var == "AMI_CACHE" then + return '/custom/cache/from/env/variable' -- Simulating environment variable not set + end + end + + -- Test Case 2: AMI_CACHE set and no cache path set from commandline + am.configure_cache(nil) + test.assert(am.options.CACHE_DIR == "/custom/cache/from/env/variable") + + os.getenv = function(var) + if var == "AMI_CACHE" then + return nil -- Simulating environment variable not set + end + end + + -- Test Case 3: Invalid cache directory (non-string) + am.configure_cache(123) + test.assert(am.options.CACHE_DIR == "/var/cache/ami") + test.assert(#log_messages > 0 and log_messages[1] == "WARN: Invalid cache directory: 123") + + -- Test Case 4: Access to global cache + am.configure_cache(nil) + test.assert(am.options.CACHE_DIR == "/var/cache/ami") + + fs.safe_write_file = function(file_path, _) + if file_path == "/var/cache/ami/.ami-test-access" then + return false -- Simulating no access to global cache directory + end + return true + end + + -- Test Case 5: No access to global cache, fallback to local + am.configure_cache(nil) + test.assert(am.options.CACHE_DIR:match("%.ami%-cache")) + test.assert(#log_messages > 1 and log_messages[2] == "DEBUG: Access to '/var/cache/ami' denied! Using local '.ami-cache' directory.") + + -- Restore original functions + os.getenv = _originalOsGetEnv + fs.safe_write_file = _originalSafeWriteFile + log_warn = _originalLogWarn + log_error = _originalLogError + log_debug = _originalLogDebug +end + if not TEST then test.summary() end