File: test_xwopen.py

package info (click to toggle)
python-ase 3.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (33 lines) | stat: -rw-r--r-- 658 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
# fmt: off
import pytest

from ase.utils import xwopen

pytestmark = pytest.mark.usefixtures('testdir')


poem = 'Wer reitet so spät durch Nacht und Wind\n'.encode()
filename = 'poem.txt'


def test_xwopen():
    with xwopen(filename) as fd:
        fd.write(poem)

    assert fd.closed

    with open(filename, 'rb') as fd:
        assert fd.read() == poem


def test_xwopen_locked():
    with xwopen(filename) as fd:
        assert fd is not None
        with xwopen(filename) as fd2:
            assert fd2 is None


def test_xwopen_fail(tmp_path):
    with pytest.raises(OSError):
        with xwopen(tmp_path / 'does_not_exist/file'):
            pass