File: test_atomic_write.py

package info (click to toggle)
trash-cli 0.24.5.26-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,972 kB
  • sloc: python: 9,789; sh: 121; makefile: 11
file content (56 lines) | stat: -rw-r--r-- 1,427 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
import errno
import os
import unittest

import pytest
from trashcli.fs import (
    atomic_write,
    open_for_write_in_exclusive_and_create_mode,
    read_file,
)

from tests.support.dirs.my_path import MyPath


@pytest.mark.slow
class Test_atomic_write(unittest.TestCase):
    def setUp(self):
        self.temp_dir = MyPath.make_temp_dir()

    def test_the_second_open_should_fail(self):
        path = self.temp_dir / "a"
        file_handle = open_for_write_in_exclusive_and_create_mode(path)
        try:
            open_for_write_in_exclusive_and_create_mode(path)
            self.fail()
        except OSError as e:
            assert e.errno == errno.EEXIST
        os.close(file_handle)

    def test_short_filename(self):
        path = self.temp_dir / 'a'

        atomic_write(path, b'contents')

        assert 'contents' == read_file(path)

    def test_too_long_filename(self):
        path = self.temp_dir / ('a' * 2000)

        try:
            atomic_write(path, b'contents')
            self.fail()
        except OSError as e:
            assert e.errno == errno.ENAMETOOLONG

    def test_filename_already_taken(self):
        atomic_write(self.temp_dir / "a", b'contents')

        try:
            atomic_write(self.temp_dir / "a", b'contents')
            self.fail()
        except OSError as e:
            assert e.errno == errno.EEXIST

    def tearDown(self):
        self.temp_dir.clean_up()