CICD流程语法树分析代码差异执行相关测试用例

首先通过git diff分析代码差异,然后对代码建立语法树,然后执行相关测试用例

shell

git fetch origin
rev=$(git ls-remote origin develop | awk '{ print $1 }')
files=$(git diff  --name-only develop origin/develop)
for file in $files; do
    changes=$(git diff --unified=0 develop origin/develop "$file" | grep '^@@' | grep -Eo '\+[0-9]+(,[0-9]+)?' | tr '\n' '&' | sed 's/ $//')
    rev="$rev~$file:$changes"
# done
done
echo "rev: $rev"

上述代码可以找到变更代码的函数名和文件名

python代码可以用ast进行语法树分析

res_set = set()
update_function_dict= {}
def get_logger(path):
    logger = logging.getLogger('mylogger')
    logger.setLevel(logging.INFO)
    fh=logging.FileHandler(path)
    fh.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    # ch = logging.StreamHandler()
    # ch.setLevel(logging.INFO)
    # ch.setFormatter(formatter)
    # logger.addHandler(ch)
    return logger
def print_all_functions(source_code, line_ranges, file_name):
    # tree = ast.parse(source_code)

    # for node in ast.walk(tree):
    #     print(node)
    #     if isinstance(node, ast.FunctionDef):
    #         print(f"Function '{node.name}':")
    #         print(f"  Start line: {node.lineno}")
    #         print(f"  End line: {node.end_lineno}")
    #         print()
    # line_ranges = [(1,1000)]
    
    class FunctionVisitor(ast.NodeVisitor):
        def visit_FunctionDef(self, node):
            for start_line, end_line in line_ranges:
                if node.end_lineno < start_line or end_line < node.lineno :
                    continue
                # res_set.add(f"{file_name}_{node.name}")
                update_function_dict[f"{file_name}:{node.name}"] = True
                res_set.add(node.name)
                # print(f"Function {node.name} at line {node.lineno}:")
                # print(ast.get_source_segment(source_code, node))

    tree = ast.parse(source_code)
    visitor = FunctionVisitor()
    visitor.visit(tree)

def process_changes(file_name, changes):
    print(f"Processing changes for file: {file_name}")
    print("Changes:")
    line_ranges = []
    for change in changes:
        if change == '':
            continue
        changes = change.split(',')
        if len(changes) == 1:
            line_ranges.append( (int(changes[0]),int(changes[0])) )
        if len(changes) == 2:
            ed = int(changes[1])
            if ed == 0 : ed =1
            line_ranges.append( (int(changes[0]),int(changes[0]) + ed - 1) )
    print(line_ranges)
    file_name = os.path.join('/home',file_name)
    with open(file_name, 'r') as file:
        source_code = file.read()
    name = file_name.split('/')[-1].split('.')[0]
    print_all_functions(source_code, line_ranges, name)

如果测试用例都通过可以更新代码

    git merge --abort
    git pull --no-commit

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值