ref: Use pathlib in tests (#4159)

Use pathlib in tests
This commit is contained in:
Christophe Bornet 2024-10-15 19:51:50 +02:00 committed by GitHub
commit eb53f66641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 59 additions and 87 deletions

View file

@ -1,17 +1,16 @@
import os
import sys
import re
from pathlib import Path
import packaging.version
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
BASE_DIR = Path(__file__).parent.parent.parent
def update_base_dep(pyproject_path: str, new_version: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
filepath = os.path.join(BASE_DIR, pyproject_path)
with open(filepath, "r") as file:
content = file.read()
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
replacement = f'langflow-base-nightly = "{new_version}"'
@ -20,8 +19,7 @@ def update_base_dep(pyproject_path: str, new_version: str) -> None:
if not pattern.search(content):
raise Exception(f'langflow-base poetry dependency not found in "{filepath}"')
content = pattern.sub(replacement, content)
with open(filepath, "w") as file:
file.write(content)
filepath.write_text(content)
def verify_pep440(version):

View file

@ -1,15 +1,14 @@
import os
import sys
import re
from pathlib import Path
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
BASE_DIR = Path(__file__).parent.parent.parent
def update_pyproject_name(pyproject_path: str, new_project_name: str) -> None:
"""Update the project name in pyproject.toml."""
filepath = os.path.join(BASE_DIR, pyproject_path)
with open(filepath, "r") as file:
content = file.read()
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
# Regex to match the version line under [tool.poetry]
pattern = re.compile(r'(?<=^name = ")[^"]+(?=")', re.MULTILINE)
@ -18,15 +17,13 @@ def update_pyproject_name(pyproject_path: str, new_project_name: str) -> None:
raise Exception(f'Project name not found in "{filepath}"')
content = pattern.sub(new_project_name, content)
with open(filepath, "w") as file:
file.write(content)
filepath.write_text(content)
def update_uv_dep(pyproject_path: str, new_project_name: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
filepath = os.path.join(BASE_DIR, pyproject_path)
with open(filepath, "r") as file:
content = file.read()
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
if new_project_name == "langflow-nightly":
pattern = re.compile(r"langflow = \{ workspace = true \}")
@ -41,8 +38,7 @@ def update_uv_dep(pyproject_path: str, new_project_name: str) -> None:
if not pattern.search(content):
raise Exception(f"{replacement} uv dependency not found in {filepath}")
content = pattern.sub(replacement, content)
with open(filepath, "w") as file:
file.write(content)
filepath.write_text(content)
def main() -> None:

View file

@ -1,17 +1,16 @@
import os
import sys
import re
from pathlib import Path
import packaging.version
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
BASE_DIR = Path(__file__).parent.parent.parent
def update_pyproject_version(pyproject_path: str, new_version: str) -> None:
"""Update the version in pyproject.toml."""
filepath = os.path.join(BASE_DIR, pyproject_path)
with open(filepath, "r") as file:
content = file.read()
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
# Regex to match the version line under [tool.poetry]
pattern = re.compile(r'(?<=^version = ")[^"]+(?=")', re.MULTILINE)
@ -21,8 +20,7 @@ def update_pyproject_version(pyproject_path: str, new_version: str) -> None:
content = pattern.sub(new_version, content)
with open(filepath, "w") as file:
file.write(content)
filepath.write_text(content)
def verify_pep440(version):

View file

@ -1,18 +1,17 @@
import os
import sys
import re
from pathlib import Path
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
BASE_DIR = Path(__file__).parent.parent.parent
def update_uv_dep(base_version: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
pyproject_path = os.path.join(BASE_DIR, "pyproject.toml")
pyproject_path = BASE_DIR / "pyproject.toml"
# Read the pyproject.toml file content
with open(pyproject_path, "r") as file:
content = file.read()
content = pyproject_path.read_text()
# For the main project, update the langflow-base dependency in the UV section
pattern = re.compile(r'(dependencies\s*=\s*\[\s*\n\s*)("langflow-base==[\d.]+")')
@ -26,8 +25,7 @@ def update_uv_dep(base_version: str) -> None:
content = pattern.sub(replacement, content)
# Write the updated content back to the file
with open(pyproject_path, "w") as file:
file.write(content)
pyproject_path.write_text(content)
def main() -> None: