File: test_smart_open.py

package info (click to toggle)
python-autocommand 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: python: 1,052; sh: 15; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download | duplicates (2)
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
from autocommand.autoparse import smart_open


def test_smart_open_is_exported():
    import autocommand

    assert autocommand.smart_open is smart_open


def test_smart_open_path_read(tmpdir):
    target = tmpdir.join('file.txt')
    target.write("Hello")

    with smart_open(str(target)) as file:
        assert not file.closed
        assert file.read() == "Hello"

    assert file.closed


def test_smart_open_path_write(tmpdir):
    target = tmpdir.join('file.txt').ensure(file=True)

    with smart_open(str(target), 'w') as file:
        assert not file.closed
        file.write("Test Content")  # Tests that the file is writable

    assert file.closed
    assert target.read() == "Test Content"


def test_smart_open_path_create(tmpdir):
    target = tmpdir.join("file.txt")

    with smart_open(str(target), 'w') as file:
        file.write("Test Content")

    assert target.read() == "Test Content"


def test_smart_open_file(tmpdir):
    path = str(tmpdir.join('file.txt').ensure(file=True))

    with open(path) as file:
        with smart_open(file) as smart_file:
            assert file is smart_file
            assert not smart_file.closed
        assert not smart_file.closed