1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-09 09:59:51 +01:00

Merge pull request #62 from vikstrous/more_tests

more tests
This commit is contained in:
Viktor Stanchev 2015-09-14 22:41:07 -07:00
commit 06d551f69d
5 changed files with 191 additions and 16 deletions

View File

@ -10,6 +10,6 @@ test:
- coverage run -m unittest discover
post:
- mkdir -p $CIRCLE_ARTIFACTS/coverage
- cd /home/ubuntu/pirate-get && coverage html --include=`pwd`*
- cd /home/ubuntu/pirate-get && coverage html --include=`pwd`* --omit="*/tests/*,*__init__*"
- cp -R /home/ubuntu/pirate-get/htmlcov/* $CIRCLE_ARTIFACTS/coverage
- coveralls

View File

@ -6,6 +6,8 @@ import configparser
import socket
import urllib.request as request
import urllib.error
import sys
import webbrowser
import pirate.data
@ -17,7 +19,7 @@ from os.path import expanduser, expandvars
from pirate.print import print
def load_config():
def parse_config_file(text):
config = configparser.ConfigParser()
# default options
@ -31,16 +33,12 @@ def load_config():
config.set('LocalDB', 'path', expanduser('~/downloads/pirate-get/db'))
config.add_section('Misc')
# TODO: try to use https://docs.python.org/3/library/configparser.html#configparser.BasicInterpolation for interpolating in the command
config.set('Misc', 'openCommand', '')
config.set('Misc', 'transmission', 'false')
config.set('Misc', 'colors', 'true')
# user-defined config files
main = expandvars('$XDG_CONFIG_HOME/pirate-get')
alt = expanduser('~/.config/pirate-get')
# read config file
config.read([main] if os.path.isfile(main) else [alt])
config.read_string(text)
# expand env variables
directory = expanduser(expandvars(config.get('Save', 'Directory')))
@ -51,6 +49,22 @@ def load_config():
return config
def load_config():
# user-defined config files
main = expandvars('$XDG_CONFIG_HOME/pirate-get')
alt = expanduser('~/.config/pirate-get')
# read config file
if os.path.isfile(main):
with open(main) as f:
return parse_config_file(f.read())
if os.path.isfile(alt):
with open(alt) as f:
return parse_config_file(f.read())
return parse_config_file("")
def parse_cmd(cmd, url):
cmd_args_regex = r'''(('[^']*'|"[^"]*"|(\\\s|[^\s])+)+ *)'''
@ -103,9 +117,7 @@ def parse_torrent_command(l):
return code, choices
def main():
config = load_config()
def parse_args(args_in):
parser = argparse.ArgumentParser(
description='finds and downloads torrents from the Pirate Bay')
parser.add_argument('-b', dest='browse',
@ -158,7 +170,7 @@ def main():
parser.add_argument('--disable-colors', dest='color',
action='store_false',
help='disable colored output')
args = parser.parse_args()
args = parser.parse_args(args_in)
# figure out the mode - browse, search, top or recent
if args.browse:
@ -170,6 +182,14 @@ def main():
else:
args.mode = 'search'
return args
def main():
config = load_config()
args = parse_args(sys.argv[1:])
if (config.getboolean('Misc', 'colors') and not args.color
or not config.getboolean('Misc', 'colors')):
pirate.data.colored_output = False

View File

@ -28,7 +28,7 @@ def parse_category(category):
return pirate.data.categories[category]
else:
print('Invalid category ignored', color='WARN')
return '0'
return 0
def parse_sort(sort):
@ -42,7 +42,7 @@ def parse_sort(sort):
return pirate.data.sorts[sort]
else:
print('Invalid sort ignored', color='WARN')
return '99'
return 99
#TODO: warn users when using a sort in a mode that doesn't accept sorts
@ -161,6 +161,7 @@ def get_torrent(info_hash):
return torrent.read()
# TODO: handle slashes in torrent names
def save_torrents(chosen_links, results, folder):
for link in chosen_links:
magnet = results[link]['magnet']
@ -178,7 +179,8 @@ def save_torrents(chosen_links, results, folder):
print('Saved {:X} in {}'.format(info_hash, file))
def save_magnets(chosen_links, mags, folder):
# TODO: handle slashes in torrent names
def save_magnets(chosen_links, results, folder):
for link in chosen_links:
magnet = results[link]['magnet']
name = re.search(r'dn=([^\&]*)', magnet)

View File

@ -32,9 +32,68 @@ class TestPirate(unittest.TestCase):
[['1d'], ('d', [1])],
[['1 ... d'], ('d', [1])],
[['1-3 d'], ('d', [1,2,3])],
[['1-3'], (None, [1,2,3])],
]
for test in tests:
self.assertEqual(pirate.pirate.parse_torrent_command(*test[0]), test[1])
def test_parse_config_file(self):
types = {
'Save': {
'magnets': bool,
'Magnets': bool,
'torrents': bool,
'directory': str,
},
'LocalDB': {
'enabled': bool,
'path': str,
}
}
config1 = """
[Save]
magnets=False
directory=dir
[LocalDB]
enabled=true
path=abc
"""
config2 = """
[Save]
Magnets=True
"""
tests = [
(config1, {'Save': {'magnets': False}}),
(config1, {'Save': {'torrents': False}}),
(config1, {'Save': {'directory': 'dir'}}),
(config1, {'LocalDB': {'enabled': True}}),
(config1, {'LocalDB': {'path': 'abc'}}),
(config2, {'Save': {'magnets': True}}),
]
for test in tests:
config = pirate.pirate.parse_config_file(test[0])
for section in test[1].keys():
for name in test[1][section].keys():
if types[section][name] == bool:
lhs = config.getboolean(section, name)
else:
lhs = config.get(section, name)
rhs = test[1][section][name]
self.assertEqual(lhs, rhs)
def test_parse_args(self):
tests = [
(['-b'], {'mode': 'browse'}),
([], {'mode': 'top'}),
(['-R'], {'mode': 'recent'}),
(['internets'], {'mode': 'search', 'search': ['internets']}),
(['internets lol', 'lel'], {'mode': 'search', 'search': ['internets lol', 'lel']}),
]
for test in tests:
config = pirate.pirate.parse_args(test[0])
for option in test[1].keys():
value = getattr(config, option)
self.assertEqual(test[1][option], value)
if __name__ == '__main__':
unittest.main()

File diff suppressed because one or more lines are too long