From 498887969cf44967506a01a6777c918fc3696199 Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Jun 14 2022 13:29:30 +0000 Subject: fix --- diff --git a/tests/test_cli/test_download_file.py b/tests/test_cli/test_download_file.py index aa5aecd..5d00902 100644 --- a/tests/test_cli/test_download_file.py +++ b/tests/test_cli/test_download_file.py @@ -36,6 +36,7 @@ class TestDownloadFile(unittest.TestCase): self.stdout = mock.patch('sys.stdout', new_callable=six.StringIO).start() self.stderr = mock.patch('sys.stderr', new_callable=six.StringIO).start() self.request_with_retry = mock.patch('koji.request_with_retry').start() + self.get_mock = self.request_with_retry.return_value.get self.head = mock.patch('requests.head').start() # will work when contextlib.closing will be removed in future # self.request_with_retry = self.request_with_retry.return_value.__enter__ @@ -56,47 +57,54 @@ class TestDownloadFile(unittest.TestCase): else: self.assertEqual(cm.exception.args, (21, 'Is a directory')) + @mock.patch('os.unlink') @mock_open() - def test_handle_download_file(self, m_open): + def test_handle_download_file(self, m_open, x): self.reset_mock() - headers = mock.MagicMock() + m_open.return_value.tell.return_value = 0 rsp_head = self.head.return_value rsp_head.status_code = 200 - rsp_head.headers = {'Content-Length': '5' } + rsp_head.headers = {'Content-Length': '5'} response = mock.MagicMock() - self.request_with_retry.return_value = response + self.get_mock.return_value = response response.headers.get.return_value = '5' # content-length response.iter_content.return_value = ['abcde'] rv = download_file("http://url", self.filename) actual = self.stdout.getvalue() - expected = 'Downloading: %s\n[====================================] 100%% 5.00 B\r\n' % self.filename + expected = 'Downloading: %s\n[====================================] 100%% 5.00 B / 5.00 B\r\n' % self.filename self.assertMultiLineEqual(actual, expected) - self.request_with_retry.assert_called_once() + self.get_mock.assert_called_once() m_open.assert_called_once() - response.headers.get.assert_called_once() + response.headers.get.assert_not_called() response.iter_content.assert_called_once() self.assertIsNone(rv) + @mock.patch('os.unlink') @mock_open() - def test_handle_download_file_undefined_length(self, m_open): + def test_handle_download_file_undefined_length(self, m_open, x): self.reset_mock() + m_open.return_value.tell.return_value = 0 + rsp_head = self.head.return_value + rsp_head.status_code = 200 + rsp_head.headers = {'Content-Length': str(65536 * 2)} response = mock.MagicMock() - self.request_with_retry.return_value = response + self.get_mock.return_value = response response.headers.get.return_value = None # content-length response.iter_content.return_value = ['a' * 65536, 'b' * 65536] rv = download_file("http://url", self.filename) actual = self.stdout.getvalue() - expected = 'Downloading: %s\n[ ] ???%% 64.00 KiB\r[ ] ???%% 128.00 KiB\r[====================================] 100%% 128.00 KiB\r\n' % self.filename + print(repr(actual)) + expected = 'Downloading: %s\n[================== ] 50%% 64.00 KiB / 128.00 KiB\r[====================================] 100%% 128.00 KiB / 128.00 KiB\r\n' % self.filename self.assertMultiLineEqual(actual, expected) - self.request_with_retry.assert_called_once() + self.get_mock.assert_called_once() m_open.assert_called_once() - response.headers.get.assert_called_once() + response.headers.get.assert_not_called() response.iter_content.assert_called_once() self.assertIsNone(rv) @@ -106,7 +114,7 @@ class TestDownloadFile(unittest.TestCase): actual = self.stdout.getvalue() expected = 'Downloading [8/10]: %s\n\n' % self.filename self.assertMultiLineEqual(actual, expected) - self.request_with_retry.assert_called_once() + self.get_mock.assert_called_once() self.assertIsNone(rv) def test_handle_download_file_quiet_noprogress(self):