aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs/pytest_tests/pytest_plugin_test.py
blob: 51f95cbd9bf8be82a0afbc12c889d3c035118d6a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Tests that the pytest plugin properly provides the "fs" fixture"""
import os
import tempfile

from pyfakefs.fake_filesystem import OSType
from pyfakefs.fake_filesystem_unittest import Pause
import pyfakefs.pytest_tests.io


def test_fs_fixture(fs):
    fs.create_file("/var/data/xx1.txt")
    assert os.path.exists("/var/data/xx1.txt")


def test_fs_fixture_alias(fake_filesystem):
    fake_filesystem.create_file("/var/data/xx1.txt")
    assert os.path.exists("/var/data/xx1.txt")


def test_both_fixtures(fs, fake_filesystem):
    fake_filesystem.create_file("/var/data/xx1.txt")
    fs.create_file("/var/data/xx2.txt")
    assert os.path.exists("/var/data/xx1.txt")
    assert os.path.exists("/var/data/xx2.txt")
    assert fs == fake_filesystem


def test_pause_resume(fs):
    fake_temp_file = tempfile.NamedTemporaryFile()
    assert fs.exists(fake_temp_file.name)
    assert os.path.exists(fake_temp_file.name)
    fs.pause()
    assert fs.exists(fake_temp_file.name)
    assert not os.path.exists(fake_temp_file.name)
    real_temp_file = tempfile.NamedTemporaryFile()
    assert not fs.exists(real_temp_file.name)
    assert os.path.exists(real_temp_file.name)
    fs.resume()
    assert not os.path.exists(real_temp_file.name)
    assert os.path.exists(fake_temp_file.name)


def test_pause_resume_contextmanager(fs):
    fake_temp_file = tempfile.NamedTemporaryFile()
    assert fs.exists(fake_temp_file.name)
    assert os.path.exists(fake_temp_file.name)
    with Pause(fs):
        assert fs.exists(fake_temp_file.name)
        assert not os.path.exists(fake_temp_file.name)
        real_temp_file = tempfile.NamedTemporaryFile()
        assert not fs.exists(real_temp_file.name)
        assert os.path.exists(real_temp_file.name)
    assert not os.path.exists(real_temp_file.name)
    assert os.path.exists(fake_temp_file.name)


def test_use_own_io_module(fs):
    filepath = "foo.txt"
    with open(filepath, "w") as f:
        f.write("bar")

    stream = pyfakefs.pytest_tests.io.InputStream(filepath)
    assert stream.read() == "bar"


def test_switch_to_windows(fs):
    fs.os = OSType.WINDOWS
    assert os.path.exists(tempfile.gettempdir())


def test_switch_to_linux(fs):
    fs.os = OSType.LINUX
    assert os.path.exists(tempfile.gettempdir())


def test_switch_to_macos(fs):
    fs.os = OSType.MACOS
    assert os.path.exists(tempfile.gettempdir())