File: test_fixture.py

package info (click to toggle)
python-pytest-shell-utilities 1.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: python: 2,998; makefile: 18
file content (50 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download
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
# Copyright 2022-2024 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
#
import pathlib
import sys
import tempfile

import pytest

from pytestshellutils.shell import Subprocess
from tests.conftest import Tempfiles


def test_run_call(tempfiles: Tempfiles, shell: Subprocess) -> None:
    script = tempfiles.makepyfile(
        """
        # coding=utf-8
        import os
        import json
        print(json.dumps(dict(**os.environ)), flush=True)
        exit(0)
        """
    )
    result = shell.run(sys.executable, script)
    assert result.returncode == 0


def test_run_cwd(tempfiles: Tempfiles, shell: Subprocess) -> None:
    system_tempdir = str(pathlib.Path(tempfile.gettempdir()).resolve())
    assert str(shell.cwd.resolve()) != system_tempdir
    script = tempfiles.makepyfile(
        """
        # coding=utf-8
        import pathlib
        print(str(pathlib.Path.cwd().resolve()), flush=True)
        exit(0)
        """
    )
    result = shell.run(sys.executable, script, cwd=system_tempdir)
    assert result.returncode == 0
    assert result.stdout.strip() == system_tempdir


def test_run_shell(shell: Subprocess) -> None:
    with pytest.raises(FileNotFoundError):
        shell.run("exit", "0")
    with pytest.raises(FileNotFoundError):
        shell.run("exit", "0", shell=False)
    result = shell.run("exit", "0", shell=True)
    assert result.returncode == 0