def test_download(self): with TemporaryDirectory() as tmpdir: cache_dir = CacheDir('sub-dir', cache_root=tmpdir) with assets_server() as (server, url): self.assertEqual(0, server.counter[0]) # no cache path = cache_dir.download(url + 'payload.zip', show_progress=True) self.assertEqual( os.path.join(cache_dir.path, 'payload.zip'), path) self.assertTrue(os.path.isfile(path)) self.assertFalse(os.path.isfile(path + '._downloading_')) self.assertEqual(1, server.counter[0]) # having cache path = cache_dir.download(url + 'payload.zip', show_progress=True) self.assertEqual( os.path.join(cache_dir.path, 'payload.zip'), path) self.assertTrue(os.path.isfile(path)) self.assertFalse(os.path.isfile(path + '._downloading_')) self.assertEqual(1, server.counter[0]) # no cache, because of filename mismatch out_file = LogIO() path = cache_dir.download(url + 'payload.zip', filename='sub-dir/payload2.zip', show_progress=False, progress_file=out_file) self.assertEqual( os.path.join(cache_dir.path, 'sub-dir/payload2.zip'), path) self.assertTrue(os.path.isfile(path)) self.assertFalse(os.path.isfile(path + '._downloading_')) self.assertEqual(2, server.counter[0]) self.assertEqual( out_file.getvalue(), 'Downloading ' + url + 'payload.zip ... ok\n' ) # test download error out_file = LogIO() with pytest.raises(Exception, match='404'): _ = cache_dir.download(url + 'not-exist.zip', progress_file=out_file) path = os.path.join(cache_dir.path, 'not-exist.zip') self.assertFalse(os.path.isfile(path)) self.assertFalse(os.path.isfile(path + '._downloading_')) self.assertEqual( out_file.getvalue(), 'Downloading ' + url + 'not-exist.zip ... error\n' )
def test_extract_file(self): with TemporaryDirectory() as tmpdir: cache_dir = CacheDir('sub-dir', cache_root=tmpdir) # old_open = Extractor.open # Extractor.open = Mock(wraps=old_open) self.assertEqual(0, PatchedExtractor.call_count) # no cache log_file = LogIO() path = cache_dir.extract_file(get_asset_path('payload.tar.gz'), progress_file=log_file, show_progress=True) self.assertEqual(1, PatchedExtractor.call_count) self.assertEqual( os.path.join(cache_dir.path, 'payload'), path) self.assertListEqual(PAYLOAD_CONTENT, summarize_dir(path)) self.assertFalse(os.path.isdir(path + '._extracting_')) self.assertEqual( 'Extracting {} ... ok\n'.format( get_asset_path('payload.tar.gz')), log_file.getvalue() ) # having cache log_file = LogIO() path = cache_dir.extract_file(get_asset_path('payload.tgz'), progress_file=log_file, show_progress=True) self.assertEqual(1, PatchedExtractor.call_count) self.assertEqual( os.path.join(cache_dir.path, 'payload'), path) self.assertListEqual(PAYLOAD_CONTENT, summarize_dir(path)) self.assertFalse(os.path.isdir(path + '._extracting_')) self.assertEqual('', log_file.getvalue()) # no cache, because of extract_dir mismatch log_file = LogIO() path = cache_dir.extract_file(get_asset_path('payload.tar.bz2'), extract_dir='sub-dir/payload2', show_progress=False, progress_file=log_file) self.assertEqual(2, PatchedExtractor.call_count) self.assertEqual( os.path.join(cache_dir.path, 'sub-dir/payload2'), path) self.assertListEqual(PAYLOAD_CONTENT, summarize_dir(path)) self.assertFalse(os.path.isdir(path + '._extracting_')) self.assertEqual( 'Extracting {} ... ok\n'.format( get_asset_path('payload.tar.bz2')), log_file.getvalue() ) # test extracting error err_archive = os.path.join(cache_dir.path, 'invalid.txt') with open(err_archive, 'wb') as f: f.write(b'not a valid archive') log_file = LogIO() with pytest.raises(Exception): _ = cache_dir.extract_file( err_archive, progress_file=log_file, show_progress=True) path = os.path.join(cache_dir.path, 'invalid') self.assertFalse(os.path.isdir(path)) self.assertFalse(os.path.isdir(path + '._extracting_')) self.assertEqual( 'Extracting {} ... error\n'.format(err_archive), log_file.getvalue() )