未验证 提交 aa1db063 编写于 作者: F Frost Ming

Give warning message when the project automatically fallbacks

上级 f0198a09
......@@ -9,6 +9,8 @@ Release v1.5.0b1 (2021-04-12)
- Remove the incompatible code from the files that will be run in-process. [#375](https://github.com/pdm-project/pdm/issues/375)
- Get the correct Python ABI tag of selected interpreter [#378](https://github.com/pdm-project/pdm/issues/378)
- Error out when doing `pdm run` on a directory not initialized yet.
- Give warning message when the project automatically fallbacks to the global project.
### Dependencies
......@@ -20,6 +22,7 @@ Release v1.5.0b1 (2021-04-12)
- Add a basic integration test for cross-python check. [#377](https://github.com/pdm-project/pdm/issues/377)
- Refactor the `project.python_executable` to `project.python` that contains all info of the interpreter. [#382](https://github.com/pdm-project/pdm/issues/382)
- Continue refactoring Python info to extract to its own module. [#383](https://github.com/pdm-project/pdm/issues/383)
- Refactor the creation of project.
Release v1.5.0b0 (2021-04-03)
......
......@@ -83,25 +83,25 @@ class Core:
options.project = obj
if getattr(options, "project", None) is None:
global_project = getattr(options, "global_project", None)
if global_project:
project_factory = self.project_class.create_global
else:
project_factory = self.project_class
default_root = (
None
if global_project or getattr(options, "search_parent", True)
else "."
)
project = project_factory(
getattr(options, "project_path", None) or default_root
project = self.create_project(
getattr(options, "project_path", None) or default_root,
is_global=global_project,
)
options.project = project
# Add reverse reference for core object
options.project.core = self
migrate_pyproject(options.project)
def create_project(
self, root_path: Optional[os.PathLike] = None, is_global: bool = False
):
return self.project_class(self, root_path, is_global)
def main(
self,
args: List[str] = None,
......
......@@ -54,32 +54,29 @@ class Project:
PYPROJECT_VERSION = "2"
GLOBAL_PROJECT = Path.home() / ".pdm" / "global-project"
core: Core
@classmethod
def create_global(cls, root_path: Optional[str] = None) -> "Project":
if root_path is None:
root_path = cls.GLOBAL_PROJECT.as_posix()
project = cls(root_path)
project.is_global = True
project.init_global_project()
return project
def __init__(self, root_path: Optional[str] = None) -> None:
self.is_global = False
def __init__(
self, core: Core, root_path: Optional[os.PathLike], is_global: bool = False
) -> None:
self._pyproject: Optional[Dict] = None
self._lockfile: Optional[Dict] = None
self._environment: Optional[Environment] = None
self._python: Optional[PythonInfo] = None
self.core = core
if root_path is None:
root_path = find_project_root()
if root_path is None and self.global_config["auto_global"]:
self.root = self.GLOBAL_PROJECT
self.is_global = True
self.init_global_project()
else:
self.root = Path(root_path or "").absolute()
root_path = find_project_root() if not is_global else self.GLOBAL_PROJECT
if not is_global and root_path is None and self.global_config["auto_global"]:
self.core.ui.echo(
"Project is not found, fallback to the global project",
fg="yellow",
err=True,
)
root_path = self.GLOBAL_PROJECT
is_global = True
self.root = Path(root_path or "").absolute()
self.is_global = is_global
self.init_global_project()
def __repr__(self) -> str:
return f"<Project '{self.root.as_posix()}'>"
......
......@@ -7,7 +7,6 @@ from pdm.cli import actions
from pdm.exceptions import PdmException, PdmUsageError
from pdm.models.requirements import parse_requirement
from pdm.models.specifiers import PySpecSet
from pdm.project import Project
from tests.conftest import Distribution
Requirement = namedtuple("Requirement", "key")
......@@ -489,16 +488,6 @@ def test_add_dependency_from_multiple_parents(project, working_set, mocker):
assert "chardet" in working_set
def test_list_packages(capsys, core):
project = Project()
project.core = core
actions.do_list(project)
out, _ = capsys.readouterr()
assert "pdm" in out
assert "tomlkit" in out
assert "pip" in out
@pytest.mark.usefixtures("repository")
def test_lock_dependencies(project):
project.add_dependencies({"requests": parse_requirement("requests")})
......@@ -510,8 +499,7 @@ def test_lock_dependencies(project):
def test_build_distributions(tmp_path, core):
project = Project()
project.core = core
project = core.create_project()
actions.do_build(project, dest=tmp_path.as_posix())
wheel = Wheel(next(tmp_path.glob("*.whl")).as_posix())
assert wheel.name == "pdm"
......
......@@ -7,7 +7,7 @@ import pytest
from pdm.cli import actions
from pdm.models.requirements import parse_requirement
from pdm.utils import temp_environ
from pdm.utils import cd, temp_environ
from tests import FIXTURES
......@@ -86,8 +86,9 @@ def test_info_command(project, invoke):
assert result.exit_code == 0
def test_info_global_project(invoke):
result = invoke(["info", "-g", "--where"])
def test_info_global_project(invoke, tmp_path):
with cd(tmp_path):
result = invoke(["info", "-g", "--where"])
assert "global-project" in result.output.strip()
......
......@@ -135,13 +135,17 @@ class TestRepository(BaseRepository):
class TestProject(Project):
def __init__(self, root_path):
self.GLOBAL_PROJECT = Path(root_path) / ".pdm-home" / "global-project"
super().__init__(root_path)
def __init__(self, core, root_path, is_global):
self.root_path = Path(root_path or ".")
self.GLOBAL_PROJECT = self.root_path / ".pdm-home" / "global-project"
super().__init__(core, root_path, is_global)
@cached_property
def global_config(self):
return Config(self.root / ".pdm-home" / "config.toml", is_global=True)
return Config(self.root_path / ".pdm-home" / "config.toml", is_global=True)
main.project_class = TestProject
class Distribution:
......@@ -222,8 +226,7 @@ def pip_global_tempdir_manager():
@pytest.fixture()
def project_no_init(tmp_path, mocker):
p = TestProject(tmp_path.as_posix())
p.core = main
p = main.create_project(tmp_path)
mocker.patch("pdm.utils.get_finder", get_local_finder)
mocker.patch("pdm.models.environment.get_finder", get_local_finder)
mocker.patch("pdm.project.core.Config.HOME_CONFIG", tmp_path)
......
......@@ -3,7 +3,6 @@ import pytest
from pdm.exceptions import ExtrasError
from pdm.models.candidates import Candidate
from pdm.models.requirements import parse_requirement
from pdm.project.core import Project
from tests import FIXTURES
......@@ -119,8 +118,7 @@ def test_parse_abnormal_specifiers(project):
],
)
def test_expand_project_root_in_url(req_str, core):
project = Project(FIXTURES.parent.parent)
project.core = core
project = core.create_project(FIXTURES.parent.parent)
if req_str.startswith("-e "):
req = parse_requirement(req_str[3:], True)
else:
......
......@@ -8,7 +8,6 @@ import pytest
from pdm.models.requirements import filter_requirements_with_extras
from pdm.pep517.api import build_wheel
from pdm.project import Project
from pdm.utils import cd, temp_environ
......@@ -59,12 +58,18 @@ def test_project_sources_overriding(project):
def test_global_project(tmp_path, core):
project = Project.create_global(tmp_path.as_posix())
project.core = core
project.init_global_project()
project = core.create_project(tmp_path, True)
assert project.environment.is_global
def test_auto_global_project(tmp_path, core):
tmp_path.joinpath(".pdm-home").mkdir()
(tmp_path / ".pdm-home/config.toml").write_text("auto_global = true\n")
with cd(tmp_path):
project = core.create_project()
assert project.is_global
def test_project_use_venv(project):
del project.project_config["python.path"]
project._python = None
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册