Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ tests/cache/4/*
.meta

build/luasrcdiet*


.vscode/*
27 changes: 27 additions & 0 deletions src/am.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 3 additions & 24 deletions src/ami.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
79 changes: 77 additions & 2 deletions tests/test/am.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading