File: test_disk_filesystem.py

package info (click to toggle)
python-maison 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 816 kB
  • sloc: python: 1,549; makefile: 9; sh: 5
file content (57 lines) | stat: -rw-r--r-- 1,514 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
import pathlib

from maison import disk_filesystem


class TestGetFilePath:
    def test_get_file_path_finds_in_current_dir(self, tmp_path: pathlib.Path):
        fs = disk_filesystem.DiskFilesystem()

        file = tmp_path / "settings.json"
        _ = file.write_text("{}")

        result = fs.get_file_path("settings.json", starting_path=tmp_path)

        assert result == file

    def test_get_file_path_traverses_up(self, tmp_path: pathlib.Path):
        fs = disk_filesystem.DiskFilesystem()

        nested = tmp_path / "a" / "b"
        nested.mkdir(parents=True)

        file = tmp_path / "a" / "target.txt"
        _ = file.write_text("found me")

        result = fs.get_file_path("target.txt", starting_path=nested)

        assert result == file

    def test_get_file_path_with_absolute_path(self, tmp_path: pathlib.Path):
        fs = disk_filesystem.DiskFilesystem()

        file = tmp_path / "absolute.txt"
        _ = file.write_text("hello")

        result = fs.get_file_path(str(file))

        assert result == file

    def test_get_file_path_returns_none_if_not_found(self):
        fs = disk_filesystem.DiskFilesystem()

        result = fs.get_file_path("ghost.ini")

        assert result is None


class TestOpenFile:
    def test_opens_file(self, tmp_path: pathlib.Path):
        fs = disk_filesystem.DiskFilesystem()

        file = tmp_path / "thing.txt"
        _ = file.write_text("hello")

        result = fs.open_file(path=file)

        assert result.read() == b"hello"