def test_nonexisting_torrent_file(capsys): nonexising_path = '/no/such/file' with pytest.raises(MainError, match=rf'^{nonexising_path}: No such file or directory$' ) as exc_info: run(['-i', nonexising_path]) assert exc_info.value.errno == errno.ENOENT
def test_custom_configfile_doesnt_exist(tmpdir, mock_content, mock_create_mode): cfgfile = tmpdir.join('wrong_special_config') with pytest.raises(ConfigError, match=f"^{str(cfgfile)}: No such file or directory$"): run(['--config', str(cfgfile), str(mock_content)]) assert mock_create_mode.call_args is None
def test_trackers___multiple_trackers_per_tier(capsys, create_torrent, human_readable): trackers = [ 'http://tracker1.1', ['http://tracker2.1', 'http://tracker2.2'], ['http://tracker3.1'] ] with create_torrent(trackers=trackers) as torrent_file: with human_readable(True): run(['-i', torrent_file]) cap = capsys.readouterr() exp_trackers = ''' Trackers Tier 1: http://tracker1.1 Tier 2: http://tracker2.1 http://tracker2.2 Tier 3: http://tracker3.1''' assert exp_trackers in cap.out exp_trackers = ('http://tracker1.1\thttp://tracker2.1\t' 'http://tracker2.2\thttp://tracker3.1') with human_readable(False): run(['-i', torrent_file]) cap = capsys.readouterr() assert re.search(rf'^Trackers\t{exp_trackers}$', cap.out, flags=re.MULTILINE)
def test_edit_excluded_files_without_path(create_torrent, tmpdir): outfile = str(tmpdir.join('out.torrent')) with create_torrent() as infile: orig = torf.Torrent.read(infile) with pytest.raises(MainError, match=r'^--exclude requires PATH$') as exc_info: run(['-i', infile, '--exclude', 'something', '-o', outfile])
def test_edit_comment(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent(comment='A comment') as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--comment', 'A different comment', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, comment='A different comment')
def test_no_changes(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent() as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new)
def test_edit_name(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent() as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--name', 'new name', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, ignore=('name', 'files', 'filetree')) assert new.name == 'new name' assert tuple(new.files) == tuple( path.replace(orig.name, 'new name') for path in orig.files) assert new.filetree == { 'new name': { 'Anotherthing.iso': torf.Torrent.File(name='Anotherthing.iso', path='new name/Anotherthing.iso', dir='new name', size=9), 'Something.jpg': torf.Torrent.File(name='Something.jpg', path='new name/Something.jpg', dir='new name', size=9), 'Thirdthing.txt': torf.Torrent.File(name='Thirdthing.txt', path='new name/Thirdthing.txt', dir='new name', size=9) } }
def test_edit_source(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent(source='the source') as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--source', 'another source', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, source='another source')
def test_torrent_unreadable(mock_content): with pytest.raises( MainError, match=r'^nonexisting.torrent: No such file or directory$' ) as exc_info: run([str(mock_content), '-i', 'nonexisting.torrent']) assert exc_info.value.errno == errno.ENOENT
def test_edit_path(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) new_content = tmpdir.mkdir('new content') new_file = new_content.join('some file') new_file.write('different data') with create_torrent() as infile: orig = torf.Torrent.read(infile) run(['-i', infile, str(new_content), '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, ignore=('files', 'filetree', 'name', 'piece_size', 'pieces', 'size')) assert tuple(new.files) == ('new content/some file', ) assert new.filetree == { 'new content': { 'some file': torf.Torrent.File(name='some file', path='new content/some file', dir='new content', size=14) } } assert new.name == 'new content' assert new.size == len('different data')
def test_remove_creator(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent(created_by='The creator') as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--nocreator', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, created_by=None)
def test_add_private(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent(private=False) as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--private', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, private=True)
def test_edit_path_with_exclude_option(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) new_content = tmpdir.mkdir('new content') new_file1 = new_content.join('some image.jpg') new_file1.write('image data') new_file2 = new_content.join('some text.txt') new_file2.write('text data') with create_torrent() as infile: orig = torf.Torrent.read(infile) run([ '-i', infile, str(new_content), '--exclude', '*.txt', '-o', outfile ]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, ignore=('files', 'filetree', 'name', 'piece_size', 'pieces', 'size')) assert tuple(new.files) == ('new content/some image.jpg', ) assert new.filetree == { 'new content': { 'some image.jpg': None, } } assert new.name == 'new content' assert new.size == len('image data')
def test_remove_webseeds(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent( webseeds=['http://webseed1', 'http://webseed2']) as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--nowebseed', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, webseeds=None)
def test_remove_trackers(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent( trackers=['http://tracker1', 'http://tracker2']) as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--notracker', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, trackers=None)
def test_invalid_date(capsys, mock_content): content_path = str(mock_content) exp_torrent_filename = os.path.basename(content_path) + '.torrent' exp_torrent_filepath = os.path.join(os.getcwd(), exp_torrent_filename) with pytest.raises(MainError, match=r'^foo: Invalid date$') as exc_info: run([content_path, '--date', 'foo']) assert exc_info.value.errno == errno.EINVAL
def test_config_unreadable(cfgfile, mock_content, mock_create_mode): cfgfile.write('something') import os os.chmod(cfgfile, 0o000) with pytest.raises(ConfigError, match=f"^{str(cfgfile)}: Permission denied$"): run([str(mock_content)]) assert mock_create_mode.call_args is None
def test_insufficient_permissions(capsys, create_torrent): with create_torrent() as torrent_file: os.chmod(torrent_file, 0o000) with pytest.raises( MainError, match=rf'^{torrent_file}: Permission denied$') as exc_info: run(['-i', torrent_file]) assert exc_info.value.errno == errno.EACCES
def test_custom_configfile(tmpdir, mock_content, mock_create_mode): cfgfile = tmpdir.join('special_config') cfgfile.write(textwrap.dedent(''' comment = asdf ''')) run(['--config', str(cfgfile), str(mock_content)]) cfg = mock_create_mode.call_args[0][0] assert cfg['comment'] == 'asdf'
def test_nonexisting_input(): nonexisting_path = '/no/such/file' with pytest.raises( MainError, match=rf'^{nonexisting_path}: No such file or directory$' ) as exc_info: run(['-i', nonexisting_path, '-o', 'out.torrent']) assert exc_info.value.errno == errno.ENOENT
def test_remove_xseed(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent(randomize_infohash=True) as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--noxseed', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, randomize_infohash=False) assert orig.infohash != new.infohash
def test_unwritable_output(create_torrent): unwritable_path = '/out.torrent' with create_torrent() as infile: with pytest.raises( MainError, match=rf'^{unwritable_path}: Permission denied$') as exc_info: run(['-i', infile, '-o', unwritable_path]) assert exc_info.value.errno == errno.EACCES
def test_invalid_boolean_name(cfgfile, mock_content, mock_create_mode): cfgfile.write(textwrap.dedent(''' foo ''')) with pytest.raises( ConfigError, match=f"^{str(cfgfile)}: Unrecognized arguments: --foo$"): run([str(mock_content)]) assert mock_create_mode.call_args is None
def test_PATH_unreadable(create_torrent): with create_torrent() as torrent_file: t = torf.Torrent.read(torrent_file) with pytest.raises( MainError, match=r'^path/to/nothing: No such file or directory$' ) as exc_info: run(['path/to/nothing', '-i', torrent_file]) assert exc_info.value.errno == errno.ENOENT
def test_unknown_profile(cfgfile, mock_content, mock_create_mode): cfgfile.write(textwrap.dedent(''' [foo] comment = Foo! ''')) with pytest.raises(ConfigError, match=rf'^{str(cfgfile)}: No such profile: bar$'): run([str(mock_content), '--profile', 'bar']) assert mock_create_mode.call_args is None
def test_existing_output(create_torrent, tmpdir): outfile = tmpdir.join('out.torrent') outfile.write('some existing file content') with create_torrent() as infile: with pytest.raises( MainError, match=rf'^{str(outfile)}: File exists$') as exc_info: run(['-i', infile, '-o', str(outfile)]) assert exc_info.value.errno == errno.EEXIST
def test_adding_to_list_via_cli(cfgfile, mock_content, mock_create_mode): cfgfile.write( textwrap.dedent(''' tracker = https://foo tracker = https://bar ''')) run([str(mock_content), '--tracker', 'https://baz']) cfg = mock_create_mode.call_args[0][0] assert cfg['tracker'] == ['https://foo', 'https://bar', 'https://baz']
def test_invalid_creation_date(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent() as infile: orig = torf.Torrent.read(infile) with pytest.raises(MainError, match=r'^foo: Invalid date$') as exc_info: run(['-i', infile, '--date', 'foo', '-o', outfile]) assert exc_info.value.errno == 22 assert not os.path.exists(outfile)
def test_edit_creation_date(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent() as infile: orig = torf.Torrent.read(infile) run(['-i', infile, '--date', '3000-05-30 15:03:01', '-o', outfile]) new = torf.Torrent.read(outfile) assert_torrents_equal(orig, new, creation_date=datetime(3000, 5, 30, 15, 3, 1))
def test_invalid_webseed_url(create_torrent, tmpdir, assert_torrents_equal): outfile = str(tmpdir.join('out.torrent')) with create_torrent( webseeds=['http://webseed1', 'http://webseed2']) as infile: orig = torf.Torrent.read(infile) with pytest.raises(MainError, match=r'^not a url: Invalid URL$') as exc_info: run(['-i', infile, '--webseed', 'not a url', '-o', outfile]) assert exc_info.value.errno == 22 assert not os.path.exists(outfile)