A feature-rich command-line audio/video downloader
Move try_rm to test helpers
Philipp Hagemeisterf4aac741d5c9
diff --git a/test/helper.py b/test/helper.pyindex 8e641e3cb3..884cf32dc2 100644--- a/test/helper.py+++ b/test/helper.py@@ -1,3 +1,4 @@+import errnoimport ioimport jsonimport os.path@@ -22,18 +23,33 @@ PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parawith io.open(PARAMETERS_FILE, encoding='utf-8') as pf:parameters = json.load(pf)++def try_rm(filename):+ """ Remove a file if it exists """+ try:+ os.remove(filename)+ except OSError as ose:+ if ose.errno != errno.ENOENT:+ raise++class FakeYDL(YoutubeDL):def __init__(self):- self.result = []# Different instances of the downloader can't share the same dictionary# some test set the "sublang" parameter, which would break the md5 checks.- self.params = dict(parameters)- def to_screen(self, s):+ params = dict(parameters)+ super(FakeYDL, self).__init__(params)+ self.result = []++ def to_screen(self, s, skip_eol=None):print(s)+def trouble(self, s, tb=None):raise Exception(s)+def download(self, x):self.result.append(x)+def expect_warning(self, regex):# Silence an expected warning matching a regexold_report_warning = self.report_warningdiff --git a/test/test_download.py b/test/test_download.pyindex 23a66254d8..23d3853c4d 100644--- a/test/test_download.py+++ b/test/test_download.py@@ -1,6 +1,5 @@#!/usr/bin/env python-import errnoimport hashlibimport ioimport os@@ -28,14 +27,6 @@ opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, Youcompat_urllib_request.install_opener(opener)socket.setdefaulttimeout(10)-def _try_rm(filename):- """ Remove a file if it exists """- try:- os.remove(filename)- except OSError as ose:- if ose.errno != errno.ENOENT:- raise-md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()class YoutubeDL(youtube_dl.YoutubeDL):@@ -54,7 +45,7 @@ def _file_md5(fn):with open(fn, 'rb') as f:return hashlib.md5(f.read()).hexdigest()-from helper import get_testcases+from helper import get_testcases, try_rmdefs = get_testcases()with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:@@ -97,9 +88,9 @@ def generator(test_case):test_cases = test_case.get('playlist', [test_case])for tc in test_cases:- _try_rm(tc['file'])- _try_rm(tc['file'] + '.part')- _try_rm(tc['file'] + '.info.json')+ try_rm(tc['file'])+ try_rm(tc['file'] + '.part')+ try_rm(tc['file'] + '.info.json')try:for retry in range(1, RETRIES + 1):try:@@ -145,9 +136,9 @@ def generator(test_case):self.assertTrue(key in info_dict.keys() and info_dict[key])finally:for tc in test_cases:- _try_rm(tc['file'])- _try_rm(tc['file'] + '.part')- _try_rm(tc['file'] + '.info.json')+ try_rm(tc['file'])+ try_rm(tc['file'] + '.part')+ try_rm(tc['file'] + '.info.json')return test_template