1import shutil
2import pytest
3import eyed3
4from uuid import uuid4
5from pathlib import Path
6
7
8DATA_D = Path(__file__).parent / "data"
9
10
11def _tempCopy(src, dest_dir):
12    testfile = Path(str(dest_dir)) / "{}.mp3".format(uuid4())
13    shutil.copyfile(str(src), str(testfile))
14    return testfile
15
16
17@pytest.fixture(scope="function")
18def audiofile(tmpdir):
19    """Makes a copy of test.mp3 and loads it using eyed3.load()."""
20    if not Path(DATA_D).exists():
21        yield None
22        return
23
24    testfile = _tempCopy(DATA_D / "test.mp3", tmpdir)
25    yield eyed3.load(testfile)
26    if testfile.exists():
27        testfile.unlink()
28
29
30@pytest.fixture(scope="function")
31def id3tag():
32    """Returns a default-constructed eyed3.id3.Tag."""
33    from eyed3.id3 import Tag
34    return Tag()
35
36
37@pytest.fixture(scope="function")
38def image(tmpdir):
39    img_file = _tempCopy(DATA_D / "CypressHill3TemplesOfBoom.jpg", tmpdir)
40    return img_file
41
42
43@pytest.fixture(scope="session")
44def eyeD3():
45    """A fixture for running `eyeD3` default plugin.
46    `eyeD3(audiofile, args, expected_retval=0, reload_version=None)`
47    """
48    from eyed3 import main
49
50    def func(audiofile, args, expected_retval=0, reload_version=None):
51        try:
52            args, _, config = main.parseCommandLine(args + [audiofile.path])
53            retval = main.main(args, config)
54        except SystemExit as sys_exit:
55            retval = sys_exit.code
56        assert retval == expected_retval
57        return eyed3.load(audiofile.path, tag_version=reload_version)
58
59    return func
60