Changeset 1652:f0e353b392fd


Ignore:
Timestamp:
Jul 26, 2016, 8:32:18 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Adapt `test_real_ftp.py` for pytest

ticket: 98
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_real_ftp.py

    r1636 r1652  
    11# encoding: UTF-8
    22
    3 # Copyright (C) 2003-2015, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     3# Copyright (C) 2003-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    44# and ftputil contributors (see `doc/contributors.txt`)
    55# See the file LICENSE for licensing terms.
     
    1818import unittest
    1919import stat
     20
     21import pytest
    2022
    2123import ftputil.compat
     
    136138        host.mkdir(dir_name)
    137139        files = host.listdir(host.curdir)
    138         self.assertTrue(dir_name in files)
     140        assert dir_name in files
    139141        # Try to remove a non-empty directory.
    140142        self.cleaner.add_file(file_name)
     
    157159            host.rmdir(dir_name)
    158160        files = host.listdir(host.curdir)
    159         self.assertTrue(dir_name not in files)
     161        assert dir_name not in files
    160162
    161163    def test_makedirs_without_existing_dirs(self):
     
    167169        self.cleaner.add_dir("_dir1_")
    168170        # Check host.
    169         self.assertTrue(host.path.isdir("_dir1_"))
    170         self.assertTrue(host.path.isdir("_dir1_/dir2"))
    171         self.assertTrue(host.path.isdir("_dir1_/dir2/dir3"))
    172         self.assertTrue(host.path.isdir("_dir1_/dir2/dir3/dir4"))
     171        assert host.path.isdir("_dir1_")
     172        assert host.path.isdir("_dir1_/dir2")
     173        assert host.path.isdir("_dir1_/dir2/dir3")
     174        assert host.path.isdir("_dir1_/dir2/dir3/dir4")
    173175
    174176    def test_makedirs_from_non_root_directory(self):
     
    177179        host = self.host
    178180        # No `_dir1_` and `_dir2_` yet
    179         self.assertFalse("_dir1_" in host.listdir(host.curdir))
    180         self.assertFalse("_dir2_" in host.listdir(host.curdir))
     181        assert "_dir1_" not in host.listdir(host.curdir)
     182        assert "_dir2_" not in host.listdir(host.curdir)
    181183        # Part 1: Try to make directories starting from `_dir1_` and
    182184        # change to non-root directory.
     
    186188        host.makedirs("_dir2_/_dir3_")
    187189        # Test for expected directory hierarchy.
    188         self.assertTrue (host.path.isdir("/_dir1_"))
    189         self.assertTrue (host.path.isdir("/_dir1_/_dir2_"))
    190         self.assertTrue (host.path.isdir("/_dir1_/_dir2_/_dir3_"))
    191         self.assertFalse(host.path.isdir("/_dir1_/_dir1_"))
     190        assert host.path.isdir("/_dir1_")
     191        assert host.path.isdir("/_dir1_/_dir2_")
     192        assert host.path.isdir("/_dir1_/_dir2_/_dir3_")
     193        assert not host.path.isdir("/_dir1_/_dir1_")
    192194        # Remove all but the directory we're in.
    193195        host.rmdir("/_dir1_/_dir2_/_dir3_")
     
    197199        host.makedirs("/_dir2_/_dir3_")
    198200        # Test for expected directory hierarchy
    199         self.assertTrue (host.path.isdir("/_dir2_"))
    200         self.assertTrue (host.path.isdir("/_dir2_/_dir3_"))
    201         self.assertFalse(host.path.isdir("/_dir1_/_dir2_"))
     201        assert host.path.isdir("/_dir2_")
     202        assert host.path.isdir("/_dir2_/_dir3_")
     203        assert not host.path.isdir("/_dir1_/_dir2_")
    202204
    203205    def test_makedirs_of_existing_directory(self):
     
    212214        self.make_remote_file("_dir1_/file1")
    213215        # Try it.
    214         self.assertRaises(ftputil.error.PermanentError,
    215                           host.makedirs, "_dir1_/file1")
    216         self.assertRaises(ftputil.error.PermanentError,
    217                           host.makedirs, "_dir1_/file1/dir2")
     216        with pytest.raises(ftputil.error.PermanentError):
     217            host.makedirs("_dir1_/file1")
     218        with pytest.raises(ftputil.error.PermanentError):
     219            host.makedirs("_dir1_/file1/dir2")
    218220
    219221    def test_makedirs_with_existing_directory(self):
     
    223225        host.makedirs("_dir1_/dir2")
    224226        # Check
    225         self.assertTrue(host.path.isdir("_dir1_"))
    226         self.assertTrue(host.path.isdir("_dir1_/dir2"))
     227        assert host.path.isdir("_dir1_")
     228        assert host.path.isdir("_dir1_/dir2")
    227229
    228230    def test_makedirs_in_non_writable_directory(self):
    229231        host = self.host
    230232        # Preparation: `rootdir1` exists but is only writable by root.
    231         self.assertRaises(ftputil.error.PermanentError, host.makedirs,
    232                           "rootdir1/dir2")
     233        with pytest.raises(ftputil.error.PermanentError):
     234            host.makedirs("rootdir1/dir2")
    233235
    234236    def test_makedirs_with_writable_directory_at_end(self):
     
    254256        self.make_remote_file("_dir1_/dir2/file4")
    255257        # Try to remove a _file_ with `rmtree`.
    256         self.assertRaises(ftputil.error.PermanentError, host.rmtree,
    257                           "_dir1_/file2")
     258        with pytest.raises(ftputil.error.PermanentError):
     259            host.rmtree("_dir1_/file2")
    258260        # Remove `dir2`.
    259261        host.rmtree("_dir1_/dir2")
    260         self.assertFalse(host.path.exists("_dir1_/dir2"))
    261         self.assertTrue (host.path.exists("_dir1_/file2"))
     262        assert not host.path.exists("_dir1_/dir2")
     263        assert host.path.exists("_dir1_/file2")
    262264        # Re-create `dir2` and remove `_dir1_`.
    263265        host.mkdir("_dir1_/dir2")
     
    265267        self.make_remote_file("_dir1_/dir2/file4")
    266268        host.rmtree("_dir1_")
    267         self.assertFalse(host.path.exists("_dir1_"))
     269        assert not host.path.exists("_dir1_")
    268270
    269271    def test_rmtree_with_error_handler(self):
     
    280282        self.assertEqual(log, [])
    281283        host.rmtree("_dir1_/file1", ignore_errors=False, onerror=error_handler)
    282         self.assertEqual(log[0][0], host.listdir)
    283         self.assertEqual(log[0][1], "_dir1_/file1")
    284         self.assertEqual(log[1][0], host.rmdir)
    285         self.assertEqual(log[1][1], "_dir1_/file1")
     284        assert log[0][0] == host.listdir
     285        assert log[0][1] == "_dir1_/file1"
     286        assert log[1][0] == host.rmdir
     287        assert log[1][1] == "_dir1_/file1"
    286288        host.rmtree("_dir1_")
    287289        # Try to remove a non-existent directory.
    288290        del log[:]
    289291        host.rmtree("_dir1_", ignore_errors=False, onerror=error_handler)
    290         self.assertEqual(log[0][0], host.listdir)
    291         self.assertEqual(log[0][1], "_dir1_")
    292         self.assertEqual(log[1][0], host.rmdir)
    293         self.assertEqual(log[1][1], "_dir1_")
     292        assert log[0][0] == host.listdir
     293        assert log[0][1] == "_dir1_"
     294        assert log[1][0] == host.rmdir
     295        assert log[1][1] == "_dir1_"
    294296
    295297    def test_remove_non_existent_item(self):
    296298        host = self.host
    297         self.assertRaises(ftputil.error.PermanentError, host.remove,
    298                           "nonexistent")
     299        with pytest.raises(ftputil.error.PermanentError):
     300            host.remove("nonexistent")
    299301
    300302    def test_remove_existing_file(self):
     
    302304        self.make_remote_file("_testfile_")
    303305        host = self.host
    304         self.assertTrue(host.path.isfile("_testfile_"))
     306        assert host.path.isfile("_testfile_")
    305307        host.remove("_testfile_")
    306         self.assertFalse(host.path.exists("_testfile_"))
     308        assert not host.path.exists("_testfile_")
    307309
    308310
     
    337339            actual_result.append(items)
    338340        # Compare with expected results.
    339         self.assertEqual(len(actual_result), len(expected_result))
     341        assert len(actual_result) == len(expected_result)
    340342        for index, _ in enumerate(actual_result):
    341             self.assertEqual(actual_result[index], expected_result[index])
     343            assert actual_result[index] == expected_result[index]
    342344
    343345    def test_walk_topdown(self):
     
    465467        self.make_remote_file("_testfile1_")
    466468        host.rename("_testfile1_", "_testfile2_")
    467         self.assertFalse(host.path.exists("_testfile1_"))
    468         self.assertTrue (host.path.exists("_testfile2_"))
     469        assert not host.path.exists("_testfile1_")
     470        assert host.path.exists("_testfile2_")
    469471
    470472    def test_rename_with_spaces_in_directory(self):
     
    475477        self.make_remote_file(dir_name + "/testfile1")
    476478        host.rename(dir_name + "/testfile1", dir_name + "/testfile2")
    477         self.assertFalse(host.path.exists(dir_name + "/testfile1"))
    478         self.assertTrue (host.path.exists(dir_name + "/testfile2"))
     479        assert not host.path.exists(dir_name + "/testfile1")
     480        assert host.path.exists(dir_name + "/testfile2")
    479481
    480482
     
    493495        # - dir
    494496        dir_stat = host.stat(dir_name)
    495         self.assertTrue(isinstance(dir_stat._st_name,
    496                                    ftputil.compat.unicode_type))
    497         self.assertEqual(host.listdir(dir_name), ["_nonempty_"])
    498         self.assertTrue (host.path.isdir (dir_name))
    499         self.assertFalse(host.path.isfile(dir_name))
    500         self.assertFalse(host.path.islink(dir_name))
     497        assert isinstance(dir_stat._st_name, ftputil.compat.unicode_type)
     498        assert host.listdir(dir_name) == ["_nonempty_"]
     499        assert host.path.isdir(dir_name)
     500        assert not host.path.isfile(dir_name)
     501        assert not host.path.islink(dir_name)
    501502        # - file
    502503        file_stat = host.stat(file_name)
    503         self.assertTrue(isinstance(file_stat._st_name,
    504                                    ftputil.compat.unicode_type))
    505         self.assertFalse(host.path.isdir (file_name))
    506         self.assertTrue (host.path.isfile(file_name))
    507         self.assertFalse(host.path.islink(file_name))
    508         self.assertEqual(host.path.getsize(file_name), 9)
     504        assert isinstance(file_stat._st_name, ftputil.compat.unicode_type)
     505        assert not host.path.isdir(file_name)
     506        assert host.path.isfile(file_name)
     507        assert not host.path.islink(file_name)
     508        assert host.path.getsize(file_name) == 9
    509509        # - file's modification time; allow up to two minutes difference
    510510        host.synchronize_times()
     
    512512        client_mtime = time.mktime(time.localtime())
    513513        calculated_time_shift = server_mtime - client_mtime
    514         self.assertFalse(abs(calculated_time_shift-host.time_shift()) > 120)
     514        assert not abs(calculated_time_shift-host.time_shift()) > 120
    515515
    516516    def test_issomething_for_nonexistent_directory(self):
     
    519519        # directory doesn't exist (see ticket #66).
    520520        nonexistent_path = "/nonexistent/nonexistent"
    521         self.assertFalse(host.path.isdir (nonexistent_path))
    522         self.assertFalse(host.path.isfile(nonexistent_path))
    523         self.assertFalse(host.path.islink(nonexistent_path))
     521        assert not host.path.isdir(nonexistent_path)
     522        assert not host.path.isfile(nonexistent_path)
     523        assert not host.path.islink(nonexistent_path)
    524524
    525525    def test_special_broken_link(self):
     
    527527        host = self.host
    528528        broken_link_name = os.path.join("dir_with_broken_link", "nonexistent")
    529         self.assertEqual(host.lstat(broken_link_name)._st_target,
    530                          "../nonexistent/nonexistent")
    531         self.assertFalse(host.path.isdir (broken_link_name))
    532         self.assertFalse(host.path.isfile(broken_link_name))
    533         self.assertTrue (host.path.islink(broken_link_name))
     529        assert (host.lstat(broken_link_name)._st_target ==
     530                "../nonexistent/nonexistent")
     531        assert not host.path.isdir(broken_link_name)
     532        assert not host.path.isfile(broken_link_name)
     533        assert host.path.islink(broken_link_name)
    534534
    535535    def test_concurrent_access(self):
     
    539539                stat_result1 = host1.stat("_testfile_")
    540540                stat_result2 = host2.stat("_testfile_")
    541                 self.assertEqual(stat_result1, stat_result2)
     541                assert stat_result1 == stat_result2
    542542                host2.remove("_testfile_")
    543543                # Can still get the result via `host1`
    544544                stat_result1 = host1.stat("_testfile_")
    545                 self.assertEqual(stat_result1, stat_result2)
     545                assert stat_result1 == stat_result2
    546546                # Stat'ing on `host2` gives an exception.
    547                 self.assertRaises(ftputil.error.PermanentError,
    548                                   host2.stat, "_testfile_")
     547                with pytest.raises(ftputil.error.PermanentError):
     548                    host2.stat("_testfile_")
    549549                # Stat'ing on `host1` after invalidation
    550550                absolute_path = host1.path.join(host1.getcwd(), "_testfile_")
    551551                host1.stat_cache.invalidate(absolute_path)
    552                 self.assertRaises(ftputil.error.PermanentError,
    553                                   host1.stat, "_testfile_")
     552                with pytest.raises(ftputil.error.PermanentError):
     553                    host1.stat("_testfile_")
    554554
    555555    def test_cache_auto_resizing(self):
     
    559559        # Make sure the cache size isn't adjusted towards smaller values.
    560560        unused_entries = host.listdir("walk_test")
    561         self.assertEqual(cache.size,
    562                          ftputil.stat_cache.StatCache._DEFAULT_CACHE_SIZE)
     561        assert cache.size == ftputil.stat_cache.StatCache._DEFAULT_CACHE_SIZE
    563562        # Make the cache very small initially and see if it gets resized.
    564563        cache.size = 2
     
    569568        expected_min_cache_size = max(len(host.listdir(host.curdir)),
    570569                                      len(entries))
    571         self.assertTrue(cache.size >= expected_min_cache_size)
     570        assert cache.size >= expected_min_cache_size
    572571
    573572
     
    577576    def test_time_shift(self):
    578577        self.host.synchronize_times()
    579         self.assertEqual(self.host.time_shift(), EXPECTED_TIME_SHIFT)
     578        assert self.host.time_shift() == EXPECTED_TIME_SHIFT
    580579
    581580    @test.skip_long_running_test
     
    595594            # Retry; shouldn't be uploaded
    596595            uploaded = host.upload_if_newer(local_file, remote_file)
    597             self.assertEqual(uploaded, False)
     596            assert uploaded is False
    598597            # Rewrite the local file.
    599598            self.make_local_file()
    600599            # Retry; should be uploaded now
    601600            uploaded = host.upload_if_newer(local_file, remote_file)
    602             self.assertEqual(uploaded, True)
     601            assert uploaded is True
    603602        finally:
    604603            # Clean up
     
    615614        # File should be downloaded as it's not present yet.
    616615        downloaded = host.download_if_newer(remote_file, local_file)
    617         self.assertEqual(downloaded, True)
     616        assert downloaded is True
    618617        try:
    619618            # If the remote file, taking the datetime precision into
     
    627626            # Local file is present and newer, so shouldn't download.
    628627            downloaded = host.download_if_newer(remote_file, local_file)
    629             self.assertEqual(downloaded, False)
     628            assert downloaded is False
    630629            # Re-make the remote file.
    631630            self.make_remote_file(remote_file)
     
    634633            # so should download.
    635634            downloaded = host.download_if_newer(remote_file, local_file)
    636             self.assertEqual(downloaded, True)
     635            assert downloaded is True
    637636        finally:
    638637            # Clean up.
     
    663662                    expected_chunks_list.append(chunk)
    664663            # Examine data collected by callback function.
    665             self.assertEqual(len(transferred_chunks_list), chunk_count)
    666             self.assertEqual(transferred_chunks_list, expected_chunks_list)
     664            assert len(transferred_chunks_list) == chunk_count
     665            assert transferred_chunks_list == expected_chunks_list
    667666        finally:
    668667            os.unlink(FILE_NAME)
     
    681680            # closed but the second is.
    682681            with host.open(REMOTE_FILE_NAME, "rb") as file_obj:
    683                 self.assertEqual(len(host._children), 2)
    684                 self.assertTrue(file_obj._host is host._children[1])
     682                assert len(host._children) == 2
     683                assert file_obj._host is host._children[1]
    685684
    686685    def test_no_timed_out_children(self):
     
    696695        # Try to get a file - which shouldn't be the timed-out file.
    697696        with host.open(REMOTE_FILE_NAME, "rb") as file_obj2:
    698             self.assertTrue(file_obj1 is not file_obj2)
     697            assert file_obj1 is not file_obj2
    699698        # Re-use closed and not timed-out child session.
    700699        with host.open(REMOTE_FILE_NAME, "rb") as file_obj3:
    701700            pass
    702         self.assertTrue(file_obj2 is file_obj3)
     701        assert file_obj2 is file_obj3
    703702
    704703    def test_no_delayed_226_children(self):
     
    714713        # Try to get a file - which shouldn't be the timed-out file.
    715714        with host.open(REMOTE_FILE_NAME, "rb") as file_obj2:
    716             self.assertTrue(file_obj1 is not file_obj2)
     715            assert file_obj1 is not file_obj2
    717716        # Re-use closed and not timed-out child session.
    718717        with host.open(REMOTE_FILE_NAME, "rb") as file_obj3:
    719718            pass
    720         self.assertTrue(file_obj2 is file_obj3)
     719        assert file_obj2 is file_obj3
    721720
    722721
     
    741740        allowed_mask = functools.reduce(operator.or_, allowed_flags)
    742741        mode = full_mode & allowed_mask
    743         self.assertEqual(mode, expected_mode,
    744                          "mode {0:o} != {1:o}".format(mode, expected_mode))
     742        assert mode == expected_mode, (
     743                 "mode {0:o} != {1:o}".format(mode, expected_mode))
    745744
    746745    def test_chmod_existing_directory(self):
     
    769768    def test_chmod_nonexistent_path(self):
    770769        # Set/get mode of a non-existing item.
    771         self.assertRaises(ftputil.error.PermanentError, self.host.chmod,
    772                           "nonexistent", 0o757)
     770        with pytest.raises(ftputil.error.PermanentError):
     771            self.host.chmod("nonexistent", 0o757)
    773772
    774773    def test_cache_invalidation(self):
     
    808807        with self.host.open(self.TEST_FILE_NAME, "rb", rest=3) as fobj:
    809808            data = fobj.read()
    810         self.assertEqual(data, b"defghijkl")
     809        assert data == b"defghijkl"
    811810
    812811    def test_for_writing(self):
     
    819818        with self.host.open(self.TEST_FILE_NAME, "rb") as fobj:
    820819            data = fobj.read()
    821         self.assertEqual(data, b"abc123")
     820        assert data == b"abc123"
    822821
    823822    def test_invalid_read_from_text_file(self):
     
    826825        a `CommandNotImplementedError` should be raised.
    827826        """
    828         with self.assertRaises(ftputil.error.CommandNotImplementedError):
     827        with pytest.raises(ftputil.error.CommandNotImplementedError):
    829828            self.host.open(self.TEST_FILE_NAME, "r", rest=3)
    830829
     
    834833        a `CommandNotImplementedError` should be raised.
    835834        """
    836         with self.assertRaises(ftputil.error.CommandNotImplementedError):
     835        with pytest.raises(ftputil.error.CommandNotImplementedError):
    837836            self.host.open(self.TEST_FILE_NAME, "w", rest=3)
    838837
     
    876875        with self.host.open("debian-keyring.tar.gz", "rb") as file2:
    877876            pass
    878         self.assertTrue(file1._session is file2._session)
     877        assert file1._session is file2._session
    879878
    880879    def test_names_with_spaces(self):
     
    882881        # can be used.
    883882        host = self.host
    884         self.assertTrue(host.path.isdir("dir with spaces"))
    885         self.assertEqual(host.listdir("dir with spaces"),
    886                          ["second dir", "some file", "some_file"])
    887         self.assertTrue(host.path.isdir ("dir with spaces/second dir"))
    888         self.assertTrue(host.path.isfile("dir with spaces/some_file"))
    889         self.assertTrue(host.path.isfile("dir with spaces/some file"))
     883        assert host.path.isdir("dir with spaces")
     884        assert (host.listdir("dir with spaces") ==
     885                ["second dir", "some file", "some_file"])
     886        assert host.path.isdir("dir with spaces/second dir")
     887        assert host.path.isfile("dir with spaces/some_file")
     888        assert host.path.isfile("dir with spaces/some file")
    890889
    891890    def test_synchronize_times_without_write_access(self):
     
    894893        # This isn't writable by the ftp account the tests are run under.
    895894        host.chdir("rootdir1")
    896         self.assertRaises(ftputil.error.TimeShiftError, host.synchronize_times)
     895        with pytest.raises(ftputil.error.TimeShiftError):
     896            host.synchronize_times()
    897897
    898898    def test_listdir_with_non_ascii_byte_string(self):
     
    905905        path = "äbc".encode("UTF-8")
    906906        names = host.listdir(path)
    907         self.assertEqual(names[0], b"file1")
    908         self.assertEqual(names[1], "file1_ö".encode("UTF-8"))
     907        assert names[0] == b"file1"
     908        assert names[1] == "file1_ö".encode("UTF-8")
    909909
    910910    def test_listdir_with_non_ascii_unicode_string(self):
     
    920920        path = "äbc".encode("UTF-8").decode("latin1")
    921921        names = host.listdir(path)
    922         self.assertEqual(names[0], "file1")
    923         self.assertEqual(names[1], "file1_ö".encode("UTF-8").decode("latin1"))
     922        assert names[0] == "file1"
     923        assert names[1] == "file1_ö".encode("UTF-8").decode("latin1")
    924924
    925925    def test_path_with_non_latin1_unicode_string(self):
     
    932932        # `UnicodeEncodeError` is also the exception that `ftplib`
    933933        # raises if it gets a non-latin1 path.
    934         with self.assertRaises(UnicodeEncodeError):
     934        with pytest.raises(UnicodeEncodeError):
    935935            self.host.mkdir(path)
    936936
     
    940940        # option is used.
    941941        host = self.host
    942         self.assertTrue(host.use_list_a_option)
     942        assert host.use_list_a_option
    943943        directory_entries = host.listdir(host.curdir)
    944         self.assertTrue(".hidden" in directory_entries)
     944        assert ".hidden" in directory_entries
    945945        host.use_list_a_option = False
    946946        directory_entries = host.listdir(host.curdir)
    947         self.assertFalse(".hidden" in directory_entries)
     947        assert ".hidden" not in directory_entries
    948948
    949949    def _make_objects_to_be_garbage_collected(self):
     
    962962        gc.collect()
    963963        objects_after_test = len(gc.garbage)
    964         self.assertFalse(objects_after_test - objects_before_test)
     964        assert not objects_after_test - objects_before_test
    965965
    966966    @unittest.skipIf(ftputil.compat.python_version > 2,
     
    981981            # Test if unicode argument works.
    982982            files = host.listdir(".")
    983         self.assertTrue("CONTENTS" in files)
     983        assert "CONTENTS" in files
    984984
    985985
Note: See TracChangeset for help on using the changeset viewer.