summaryrefslogtreecommitdiff
path: root/tests/session/cmd/run/test_common.py
blob: 3a0866cc2f1f684f2ec53963649848f3463fd760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations

import os
import re
from argparse import ArgumentError, ArgumentParser, Namespace
from pathlib import Path

import pytest

from tox.pytest import ToxProjectCreator
from tox.session.cmd.run.common import InstallPackageAction, SkipMissingInterpreterAction


@pytest.mark.parametrize("values", ["config", None, "true", "false"])
def test_skip_missing_interpreter_action_ok(values: str | None) -> None:
    args_namespace = Namespace()
    SkipMissingInterpreterAction(option_strings=["-i"], dest="into")(ArgumentParser(), args_namespace, values)
    expected = "true" if values is None else values
    assert args_namespace.into == expected


def test_skip_missing_interpreter_action_nok() -> None:
    argument_parser = ArgumentParser()
    with pytest.raises(ArgumentError, match=r"value must be 'config', 'true', or 'false' \(got 'bad value'\)"):
        SkipMissingInterpreterAction(option_strings=["-i"], dest="into")(argument_parser, Namespace(), "bad value")


def test_install_pkg_ok(tmp_path: Path) -> None:
    argument_parser = ArgumentParser()
    path = tmp_path / "a"
    path.write_text("")
    namespace = Namespace()
    InstallPackageAction(option_strings=["--install-pkg"], dest="into")(argument_parser, namespace, str(path))
    assert namespace.into == path


def test_install_pkg_does_not_exist(tmp_path: Path) -> None:
    argument_parser = ArgumentParser()
    path = str(tmp_path / "a")
    with pytest.raises(ArgumentError, match=re.escape(f"argument --install-pkg: {path} does not exist")):
        InstallPackageAction(option_strings=["--install-pkg"], dest="into")(argument_parser, Namespace(), path)


def test_install_pkg_not_file(tmp_path: Path) -> None:
    argument_parser = ArgumentParser()
    path = str(tmp_path)
    with pytest.raises(ArgumentError, match=re.escape(f"argument --install-pkg: {path} is not a file")):
        InstallPackageAction(option_strings=["--install-pkg"], dest="into")(argument_parser, Namespace(), path)


def test_install_pkg_empty() -> None:
    argument_parser = ArgumentParser()
    with pytest.raises(ArgumentError, match=re.escape("argument --install-pkg: cannot be empty")):
        InstallPackageAction(option_strings=["--install-pkg"], dest="into")(argument_parser, Namespace(), "")


def test_empty_report(tox_project: ToxProjectCreator) -> None:
    proj = tox_project({"tox.ini": ""})
    outcome = proj.run("exec", "-qq", "--", "python", "-c", "print('foo')")

    outcome.assert_success()
    outcome.assert_out_err(f"foo{os.linesep}", "")