1. 程式人生 > 其它 >httprunner的env裡,有空格和註釋報錯問題解決

httprunner的env裡,有空格和註釋報錯問題解決

.env

testenv=xxxxx

1.當檔案內包含註釋或空行時,丟擲異常 httprunner.exceptions.FileFormatError: .env format error
原因是原始碼中沒有對空行和 # 號做處理,程式碼片段 (loader - 130):

with open(dot_env_path, mode="rb") as fp:
    for line in fp:
        # maxsplit=1
        if b"=" in line:
            variable, value = line.split(b"=", 1)
        elif b":" in line:
            variable, value = line.split(b":", 1)
        else:
            raise exceptions.FileFormatError(".env format error")

2.加上判斷忽略掉註釋和空行,就不會報錯了

with open(dot_env_path, mode="rb") as fp:
    for line in fp:
        # maxsplit=1
        line = line.strip()
        if not len(line) or line.startswith(b"#"):
            continue
        if b"=" in line:
            variable, value = line.split(b"=", 1)
        elif b":" in line:
            variable, value = line.split(b":", 1)
        else:
            raise exceptions.FileFormatError(".env format error")