Changeset 1655:d66da25dc032


Ignore:
Timestamp:
Jul 26, 2016, 9:07:45 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Adapt `test_stat.py` for pytest

ticket: 98
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_stat.py

    r1635 r1655  
    1 # Copyright (C) 2003-2015, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2003-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    1010import time
    1111import unittest
     12
     13import pytest
    1214
    1315import ftputil
     
    6668              stat_tuple_to_seconds(expected_stat_result[8])
    6769            # Compare lists.
    68             self.assertEqual(stat_result, expected_stat_result)
     70            assert stat_result == expected_stat_result
    6971
    7072    def _test_invalid_lines(self, parser_class, lines):
    7173        parser = parser_class()
    7274        for line in lines:
    73             self.assertRaises(ftputil.error.ParserError,
    74                               parser.parse_line, line)
     75            with pytest.raises(ftputil.error.ParserError):
     76                parser.parse_line(line)
    7577
    7678    def _expected_year(self):
     
    310312        """
    311313        abs_difference = abs(time1 - time2)
    312         try:
    313             self.assertFalse(abs_difference > 60.0)
    314         except AssertionError:
    315             print("Difference is", abs_difference, "seconds")
    316             raise
     314        assert abs_difference <= 60.0, \
     315                 "Difference is %s seconds" % abs_difference
    317316
    318317    def _test_time_shift(self, supposed_time_shift, deviation=0.0):
     
    383382              "st_nlink=1, st_uid='45854', st_gid='200', st_size=4604, "
    384383              "st_atime=None, st_mtime=1453241460.0, st_ctime=None)")
    385         self.assertEqual(repr(stat_result), expected_result)
     384        assert repr(stat_result) == expected_result
    386385
    387386    def test_failing_lstat(self):
    388387        """Test whether `lstat` fails for a nonexistent path."""
    389         self.assertRaises(ftputil.error.PermanentError,
    390                           self.stat._lstat, "/home/sschw/notthere")
    391         self.assertRaises(ftputil.error.PermanentError,
    392                           self.stat._lstat, "/home/sschwarzer/notthere")
     388        with pytest.raises(ftputil.error.PermanentError):
     389            self.stat._lstat("/home/sschw/notthere")
     390        with pytest.raises(ftputil.error.PermanentError):
     391            self.stat._lstat("/home/sschwarzer/notthere")
    393392
    394393    def test_lstat_for_root(self):
     
    400399        possible to do this for the root directory `/`.
    401400        """
    402         self.assertRaises(ftputil.error.RootDirError, self.stat._lstat, "/")
    403         try:
     401        with pytest.raises(ftputil.error.RootDirError) as exc_info:
    404402            self.stat._lstat("/")
    405         except ftputil.error.RootDirError as exc:
    406             self.assertFalse(isinstance(exc, ftputil.error.FTPOSError))
     403        # `RootDirError` is "outside" the `FTPOSError` hierarchy.
     404        assert not isinstance(exc_info.value, ftputil.error.FTPOSError)
     405        del exc_info
    407406
    408407    def test_lstat_one_unix_file(self):
     
    410409        stat_result = self.stat._lstat("/home/sschwarzer/index.html")
    411410        # Second form is needed for Python 3
    412         self.assertTrue(oct(stat_result.st_mode) in ("0100644", "0o100644"))
    413         self.assertEqual(stat_result.st_size, 4604)
    414         self.assertEqual(stat_result._st_mtime_precision, 60)
     411        assert oct(stat_result.st_mode) in ("0100644", "0o100644")
     412        assert stat_result.st_size == 4604
     413        assert stat_result._st_mtime_precision == 60
    415414
    416415    def test_lstat_one_ms_file(self):
     
    418417        self.stat = _test_stat(session_factory=mock_ftplib.MockMSFormatSession)
    419418        stat_result = self.stat._lstat("/home/msformat/abcd.exe")
    420         self.assertEqual(stat_result._st_mtime_precision, 60)
     419        assert stat_result._st_mtime_precision == 60
    421420
    422421    def test_lstat_one_unix_dir(self):
     
    424423        stat_result = self.stat._lstat("/home/sschwarzer/scios2")
    425424        # Second form is needed for Python 3
    426         self.assertTrue(oct(stat_result.st_mode) in ("042755", "0o42755"))
    427         self.assertEqual(stat_result.st_ino, None)
    428         self.assertEqual(stat_result.st_dev, None)
    429         self.assertEqual(stat_result.st_nlink, 6)
    430         self.assertEqual(stat_result.st_uid, "45854")
    431         self.assertEqual(stat_result.st_gid, "200")
    432         self.assertEqual(stat_result.st_size, 512)
    433         self.assertEqual(stat_result.st_atime, None)
    434         self.assertTrue(stat_result.st_mtime ==
    435                         stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0)))
    436         self.assertEqual(stat_result.st_ctime, None)
    437         self.assertEqual(stat_result._st_mtime_precision, 24*60*60)
    438         self.assertTrue(stat_result ==
    439           (17901, None, None, 6, "45854", "200", 512, None,
    440            stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0)), None))
     425        assert oct(stat_result.st_mode) in ("042755", "0o42755")
     426        assert stat_result.st_ino is None
     427        assert stat_result.st_dev is None
     428        assert stat_result.st_nlink == 6
     429        assert stat_result.st_uid == "45854"
     430        assert stat_result.st_gid == "200"
     431        assert stat_result.st_size == 512
     432        assert stat_result.st_atime is None
     433        assert (stat_result.st_mtime ==
     434                stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0)))
     435        assert stat_result.st_ctime is None
     436        assert stat_result._st_mtime_precision == 24*60*60
     437        assert stat_result == (17901, None, None, 6, "45854", "200", 512, None,
     438                               stat_tuple_to_seconds((1999, 9, 20, 0, 0, 0)),
     439                               None)
    441440
    442441    def test_lstat_one_ms_dir(self):
     
    444443        self.stat = _test_stat(session_factory=mock_ftplib.MockMSFormatSession)
    445444        stat_result = self.stat._lstat("/home/msformat/WindowsXP")
    446         self.assertEqual(stat_result._st_mtime_precision, 60)
     445        assert stat_result._st_mtime_precision == 60
    447446
    448447    def test_lstat_via_stat_module(self):
    449448        """Test `lstat` indirectly via `stat` module."""
    450449        stat_result = self.stat._lstat("/home/sschwarzer/")
    451         self.assertTrue(stat.S_ISDIR(stat_result.st_mode))
     450        assert stat.S_ISDIR(stat_result.st_mode)
    452451
    453452    def test_stat_following_link(self):
     
    455454        # Simple link
    456455        stat_result = self.stat._stat("/home/link")
    457         self.assertEqual(stat_result.st_size, 4604)
     456        assert stat_result.st_size == 4604
    458457        # Link pointing to a link
    459458        stat_result = self.stat._stat("/home/python/link_link")
    460         self.assertEqual(stat_result.st_size, 4604)
     459        assert stat_result.st_size == 4604
    461460        stat_result = self.stat._stat("../python/link_link")
    462         self.assertEqual(stat_result.st_size, 4604)
     461        assert stat_result.st_size == 4604
    463462        # Recursive link structures
    464         self.assertRaises(ftputil.error.PermanentError,
    465                           self.stat._stat, "../python/bad_link")
    466         self.assertRaises(ftputil.error.PermanentError,
    467                           self.stat._stat, "/home/bad_link")
     463        with pytest.raises(ftputil.error.PermanentError):
     464            self.stat._stat("../python/bad_link")
     465        with pytest.raises(ftputil.error.PermanentError):
     466            self.stat._stat("/home/bad_link")
    468467
    469468    #
     
    473472        """Test non-switching of parser format with `PermanentError`."""
    474473        self.stat = _test_stat(session_factory=mock_ftplib.MockMSFormatSession)
    475         self.assertEqual(self.stat._allow_parser_switching, True)
     474        assert self.stat._allow_parser_switching is True
    476475        # With these directory contents, we get a `ParserError` for
    477476        # the Unix parser first, so `_allow_parser_switching` can be
    478477        # switched off no matter whether we got a `PermanentError`
    479478        # afterward or not.
    480         self.assertRaises(ftputil.error.PermanentError,
    481                           self.stat._lstat, "/home/msformat/nonexistent")
    482         self.assertEqual(self.stat._allow_parser_switching, False)
     479        with pytest.raises(ftputil.error.PermanentError):
     480            self.stat._lstat("/home/msformat/nonexistent")
     481        assert self.stat._allow_parser_switching is False
    483482
    484483    def test_parser_switching_default_to_unix(self):
    485484        """Test non-switching of parser format; stay with Unix."""
    486         self.assertEqual(self.stat._allow_parser_switching, True)
    487         self.assertTrue(isinstance(self.stat._parser, ftputil.stat.UnixParser))
     485        assert self.stat._allow_parser_switching is True
     486        assert isinstance(self.stat._parser, ftputil.stat.UnixParser)
    488487        stat_result = self.stat._lstat("/home/sschwarzer/index.html")
    489488        # The Unix parser worked, so keep it.
    490         self.assertTrue(isinstance(self.stat._parser, ftputil.stat.UnixParser))
    491         self.assertEqual(self.stat._allow_parser_switching, False)
     489        assert isinstance(self.stat._parser, ftputil.stat.UnixParser)
     490        assert self.stat._allow_parser_switching is False
    492491
    493492    def test_parser_switching_to_ms(self):
    494493        """Test switching of parser from Unix to MS format."""
    495494        self.stat = _test_stat(session_factory=mock_ftplib.MockMSFormatSession)
    496         self.assertEqual(self.stat._allow_parser_switching, True)
    497         self.assertTrue(isinstance(self.stat._parser, ftputil.stat.UnixParser))
     495        assert self.stat._allow_parser_switching is True
     496        assert isinstance(self.stat._parser, ftputil.stat.UnixParser)
    498497        # Parsing the directory `/home/msformat` with the Unix parser
    499498        # fails, so switch to the MS parser.
    500499        stat_result = self.stat._lstat("/home/msformat/abcd.exe")
    501         self.assertTrue(isinstance(self.stat._parser, ftputil.stat.MSParser))
    502         self.assertEqual(self.stat._allow_parser_switching, False)
    503         self.assertEqual(stat_result._st_name, "abcd.exe")
    504         self.assertEqual(stat_result.st_size, 12266720)
     500        assert isinstance(self.stat._parser, ftputil.stat.MSParser)
     501        assert self.stat._allow_parser_switching is False
     502        assert stat_result._st_name == "abcd.exe"
     503        assert stat_result.st_size == 12266720
    505504
    506505    def test_parser_switching_regarding_empty_dir(self):
    507506        """Test switching of parser if a directory is empty."""
    508507        self.stat = _test_stat(session_factory=mock_ftplib.MockMSFormatSession)
    509         self.assertEqual(self.stat._allow_parser_switching, True)
     508        assert self.stat._allow_parser_switching is True
    510509        # When the directory we're looking into doesn't give us any
    511510        # lines we can't decide whether the first parser worked,
    512511        # because it wasn't applied. So keep the parser for now.
    513512        result = self.stat._listdir("/home/msformat/XPLaunch/empty")
    514         self.assertEqual(result, [])
    515         self.assertEqual(self.stat._allow_parser_switching, True)
    516         self.assertTrue(isinstance(self.stat._parser, ftputil.stat.UnixParser))
     513        assert result == []
     514        assert self.stat._allow_parser_switching is True
     515        assert isinstance(self.stat._parser, ftputil.stat.UnixParser)
    517516
    518517
     
    526525    def test_failing_listdir(self):
    527526        """Test failing `FTPHost.listdir`."""
    528         self.assertRaises(ftputil.error.PermanentError,
    529                           self.stat._listdir, "notthere")
     527        with pytest.raises(ftputil.error.PermanentError):
     528            self.stat._listdir("notthere")
    530529
    531530    def test_succeeding_listdir(self):
    532531        """Test succeeding `FTPHost.listdir`."""
    533532        # Do we have all expected "files"?
    534         self.assertEqual(len(self.stat._listdir(".")), 9)
     533        assert len(self.stat._listdir(".")) == 9
    535534        # Have they the expected names?
    536535        expected = ("chemeng download image index.html os2 "
     
    538537        remote_file_list = self.stat._listdir(".")
    539538        for file in expected:
    540             self.assertTrue(file in remote_file_list)
     539            assert file in remote_file_list
    541540
    542541
Note: See TracChangeset for help on using the changeset viewer.