Skip to content

Commit

Permalink
Make it possible for a package to require a compatible version of Par…
Browse files Browse the repository at this point in the history
…tCAD (#1)
  • Loading branch information
openvmp authored Dec 29, 2023
1 parent baecba2 commit 7f98599
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ cairosvg==2.7.0
ocp_vscode==2.0.13
GitPython==3.1.40
progress==1.6
ruamel.yaml==0.18.5
ruamel.yaml==0.18.5
packaging==23.1
15 changes: 14 additions & 1 deletion src/partcad/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os
import json
import yaml
from importlib.metadata import version
from packaging.specifiers import SpecifierSet

DEFAULT_CONFIG_FILENAME = "partcad.yaml"

Expand All @@ -22,7 +24,7 @@ def __init__(self, config_path=DEFAULT_CONFIG_FILENAME):
if os.path.isdir(config_path):
config_path += "/" + DEFAULT_CONFIG_FILENAME
if not os.path.isfile(config_path):
print("PartCad configuration file is not found: %s" % config_path)
print("PartCAD configuration file is not found: %s" % config_path)
return
self.config_path = config_path

Expand All @@ -32,3 +34,14 @@ def __init__(self, config_path=DEFAULT_CONFIG_FILENAME):
self.config_obj = yaml.safe_load(open(config_path, "r"))
if config_path.endswith(".json"):
self.config_obj = json.load(open(config_path, "r"))

if "partcad" in self.config_obj:
# See what version of PartCAD is required for this package
partcad_requirements = SpecifierSet(self.config_obj["partcad"])
partcad_version = version("partcad")
if partcad_version not in partcad_requirements:
# TODO(clairbee): add better error and exception handling
raise Exception(
"ERROR: Incompatible PartCAD version! %s does not satisfy %s"
% (partcad_version, partcad_requirements)
)
2 changes: 2 additions & 0 deletions tests/unit/data/project_config_invalid_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This configuration file has the version requirements that always fail
partcad: "<0.1.0"
2 changes: 2 additions & 0 deletions tests/unit/data/project_config_valid_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This configuration file has the version requirements that always pass
partcad: ">=0.1.0"
29 changes: 29 additions & 0 deletions tests/unit/test_project_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
#
# OpenVMP, 2023
#
# Author: Roman Kuzmenko
# Created: 2023-12-27
#
# Licensed under Apache License, Version 2.0.
#

import partcad as pc


def test_project_config_version_1():
"""Positive test case for PartCAD version requirement in the package config file"""
try:
ctx = pc.Context("tests/unit/data/project_config_valid_1.yaml")
assert ctx.config_obj["partcad"] == ">=0.1.0"
except Exception as e:
assert False, "Valid configuration file caused an exception: %s" % e


def test_project_config_version_2():
"""Negative test case for PartCAD version requirement in the package config file"""
try:
ctx = pc.Context("tests/unit/data/project_config_invalid_1.yaml")
assert False, "Invalid configuration file did not cause an exception"
except:
_ignore = True

0 comments on commit 7f98599

Please sign in to comment.