import os
import sys
import shutil
import unittest
from unittest import mock
THIS_FILE_NAME = __file__
FILE_PATH = os.path.dirname(os.path.realpath(THIS_FILE_NAME))
TOP_PATH = os.path.join(FILE_PATH, "../../../")
API_ROOT_PATH = os.path.join(
TOP_PATH, "build/adapter_ut")
FRAMEWORK_PATH = os.path.join(
TOP_PATH, "tools/build/")
sys.path.insert(0, FRAMEWORK_PATH)
import asc_op_compile_base
from asc_op_compile_base.common.platform import set_current_compile_soc_info, get_soc_spec
from asc_op_compile_base.common import register
from asc_op_compile_base.common import buildcfg
from asc_op_compile_base.common.utils.log_utils import LogUtil, AscendCLogLevel
from asc_op_compile_base.asc_op_compiler.global_storage import global_var_storage
def SetCurrentSocInfo(soc: str):
set_current_compile_soc_info(soc)
global_var_storage.set_variable("ascendc_short_soc_version", get_soc_spec("SHORT_SOC_VERSION"))
class TestLogUtils(unittest.TestCase):
def setUp(self):
print(f"-------------------SetUp----------------")
def tearDown(self):
print(f"-------------------TearDown-------------")
def test_dump_log_fail(self):
emptystr = ""
LogUtil.dump_log(emptystr)
self.assertEqual(emptystr, "")
def test_print_compile_log_lowlevel(self):
op_name = "test_op"
output = "a debug info"
with mock.patch('os.environ', {"ASCEND_GLOBAL_LOG_LEVEL": 3}):
LogUtil().print_compile_log(op_name, output, AscendCLogLevel.LOG_DEBUG)
def test_print_compile_plog_lowlevel(self):
op_name = "test_op"
output = "a debug info"
with mock.patch('os.environ', {"ASCEND_GLOBAL_LOG_LEVEL": 3}):
with mock.patch('os.environ', {"ASCEND_SLOG_PRINT_TO_STDOUT": 1}):
LogUtil().plog_print(op_name, output, AscendCLogLevel.LOG_DEBUG)
LogUtil().plog_print(op_name, output, AscendCLogLevel.LOG_INFO)
LogUtil().plog_print(op_name, output, AscendCLogLevel.LOG_WARNING)
LogUtil().plog_print(op_name, output, AscendCLogLevel.LOG_ERROR)
self.assertEqual(op_name, "test_op")
def test_detail_log_print(self):
op_name = "test_op"
output = "a detail info"
with mock.patch('os.environ', {"ASCEND_GLOBAL_EVENT_ENABLE": 1}):
LogUtil().detail_log_print(op_name, output, AscendCLogLevel.LOG_INFO)
def test_fix_string_escapes(self):
self.assertEqual(LogUtil.fix_string_escapes("Hellow World"), "Hellow World")
self.assertEqual(LogUtil.fix_string_escapes("中文字符!!!"), "中文字符!!!")
self.assertEqual(LogUtil.fix_string_escapes("\n"), "\n")
self.assertEqual(LogUtil.fix_string_escapes("\t"), "\t")
self.assertEqual(LogUtil.fix_string_escapes("\r\n"), "\r\n")
self.assertEqual(LogUtil.fix_string_escapes("Process 50% complete"), "Process 50%% complete")
self.assertEqual(LogUtil.fix_string_escapes("%% in config"), "%%%% in config")
self.assertEqual(LogUtil.fix_string_escapes("%s %d"), "%%s %%d")
self.assertEqual(LogUtil.fix_string_escapes(r"%A%%"), "%%A%%%%")
self.assertEqual(LogUtil.fix_string_escapes("Path\\to\\file"), "Path\\to\\file")
self.assertEqual(LogUtil.fix_string_escapes(r"C:\\Users\test"), "C:\\\\Users\\test")
self.assertEqual(LogUtil.fix_string_escapes(r"\n"), "\\n")
self.assertEqual(LogUtil.fix_string_escapes("\x07\a\x00\0\x0c\f\x05"), "\\a\\a\\0\\0\\f\\f\\x05")
self.assertEqual(LogUtil.fix_string_escapes(r"\x07\a\x00\0\x0c\f\x05"), "\\x07\\a\\x00\\0\\x0c\\f\\x05")
self.assertEqual(LogUtil.fix_string_escapes("\\x00\\0\\x0c\\f\\x05\\r\\n"), "\\x00\\0\\x0c\\f\\x05\\r\\n")
self.assertEqual(LogUtil.fix_string_escapes("%\n%s\\n\"100%完成\n结果:\""), "%%\n%%s\\n\"100%%完成\n结果:\"")