File: test_xwopen.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (32 lines) | stat: -rw-r--r-- 654 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
import pytest
from ase.utils import xwopen


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


poem = 'Wer reitet so spät durch Nacht und Wind\n'.encode('utf-8')
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