File: test_util_cdrom.py

package info (click to toggle)
picard 2.13.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,012 kB
  • sloc: python: 68,818; ansic: 89; makefile: 14
file content (130 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


import io
from typing import Iterable
import unittest

from test.picardtestcase import PicardTestCase

from picard.const.sys import IS_WIN
from picard.util import cdrom


MOCK_CDROM_INFO = """CD-ROM information, Id: cdrom.c 3.20 2003/12/17

drive name:		sr1	sr0
drive speed:		24	24
drive # of slots:	1	1
Can close tray:		0	1
Can open tray:		1	1
Can lock tray:		1	1
Can change speed:	1	1
Can select disk:	0	0
Can read multisession:	1	1
Can read MCN:		1	1
Reports media changed:	1	1
Can play audio:		1	0
Can write CD-R:		1	1
Can write CD-RW:	1	1
Can read DVD:		1	1
Can write DVD-R:	1	1
Can write DVD-RAM:	1	1
Can read MRW:		1	1
Can write MRW:		1	1
Can write RAM:		1	1
"""

MOCK_CDROM_INFO_EMPTY = """CD-ROM information, Id: cdrom.c 3.20 2003/12/17

drive name:
drive speed:
drive # of slots:
Can close tray:
Can open tray:
Can lock tray:
Can change speed:
Can select disk:
Can read multisession:
Can read MCN:
Reports media changed:
Can play audio:
Can write CD-R:
Can write CD-RW:
Can read DVD:
Can write DVD-R:
Can write DVD-RAM:
Can read MRW:
Can write MRW:
Can write RAM:
"""


class LinuxParseCdromInfoTest(PicardTestCase):

    def test_drives(self):
        with io.StringIO(MOCK_CDROM_INFO) as f:
            drives = list(cdrom._parse_linux_cdrom_info(f))
            self.assertEqual([('sr1', True), ('sr0', False)], drives)

    def test_empty(self):
        with io.StringIO(MOCK_CDROM_INFO_EMPTY) as f:
            drives = list(cdrom._parse_linux_cdrom_info(f))
            self.assertEqual([], drives)

    def test_empty_string(self):
        with io.StringIO("") as f:
            drives = list(cdrom._parse_linux_cdrom_info(f))
            self.assertEqual([], drives)


class GetCdromDrivesTest(PicardTestCase):

    def test_get_cdrom_drives(self):
        self.set_config_values({"cd_lookup_device": "/dev/cdrom"})
        # Independent of the implementation get_cdrom_drives must not rais
        # and return an Iterable.
        drives = cdrom.get_cdrom_drives()
        self.assertIsInstance(drives, Iterable)
        self.assertTrue(set(cdrom.DEFAULT_DRIVES).issubset(drives))

    def test_generic_iter_drives(self):
        self.set_config_values({"cd_lookup_device": "/dev/cdrom"})
        self.assertEqual(["/dev/cdrom"], list(cdrom._generic_iter_drives()))
        self.set_config_values({"cd_lookup_device": "/dev/cdrom, /dev/sr0"})
        self.assertEqual(["/dev/cdrom", "/dev/sr0"], list(cdrom._generic_iter_drives()))
        self.set_config_values({"cd_lookup_device": ""})
        self.assertEqual([], list(cdrom._generic_iter_drives()))
        self.set_config_values({"cd_lookup_device": " ,, ,\t, "})
        self.assertEqual([], list(cdrom._generic_iter_drives()))


@unittest.skipUnless(IS_WIN, "windows test")
class WindowsGetCdromDrivesTest(PicardTestCase):

    def test_autodetect(self):
        self.assertTrue(cdrom.AUTO_DETECT_DRIVES)

    def test_iter_drives(self):
        drives = cdrom._iter_drives()
        self.assertIsInstance(drives, Iterable)
        # This should not raise
        list(drives)