Changeset 1811:20115e5cfc46


Ignore:
Timestamp:
Jun 18, 2019, 1:36:31 AM (4 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
histedit_source:
b6f455aea212bf8699747db5e915d098b5bb1390,2d03c1ea4c4d92695011e92eaead5e938b943d1d
Message:
Use Pytest's `tmp_path` fixture

Use the `tmp_path` fixture to make sure that temporary files
are removed. Also, simplify the code even more by using the
`path.read_bytes` and `path.write_bytes` methods.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_host.py

    r1810 r1811  
    245245    """Test upload and download."""
    246246
    247     def generate_file(self, data, file_name):
    248         """Generate a local data file."""
    249         with open(file_name, "wb") as source_file:
    250             source_file.write(data)
    251 
    252     def test_download(self):
     247    def test_download(self, tmp_path):
    253248        """Test mode download."""
    254249        remote_file_name = "dummy_name"
    255250        remote_file_content = b"dummy_content"
    256         local_target = "_test_target_"
     251        local_target = tmp_path / "test_target"
    257252        host_script = [
    258253          Call("__init__"),
     
    271266        ]
    272267        multisession_factory = scripted_session.factory(host_script, file_script)
    273         host = test_base.ftp_host_factory(multisession_factory)
    274268        # Download
    275         with host:
    276             host.download(remote_file_name, local_target)
     269        with test_base.ftp_host_factory(multisession_factory) as host:
     270            host.download(remote_file_name, str(local_target))
    277271        # Verify expected operations on mock socket as done in `FTPFile.close`.
    278272        # We expect one `gettimeout` and two `settimeout` calls.
     
    284278        assert (file_session.sock.settimeout.call_args_list[1] ==
    285279                ((file_session.sock.gettimeout(),), {}))
    286         # Read file and compare
    287         with open(local_target, "rb") as fobj:
    288             data = fobj.read()
    289         assert data == remote_file_content
    290         # Clean up
    291         os.unlink(local_target)
    292 
    293     def test_conditional_upload_without_upload(self):
     280        assert local_target.read_bytes() == remote_file_content
     281
     282    def test_conditional_upload_without_upload(self, tmp_path):
    294283        """
    295284        If the target file is newer, no upload should happen.
    296285        """
    297         local_source = "_test_source_"
     286        local_source = tmp_path / "test_source"
    298287        data = binary_data()
    299         self.generate_file(data, local_source)
     288        local_source.write_bytes(data)
    300289        dir_result = test_base.dir_line(mode_string="-rw-r--r--",
    301290                                        date_=datetime.date.today() +
     
    318307        multisession_factory = scripted_session.factory(script)
    319308        with test_base.ftp_host_factory(multisession_factory) as host:
    320             flag = host.upload_if_newer(local_source, "/newer")
     309            flag = host.upload_if_newer(str(local_source), "/newer")
    321310        assert flag is False
    322311
    323     def test_conditional_upload_with_upload(self):
     312    def test_conditional_upload_with_upload(self, tmp_path):
    324313        """
    325314        If the target file is older or doesn't exist, the source file
    326315        should be uploaded.
    327316        """
     317        local_source = tmp_path / "test_source"
    328318        file_content = b"dummy_content"
    329         local_source = "_test_source_"
    330         self.generate_file(file_content, local_source)
     319        local_source.write_bytes(file_content)
    331320        remote_file_name = "dummy_name"
    332321        dir_result = test_base.dir_line(mode_string="-rw-r--r--",
     
    358347        with unittest.mock.patch("test.test_base.MockableBytesIO.write") as write_mock:
    359348            with test_base.ftp_host_factory(multisession_factory) as host:
    360                 flag = host.upload_if_newer(local_source, "/older")
     349                flag = host.upload_if_newer(str(local_source), "/older")
    361350            write_mock.assert_called_with(file_content)
    362351        assert flag is True
     
    369358        with unittest.mock.patch("test.test_base.MockableBytesIO.write") as write_mock:
    370359            with test_base.ftp_host_factory(multisession_factory) as host:
    371                 flag = host.upload_if_newer(local_source, "/notthere")
     360                flag = host.upload_if_newer(str(local_source), "/notthere")
    372361            write_mock.assert_called_with(file_content)
    373362        assert flag is True
    374         # Clean up.
    375         os.unlink(local_source)
    376 
    377     # FIXME: We always want to delete the unneeded target file, but we
    378     # only want the file content comparison if the previous test
    379     # (whether the file was downloaded) succeeded.
    380     def compare_and_delete_downloaded_data(self, file_name, expected_data):
    381         """
    382         Compare content of downloaded file with its source, then
    383         delete the local target file.
    384         """
    385         with open(file_name, "rb") as fobj:
    386             data = fobj.read()
    387         try:
    388             assert data == expected_data
    389         finally:
    390             os.unlink(file_name)
    391 
    392     def test_conditional_download_without_target(self):
     363
     364    def test_conditional_download_without_target(self, tmp_path):
    393365        """
    394366        Test conditional binary mode download when no target file
    395367        exists.
    396368        """
    397         local_target = "_test_target_"
     369        local_target = tmp_path / "test_target"
    398370        data = binary_data()
    399371        # Target does not exist, so download.
     
    419391        ]
    420392        multisession_factory = scripted_session.factory(host_script, file_script)
    421         try:
    422             with test_base.ftp_host_factory(multisession_factory) as host:
    423                 flag = host.download_if_newer("/newer", local_target)
    424             assert flag is True
    425         finally:
    426             self.compare_and_delete_downloaded_data(local_target, data)
    427 
    428     def test_conditional_download_with_older_target(self):
     393        with test_base.ftp_host_factory(multisession_factory) as host:
     394            flag = host.download_if_newer("/newer", str(local_target))
     395        assert flag is True
     396        assert local_target.read_bytes() == data
     397
     398    def test_conditional_download_with_older_target(self, tmp_path):
    429399        """Test conditional binary mode download with newer source file."""
    430         local_target = "_test_target_"
    431         # Make target file.
    432         with open(local_target, "w"):
    433             pass
     400        local_target = tmp_path / "test_target"
     401        # Make sure file exists for the timestamp comparison.
     402        local_target.touch()
    434403        data = binary_data()
    435404        # Target is older, so download.
     
    462431        ]
    463432        multisession_factory = scripted_session.factory(host_script, file_script)
    464         try:
    465             with test_base.ftp_host_factory(multisession_factory) as host:
    466                 flag = host.download_if_newer("/newer", local_target)
    467             assert flag is True
    468         finally:
    469             self.compare_and_delete_downloaded_data(local_target, data)
    470 
    471     def test_conditional_download_with_newer_target(self):
     433        with test_base.ftp_host_factory(multisession_factory) as host:
     434            flag = host.download_if_newer("/newer", str(local_target))
     435        assert flag is True
     436        assert local_target.read_bytes() == data
     437
     438    def test_conditional_download_with_newer_target(self, tmp_path):
    472439        """Test conditional binary mode download with older source file."""
    473         local_target = "_test_target_"
    474         # Make target file.
    475         with open(local_target, "w"):
    476             pass
     440        local_target = tmp_path / "test_target"
     441        # Make sure file exists for timestamp comparison.
     442        local_target.touch()
    477443        data = binary_data()
    478444        # Use date in the past, so the target file is newer and no
     
    504470        multisession_factory = scripted_session.factory(host_script, file_script)
    505471        with test_base.ftp_host_factory(multisession_factory) as host:
    506             flag = host.download_if_newer("/newer", local_target)
     472            flag = host.download_if_newer("/newer", str(local_target))
    507473        assert flag is False
    508474
     
    727693            host.upload("Makefile", ftputil.tool.as_bytes("target"))
    728694
    729     def test_download(self):
     695    def test_download(self, tmp_path):
    730696        """Test whether `download` accepts either unicode or bytes."""
     697        local_target = tmp_path / "local_target"
    731698        host_script = [
    732699          Call("__init__"),
     
    745712          Call(method_name="close"),
    746713        ]
    747         local_file_name = "_local_target_"
    748714        multisession_factory = scripted_session.factory(host_script, file_script)
    749715        # The source file needs to be present in the current directory.
    750716        with test_base.ftp_host_factory(multisession_factory) as host:
    751             host.download("source", local_file_name)
     717            host.download("source", str(local_target))
    752718        # Create new `BytesIO` object.
    753719        file_script[4] = Call(method_name="transfercmd",
     
    756722        multisession_factory = scripted_session.factory(host_script, file_script)
    757723        with test_base.ftp_host_factory(multisession_factory) as host:
    758             host.download(ftputil.tool.as_bytes("source"), local_file_name)
    759         os.remove(local_file_name)
     724            host.download(ftputil.tool.as_bytes("source"), str(local_target))
    760725
    761726    def test_rename(self):
Note: See TracChangeset for help on using the changeset viewer.