Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fork_master
Browse files Browse the repository at this point in the history
New tests - merged with md5 correction
  • Loading branch information
FiloSottile committed Nov 29, 2012
2 parents caaa47d + 20ba042 commit 8192ebe
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 208 deletions.
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -23,6 +23,13 @@ test:
.PHONY: all clean install test README.md youtube-dl.bash-completion
# TODO un-phony README.md and youtube-dl.bash_completion by reading from .in files and generating from them

youtube-dl: youtube_dl/*.py
zip --quiet youtube-dl youtube_dl/*.py
zip --quiet --junk-paths youtube-dl youtube_dl/__main__.py
echo '#!/usr/bin/env python' > youtube-dl
cat youtube-dl.zip >> youtube-dl
rm youtube-dl.zip
chmod a+x youtube-dl

youtube-dl.exe: youtube_dl/*.py
bash devscripts/wine-py2exe.sh build_exe.py
Expand Down
6 changes: 0 additions & 6 deletions __main__.py

This file was deleted.

128 changes: 128 additions & 0 deletions test/gentests.py
@@ -0,0 +1,128 @@
#!/usr/bin/env python3

import io # for python 2
import json
import os
import sys
import unittest

# Allow direct execution
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import youtube_dl.InfoExtractors

HEADER = u'''#!/usr/bin/env python
# DO NOT EDIT THIS FILE BY HAND!
# It is auto-generated from tests.json and gentests.py.
import hashlib
import io
import os
import json
import unittest
import sys
# Allow direct execution
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.FileDownloader import FileDownloader
import youtube_dl.InfoExtractors
def _file_md5(fn):
with open(fn, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()
def md5_for_file(filename, block_size=2**20):
with open(filename) as f:
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
_file_md5 = md5_for_file
try:
_skip_unless = unittest.skipUnless
except AttributeError: # Python 2.6
def _skip_unless(cond, reason='No reason given'):
def resfunc(f):
def wfunc(*args, **kwargs):
if cond:
return f(*args, **kwargs)
else:
print('Skipped test')
return
return wfunc
return resfunc
_skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
class DownloadTest(unittest.TestCase):
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
def setUp(self):
# Clear old files
self.tearDown()
with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
self.parameters = json.load(pf)
'''

FOOTER = u'''
if __name__ == '__main__':
unittest.main()
'''

DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')

def gentests():
with io.open(DEF_FILE, encoding='utf-8') as deff:
defs = json.load(deff)
with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
testf.write(HEADER)
spaces = ' ' * 4
write = lambda l: testf.write(spaces + l + '\n')

for d in defs:
name = d['name']
ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
testf.write('\n')
write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
if not d['file']:
write('@_skip("No output file specified")')
elif 'skip' in d:
write('@_skip(' + repr(d['skip']) + ')')
write('def test_' + name + '(self):')
write(' filename = ' + repr(d['file']))
write(' fd = FileDownloader(self.parameters)')
write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
for ien in d.get('addIEs', []):
write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
write(' fd.download([' + repr(d['url']) + '])')
write(' self.assertTrue(os.path.exists(filename))')
if 'size' in d:
write(' self.assertEqual(os.path.getsize(filename), ' + repr(d['size']) + ')')
if 'md5' in d:
write(' md5_for_file = _file_md5(filename)')
write(' self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')

testf.write('\n\n')
write('def tearDown(self):')
for d in defs:
if d['file']:
write(' if os.path.exists(' + repr(d['file']) + '):')
write(' os.remove(' + repr(d['file']) + ')')
else:
write(' # No file specified for ' + d['name'])
testf.write('\n')
testf.write(FOOTER)

if __name__ == '__main__':
gentests()

0 comments on commit 8192ebe

Please sign in to comment.