-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: replace tmpdir -> tmp_path (#541)
`tmpdir` uses the legacy `py.path.local` type, recent pytests prefer using `tmp_path` which is a `pathlib.Path`.
- Loading branch information
Showing
2 changed files
with
79 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,108 @@ | ||
import subprocess | ||
import sys | ||
import os | ||
from pathlib import Path | ||
|
||
import py | ||
import pytest | ||
|
||
from trustme._cli import main | ||
|
||
|
||
def test_trustme_cli(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
main(argv=[]) | ||
def test_trustme_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
main(argv=[]) | ||
|
||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
def test_trustme_cli_e2e(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
rv = subprocess.call([sys.executable, "-m", "trustme"]) | ||
assert rv == 0 | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
def test_trustme_cli_e2e(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
rv = subprocess.call([sys.executable, "-m", "trustme"]) | ||
assert rv == 0 | ||
|
||
def test_trustme_cli_directory(tmpdir: py.path.local) -> None: | ||
subdir = tmpdir.mkdir("sub") | ||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
|
||
def test_trustme_cli_directory(tmp_path: Path) -> None: | ||
subdir = tmp_path.joinpath("sub") | ||
subdir.mkdir() | ||
main(argv=["-d", str(subdir)]) | ||
|
||
assert subdir.join("server.key").check(exists=1) | ||
assert subdir.join("server.pem").check(exists=1) | ||
assert subdir.join("client.pem").check(exists=1) | ||
assert subdir.joinpath("server.key").exists() | ||
assert subdir.joinpath("server.pem").exists() | ||
assert subdir.joinpath("client.pem").exists() | ||
|
||
|
||
def test_trustme_cli_directory_does_not_exist(tmpdir: py.path.local) -> None: | ||
notdir = tmpdir.join("notdir") | ||
def test_trustme_cli_directory_does_not_exist(tmp_path: Path) -> None: | ||
notdir = tmp_path.joinpath("notdir") | ||
with pytest.raises(ValueError, match="is not a directory"): | ||
main(argv=["-d", str(notdir)]) | ||
|
||
|
||
def test_trustme_cli_identities(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
main(argv=["-i", "example.org", "www.example.org"]) | ||
def test_trustme_cli_identities(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
main(argv=["-i", "example.org", "www.example.org"]) | ||
|
||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
def test_trustme_cli_identities_empty(tmpdir: py.path.local) -> None: | ||
|
||
def test_trustme_cli_identities_empty(tmp_path: Path) -> None: | ||
with pytest.raises(ValueError, match="at least one identity"): | ||
main(argv=["-i"]) | ||
|
||
|
||
def test_trustme_cli_common_name(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
main(argv=["--common-name", "localhost"]) | ||
def test_trustme_cli_common_name(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
main(argv=["--common-name", "localhost"]) | ||
|
||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
|
||
def test_trustme_cli_expires_on(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
main(argv=["--expires-on", "2035-03-01"]) | ||
|
||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
def test_trustme_cli_expires_on(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
main(argv=["--expires-on", "2035-03-01"]) | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
def test_trustme_cli_invalid_expires_on(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
with pytest.raises(ValueError, match="does not match format"): | ||
main(argv=["--expires-on", "foobar"]) | ||
|
||
def test_trustme_cli_invalid_expires_on(tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
with pytest.raises(ValueError, match="does not match format"): | ||
main(argv=["--expires-on", "foobar"]) | ||
assert not tmp_path.joinpath("server.key").exists() | ||
assert not tmp_path.joinpath("server.pem").exists() | ||
assert not tmp_path.joinpath("client.pem").exists() | ||
|
||
assert tmpdir.join("server.key").check(exists=0) | ||
assert tmpdir.join("server.pem").check(exists=0) | ||
assert tmpdir.join("client.pem").check(exists=0) | ||
|
||
def test_trustme_cli_quiet( | ||
capsys: pytest.CaptureFixture[str], | ||
tmp_path: Path, | ||
monkeypatch: pytest.MonkeyPatch, | ||
) -> None: | ||
monkeypatch.chdir(tmp_path) | ||
|
||
def test_trustme_cli_quiet(capsys: pytest.CaptureFixture[str], tmpdir: py.path.local) -> None: | ||
with tmpdir.as_cwd(): | ||
main(argv=["-q"]) | ||
main(argv=["-q"]) | ||
|
||
assert tmpdir.join("server.key").check(exists=1) | ||
assert tmpdir.join("server.pem").check(exists=1) | ||
assert tmpdir.join("client.pem").check(exists=1) | ||
assert tmp_path.joinpath("server.key").exists() | ||
assert tmp_path.joinpath("server.pem").exists() | ||
assert tmp_path.joinpath("client.pem").exists() | ||
|
||
captured = capsys.readouterr() | ||
assert not captured.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters