File: test_resource.py

package info (click to toggle)
plover 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,356 kB
  • sloc: python: 21,589; sh: 682; ansic: 25; makefile: 11
file content (101 lines) | stat: -rw-r--r-- 3,586 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from pathlib import Path
import inspect

import pytest

from plover.misc import normalize_path
from plover.resource import (
    resource_exists,
    resource_filename,
    resource_timestamp,
    resource_update,
)


@pytest.mark.parametrize(
    "resource, exists, filename",
    (
        # Relative filename.
        (Path(__file__).relative_to(Path.cwd()), True, None),
        # Relative directory.
        (Path(__file__).parent.relative_to(Path.cwd()), True, None),
        # Absolute filename.
        (__file__, True, None),
        # Absolute directory.
        (Path(__file__).parent, True, None),
        # Missing relative path.
        ("test/pouet", False, None),
        # Missing absolute path.
        (Path.cwd() / "test" / "pouet", False, None),
        # Asset filename.
        ("asset:plover:assets/user.json", True, "plover/assets/user.json"),
        # Asset directory.
        ("asset:plover:", True, "plover"),
        ("asset:plover:assets", True, "plover/assets"),
        # Missing asset.
        ("asset:plover:assets/pouet.json", False, "plover/assets/pouet.json"),
        # Invalid asset: missing package and path.
        ("asset:", ValueError, ValueError),
        # Invalid asset: missing path.
        ("asset:package", ValueError, ValueError),
        # Invalid asset: absolute resource path.
        ("asset:plover:/assets/user.json", ValueError, ValueError),
    ),
)
def test_resource(resource, exists, filename):
    resource = str(resource)
    if inspect.isclass(exists):
        exception = exists
        with pytest.raises(exception):
            resource_exists(resource)
        with pytest.raises(exception):
            resource_filename(resource)
        with pytest.raises(exception):
            resource_timestamp(resource)
        return
    assert resource_exists(resource) == exists
    if filename is None:
        filename = resource
    assert normalize_path(resource_filename(resource)) == normalize_path(filename)
    if exists:
        timestamp = Path(filename).stat().st_mtime
        assert resource_timestamp(resource) == timestamp
    else:
        with pytest.raises(FileNotFoundError):
            resource_timestamp(resource)


def test_resource_update(tmp_path):
    # Can't update assets.
    resource = "asset:plover:assets/pouet.json"
    resource_path = Path(resource_filename(resource))
    with pytest.raises(ValueError):
        with resource_update(resource):
            resource_path.write_bytes(b"contents")
    assert not resource_path.exists()
    # Don't update resource on exception (but still cleanup).
    resource = (tmp_path / "resource").resolve()
    exception_str = "Houston, we have a problem"
    with pytest.raises(Exception, match=exception_str):
        with resource_update(str(resource)) as tmpf:
            tmpf = Path(tmpf)
            tmpf.write_bytes(b"contents")
            raise Exception(exception_str)
    assert not resource.exists()
    assert not tmpf.exists()
    # Normal use.
    with resource_update(str(resource)) as tmpf:
        tmpf = Path(tmpf).resolve()
        # Temporary filename must be different.
        assert tmpf != resource
        # And must be empty.
        assert not tmpf.stat().st_size
        # Temporary file must be created in the same
        # directory as the target resource (so an
        # atomic rename can be used).
        assert tmpf.parent == resource.parent
        # Save something.
        tmpf.write_bytes(b"contents")
        st = tmpf.stat()
    assert resource.stat() == st
    assert resource.read_bytes() == b"contents"