25th November 2022
It also helps to run
pdm updatewith the
-vflag, that way you see which are the candidates that are rejected, and you can put the constrain you want. For example, I was seeing the next traceback:
pdm.termui: Conflicts detected: pyflakes>=3.0.0 (from <Candidate autoflake 2.0.0 from https://pypi.org/simple/autoflake/>) pyflakes<2.5.0,>=2.4.0 (from <Candidate flake8 4.0.1 from unknown>)
So I added a new dependency to pin it:
[tool.pdm.dev-dependencies] dependencies = [ # Until flakeheaven supports flake8 5.x # https://github.com/flakeheaven/flakeheaven/issues/132 "flake8>=4.0.1,<5.0.0", "pyflakes<2.5.0", ]
If none of the above works, you can override them:
[tool.pdm.overrides] "importlib-metadata" = ">=3.10"
For basic command line tools with file system operations, the
CliRunner.isolated_filesystem()method is useful for setting the current working directory to a new, empty folder.
from click.testing import CliRunner from cat import cat def test_cat(): runner = CliRunner() with runner.isolated_filesystem(): with open("hello.txt", "w") as f: f.write("Hello World!") result = runner.invoke(cat, ["hello.txt"]) assert result.exit_code == 0 assert result.output == "Hello World!\n"
temp_dirto control where the temporary directory is created. The directory will not be removed by Click in this case. This is useful to integrate with a framework like Pytest that manages temporary files.
def test_keep_dir(tmp_path): runner = CliRunner() with runner.isolated_filesystem(temp_dir=tmp_path) as td: ...
From the docs:
true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX
false(the default), a missing parent raises
FileExistsErroris raised if the target directory already exists.
FileExistsErrorexceptions will be ignored (same behavior as the POSIX
mkdir -pcommand), but only if the last path component is not an existing non-directory file.
Create a file at this given path.
If the file already exists, the function succeeds if
true(and its modification time is updated to the current time), otherwise
If the parent directory doesn't exist you need to create it first.
global_conf_path = xdg_home / "autoimport" / "config.toml" global_conf_path.parent.mkdir(parents=True) global_conf_path.touch(exist_ok=True)