示例#1
0
def test_argv_by_name(monkeypatch):
    """
    Check that the program accepts a song or list of songs by name, and
    returns a list of song objects with the information it parsed.
    """
    # Run it first with only one song
    artist, title = 'judas priest', 'no surrender'
    new_args = [__file__, f'{artist} - {title}']
    param_song = Song(artist, title)
    monkeypatch.setattr(sys, 'argv', new_args)
    songs = parse_argv()
    assert songs == set([param_song])

    # Then try with multiple songs
    param_songs = [
        Song('kreator', 'mars mantra'),
        Song('nervosa', 'morbid courage'),
        Song('ac/dc', 'night prowler'),
    ]
    new_args = [__file__]
    for p_song in param_songs:
        new_args.append(f'{p_song.artist} - {p_song.title}')
    monkeypatch.setattr(sys, 'argv', new_args)
    songs = parse_argv()
    assert songs == set(param_songs)
示例#2
0
def test_argv_flag(monkeypatch, arg, config):
    """
    Test the arguments that work as flags, and set an attribute of CONFIG to
    True.
    """
    new_argv = ['python', __file__, arg]
    monkeypatch.setattr(sys, 'argv', new_argv)
    parse_argv()
    assert CONFIG[config]
示例#3
0
def test_argv_incompatible(monkeypatch, args):
    """
    Check that the `parse_argv` function raises some kind of error when these
    incompatible arguments are passed at the same time.
    """
    new_args = [__file__] + args
    monkeypatch.setattr(sys, 'argv', new_args)
    with pytest.raises(SystemExit):
        parse_argv()
示例#4
0
def test_argv_invalid_jobs(monkeypatch, num):
    """
    Check that `parse_argv()` raises some kind of error when an invalid number
    of jobs is passed.
    """
    new_argv = ['python', __file__, '-j', str(num)]

    current_value = CONFIG['jobcount']
    monkeypatch.setattr(sys, 'argv', new_argv)
    with pytest.raises(SystemExit):
        parse_argv()
    assert CONFIG['jobcount'] == current_value
示例#5
0
def test_argv_param(monkeypatch, arg, config, klass):
    """
    Test the arguments that accept a parameter and store it CONFIG.
    """
    new_argv = ['python', __file__, arg]
    if klass is int:
        param = random.randint(1, 10)
    new_argv.append(str(param))

    monkeypatch.setattr(sys, 'argv', new_argv)
    parse_argv()
    assert CONFIG[config] == param
示例#6
0
def test_argv_recursive(monkeypatch, mp3file):
    """
    Check that the `-r` flag searches recursively for mp3 files and returns a
    list with all the names found.
    """
    tmpdir = Path(tempfile.mkdtemp())
    tempdir_dir = tmpdir / 'dir'
    tempdir_dir.mkdir()
    mp3s = [
        tmpdir / 'first.mp3',
        tmpdir / 'second.mp3',
        tempdir_dir / 'third.mp3',
        tempdir_dir / 'fourth.mp3',
    ]
    for mp3 in mp3s:
        shutil.copyfile(mp3file, mp3)

    monkeypatch.setattr(sys, 'argv', [__file__, '-r'])
    with chdir(str(tmpdir)):
        songs = parse_argv()
        songs = set(Path(s.filename).absolute() for s in songs)
    try:
        assert set(mp3s) == songs
    finally:
        shutil.rmtree(tmpdir)
示例#7
0
def test_argv_from_file(monkeypatch, tmpdir, mp3file):
    """
    Check that the `--from_file` argument can read a text file containing a
    list of filenames, and return a list with all of them.
    """
    mp3_files = [
        tmpdir / 'first.mp3',
        tmpdir / 'second.mp3',
        tmpdir / 'third.mp3',
    ]
    song_tags = [
        ('white wizzard', 'the sun also rises'),
        ('mastodon', 'andromeda'),
        ('megadeth', 'dawn patrol'),
    ]
    songs = []
    for filename, tag in zip(mp3_files, song_tags):
        artist, title = tag
        shutil.copyfile(mp3file, filename)
        tag_mp3(filename, artist=artist, title=title)
        songs.append(Song(artist=artist, title=title))

    filelist = tmpdir / 'filelist'
    with open(filelist, 'w') as file:
        for filename in mp3_files:
            file.write(str(filename) + '\n')
        file.flush()

    monkeypatch.setattr(sys, 'argv', [__file__, '--from-file', str(filelist)])
    parsed_songs = parse_argv()

    assert parsed_songs == set(parsed_songs)
示例#8
0
def test_argv_empty(monkeypatch):
    """
    Check that, with no arguments, the program searches for the currently
    playing song.
    """
    current_song = Song('Fallujah', 'Assemblage of wolves')

    def fake_get_current_song():
        return current_song

    new_args = [__file__]
    monkeypatch.setattr(sys, 'argv', new_args)
    monkeypatch.setattr(lyricfetch.cli, 'get_current_song',
                        fake_get_current_song)
    songs = parse_argv()
    assert songs == set([current_song])
示例#9
0
def test_argv_filename(monkeypatch, mp3file, tmpdir):
    """
    Test that the main script can accept a list of file names passed as
    parameters, and return them all in a list.
    """
    mp3s = [
        tmpdir / 'first.mp3',
        tmpdir / 'second.mp3',
        tmpdir / 'third.mp3',
    ]
    new_args = [__file__]
    for mp3 in mp3s:
        shutil.copyfile(mp3file, mp3)
        new_args.append(str(mp3))

    monkeypatch.setattr(sys, 'argv', new_args)
    songs = parse_argv()
    songs = set(pypath(s.filename) for s in songs)
    assert set(mp3s) == songs
示例#10
0
def test_argv_recursive_path(monkeypatch, mp3file, tmpdir):
    """
    Check that the `-r` flag can search in a different path given as an
    argument too.
    """
    tempdir_dir = tmpdir / 'dir'
    tempdir_dir.mkdir()
    mp3s = [
        tmpdir / 'first.mp3',
        tmpdir / 'second.mp3',
        tempdir_dir / 'third.mp3',
        tempdir_dir / 'fourth.mp3',
    ]
    for mp3 in mp3s:
        shutil.copyfile(mp3file, mp3)

    monkeypatch.setattr(sys, 'argv', [__file__, '-r', str(tmpdir)])
    songs = parse_argv()
    songs = set(pypath(s.filename) for s in songs)
    assert set(mp3s) == songs