CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_file_util.py96 linesDownload Raw Back to tests
1"""Tests for distutils.file_util."""2 3import errno4import os5import unittest.mock as mock6from distutils.errors import DistutilsFileError7from distutils.file_util import copy_file, move_file8 9import jaraco.path10import pytest11 12 13@pytest.fixture(autouse=True)14def stuff(request, tmp_path):15    self = request.instance16    self.source = tmp_path / 'f1'17    self.target = tmp_path / 'f2'18    self.target_dir = tmp_path / 'd1'19 20 21class TestFileUtil:22    def test_move_file_verbosity(self, caplog):23        jaraco.path.build({self.source: 'some content'})24 25        move_file(self.source, self.target, verbose=False)26        assert not caplog.messages27 28        # back to original state29        move_file(self.target, self.source, verbose=False)30 31        move_file(self.source, self.target, verbose=True)32        wanted = [f'moving {self.source} -> {self.target}']33        assert caplog.messages == wanted34 35        # back to original state36        move_file(self.target, self.source, verbose=False)37 38        caplog.clear()39        # now the target is a dir40        os.mkdir(self.target_dir)41        move_file(self.source, self.target_dir, verbose=True)42        wanted = [f'moving {self.source} -> {self.target_dir}']43        assert caplog.messages == wanted44 45    def test_move_file_exception_unpacking_rename(self):46        # see issue 2218247        with (48            mock.patch("os.rename", side_effect=OSError("wrong", 1)),49            pytest.raises(DistutilsFileError),50        ):51            jaraco.path.build({self.source: 'spam eggs'})52            move_file(self.source, self.target, verbose=False)53 54    def test_move_file_exception_unpacking_unlink(self):55        # see issue 2218256        with (57            mock.patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")),58            mock.patch("os.unlink", side_effect=OSError("wrong", 1)),59            pytest.raises(DistutilsFileError),60        ):61            jaraco.path.build({self.source: 'spam eggs'})62            move_file(self.source, self.target, verbose=False)63 64    def test_copy_file_hard_link(self):65        jaraco.path.build({self.source: 'some content'})66        # Check first that copy_file() will not fall back on copying the file67        # instead of creating the hard link.68        try:69            os.link(self.source, self.target)70        except OSError as e:71            self.skipTest(f'os.link: {e}')72        else:73            self.target.unlink()74        st = os.stat(self.source)75        copy_file(self.source, self.target, link='hard')76        st2 = os.stat(self.source)77        st3 = os.stat(self.target)78        assert os.path.samestat(st, st2), (st, st2)79        assert os.path.samestat(st2, st3), (st2, st3)80        assert self.source.read_text(encoding='utf-8') == 'some content'81 82    def test_copy_file_hard_link_failure(self):83        # If hard linking fails, copy_file() falls back on copying file84        # (some special filesystems don't support hard linking even under85        #  Unix, see issue #8876).86        jaraco.path.build({self.source: 'some content'})87        st = os.stat(self.source)88        with mock.patch("os.link", side_effect=OSError(0, "linking unsupported")):89            copy_file(self.source, self.target, link='hard')90        st2 = os.stat(self.source)91        st3 = os.stat(self.target)92        assert os.path.samestat(st, st2), (st, st2)93        assert not os.path.samestat(st2, st3), (st2, st3)94        for fn in (self.source, self.target):95            assert fn.read_text(encoding='utf-8') == 'some content'96 
Aluode/PerceptionLabPortable · CoolFace