diff --git a/mycroft/util/download.py b/mycroft/util/download.py index 6cb97c3795..9b51cc8600 100644 --- a/mycroft/util/download.py +++ b/mycroft/util/download.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from glob import glob import os from os.path import exists, dirname import subprocess @@ -32,16 +33,11 @@ def _get_download_tmp(dest): (str) path to temporary download location """ tmp_base = dest + '.part' - if not exists(tmp_base): - return tmp_base + existing = glob(tmp_base + '*') + if len(existing) > 0: + return '{}.{}'.format(tmp_base, len(existing)) else: - i = 1 - while(True): - tmp = tmp_base + '.' + str(i) - if not exists(tmp): - return tmp - else: - i += 1 + return tmp_base class Downloader(Thread): diff --git a/test/unittests/util/test_download.py b/test/unittests/util/test_download.py index 3f37b39634..fddde00542 100644 --- a/test/unittests/util/test_download.py +++ b/test/unittests/util/test_download.py @@ -1,7 +1,8 @@ from threading import Event from unittest import TestCase, mock -from mycroft.util.download import download, _running_downloads +from mycroft.util.download import (download, _running_downloads, + _get_download_tmp) TEST_URL = 'http://example.com/mycroft-test.tar.gz' TEST_DEST = '/tmp/file.tar.gz' @@ -83,3 +84,22 @@ class TestDownload(TestCase): self.assertTrue(downloader is downloader2) transfer_done.set() downloader.join() + + +@mock.patch('mycroft.util.download.glob') +class TestGetTemp(TestCase): + def test_no_existing(self, mock_glob): + mock_glob.return_value = [] + dest = '/tmp/test' + self.assertEqual(_get_download_tmp(dest), dest + '.part') + + def test_existing(self, mock_glob): + mock_glob.return_value = ['/tmp/test.part'] + dest = '/tmp/test' + self.assertEqual(_get_download_tmp(dest), dest + '.part.1') + + def test_multiple_existing(self, mock_glob): + mock_glob.return_value = ['/tmp/test.part', '/tmp/test.part.1', + '/tmp/test.part.2'] + dest = '/tmp/test' + self.assertEqual(_get_download_tmp(dest), dest + '.part.3')