• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests that the pytest plugin properly provides the "fs" fixture"""
2import os
3import tempfile
4
5from pyfakefs.fake_filesystem import OSType
6from pyfakefs.fake_filesystem_unittest import Pause
7import pyfakefs.pytest_tests.io
8
9
10def test_fs_fixture(fs):
11    fs.create_file("/var/data/xx1.txt")
12    assert os.path.exists("/var/data/xx1.txt")
13
14
15def test_fs_fixture_alias(fake_filesystem):
16    fake_filesystem.create_file("/var/data/xx1.txt")
17    assert os.path.exists("/var/data/xx1.txt")
18
19
20def test_both_fixtures(fs, fake_filesystem):
21    fake_filesystem.create_file("/var/data/xx1.txt")
22    fs.create_file("/var/data/xx2.txt")
23    assert os.path.exists("/var/data/xx1.txt")
24    assert os.path.exists("/var/data/xx2.txt")
25    assert fs == fake_filesystem
26
27
28def test_pause_resume(fs):
29    fake_temp_file = tempfile.NamedTemporaryFile()
30    assert fs.exists(fake_temp_file.name)
31    assert os.path.exists(fake_temp_file.name)
32    fs.pause()
33    assert fs.exists(fake_temp_file.name)
34    assert not os.path.exists(fake_temp_file.name)
35    real_temp_file = tempfile.NamedTemporaryFile()
36    assert not fs.exists(real_temp_file.name)
37    assert os.path.exists(real_temp_file.name)
38    fs.resume()
39    assert not os.path.exists(real_temp_file.name)
40    assert os.path.exists(fake_temp_file.name)
41
42
43def test_pause_resume_contextmanager(fs):
44    fake_temp_file = tempfile.NamedTemporaryFile()
45    assert fs.exists(fake_temp_file.name)
46    assert os.path.exists(fake_temp_file.name)
47    with Pause(fs):
48        assert fs.exists(fake_temp_file.name)
49        assert not os.path.exists(fake_temp_file.name)
50        real_temp_file = tempfile.NamedTemporaryFile()
51        assert not fs.exists(real_temp_file.name)
52        assert os.path.exists(real_temp_file.name)
53    assert not os.path.exists(real_temp_file.name)
54    assert os.path.exists(fake_temp_file.name)
55
56
57def test_use_own_io_module(fs):
58    filepath = "foo.txt"
59    with open(filepath, "w") as f:
60        f.write("bar")
61
62    stream = pyfakefs.pytest_tests.io.InputStream(filepath)
63    assert stream.read() == "bar"
64
65
66def test_switch_to_windows(fs):
67    fs.os = OSType.WINDOWS
68    assert os.path.exists(tempfile.gettempdir())
69
70
71def test_switch_to_linux(fs):
72    fs.os = OSType.LINUX
73    assert os.path.exists(tempfile.gettempdir())
74
75
76def test_switch_to_macos(fs):
77    fs.os = OSType.MACOS
78    assert os.path.exists(tempfile.gettempdir())
79