-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

loadfile(os.getenv('CONFIG_FILE'), 't', { package = package, os = os })()
-- 增加mock的文件路径
local mc_utils = require('mc.utils')
local test_dir = mc_utils.realpath('.')
package.path = test_dir .. '/mock/?.lua;' .. test_dir .. '/test/unit/mock/?.lua;' .. package.path

local mc_context = require('mc.context')
local logging = require('mc.logging')
local utils_core = require('utils.core')
local vos_utils = require('utils.vos')
local lu = require('luaunit')

local current_file_dir = debug.getinfo(1).source:match('@?(.*)/')
utils_core.chdir(current_file_dir)
logging:setPrint(nil)
logging:setLevel(logging.INFO)

TestSpdm = {}

-- mock所有的路径配置
local function redirect_path() end

function TestSpdm:setupClass()
    self.test_data_dir = test_dir .. '/tmp'
    redirect_path()
    self:prepare_file()
    self.ctx = mc_context.new('UT', 'Administrator', '127.0.0.1')
    self.ctx.operation_log = { operation = nil, result = nil, params = {} }
    -- mock 一些内部实现的函数,需要在测试类初始化后进行
    self:mock()
end

function TestSpdm:prepare_file()
    mc_utils.mkdir(self.test_data_dir .. '/tmp', mc_utils.S_IRWXU)
    mc_utils.mkdir(self.test_data_dir .. '/dev/', mc_utils.S_IRWXU)
    mc_utils.mkdir(self.test_data_dir .. '/dev/shm', mc_utils.S_IRWXU)
    mc_utils.mkdir(self.test_data_dir .. '/dev/shm/tmp', mc_utils.S_IRWXU)
end

function TestSpdm:mock() end

function TestSpdm:teardownClass()
    mc_utils.remove_file(self.test_data_dir)
end

function TestSpdm:setUp() end

function TestSpdm:tearDown() end

-- mock app_preloader里面的excute_s函数
os.execute_s = function(cmd) -- luacheck: ignore 142
    local ret = vos_utils.check_before_system_s('/bin/sh', '-c', cmd)
    if ret ~= 0 then
        return false, 'exit', ret
    end
    return true, 'exit', ret
end

os.exit(lu.LuaUnit.run())