X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=test.py;h=14e4667d07c3c916f25f52231240da7ac329f64e;hb=2d715cb0549de365d41e5e95daef269723fde809;hp=79a26934251b50559e6865f18ccce443b8d1e4b3;hpb=72c3f57ce7971bb440ca3591d99b7638efa3547b;p=python-musicpd.git diff --git a/test.py b/test.py index 79a2693..14e4667 100755 --- a/test.py +++ b/test.py @@ -36,24 +36,66 @@ TEST_MPD_HOST, TEST_MPD_PORT = ('example.com', 10000) class testEnvVar(unittest.TestCase): def test_envvar(self): - os.environ.pop('MPD_HOST', None) - os.environ.pop('MPD_PORT', None) - client = musicpd.MPDClient() - self.assertEqual(client.host, 'localhost') - self.assertEqual(client.port, '6600') + # mock "os.path.exists" here to ensure there are no socket in + # XDG_RUNTIME_DIR/mpd or /run/mpd since with test defaults fallbacks + # when : + # * neither MPD_HOST nor XDG_RUNTIME_DIR are not set + # * /run/mpd does not expose a socket + with mock.patch('os.path.exists', return_value=False): + os.environ.pop('MPD_HOST', None) + os.environ.pop('MPD_PORT', None) + client = musicpd.MPDClient() + self.assertEqual(client.host, 'localhost') + self.assertEqual(client.port, '6600') + + os.environ.pop('MPD_HOST', None) + os.environ['MPD_PORT'] = '6666' + client = musicpd.MPDClient() + self.assertEqual(client.pwd, None) + self.assertEqual(client.host, 'localhost') + self.assertEqual(client.port, '6666') + # Test password extraction os.environ['MPD_HOST'] = 'pa55w04d@example.org' client = musicpd.MPDClient() self.assertEqual(client.pwd, 'pa55w04d') self.assertEqual(client.host, 'example.org') - self.assertEqual(client.port, '6600') - os.environ.pop('MPD_HOST', None) - os.environ['MPD_PORT'] = '6666' + # Test host alone + os.environ['MPD_HOST'] = 'example.org' client = musicpd.MPDClient() + self.assertFalse(client.pwd) + self.assertEqual(client.host, 'example.org') + + # Test password extraction (no host) + os.environ['MPD_HOST'] = 'pa55w04d@' + with mock.patch('os.path.exists', return_value=False): + client = musicpd.MPDClient() + self.assertEqual(client.pwd, 'pa55w04d') + self.assertEqual(client.host, 'localhost') + + # Test badly formatted MPD_HOST + os.environ['MPD_HOST'] = '@' + with mock.patch('os.path.exists', return_value=False): + client = musicpd.MPDClient() self.assertEqual(client.pwd, None) self.assertEqual(client.host, 'localhost') - self.assertEqual(client.port, '6666') + + # Test unix socket extraction + os.environ['MPD_HOST'] = 'pa55w04d@/unix/sock' + client = musicpd.MPDClient() + self.assertEqual(client.host, '/unix/sock') + + # Test plain abstract socket extraction + os.environ['MPD_HOST'] = '@abstract' + client = musicpd.MPDClient() + self.assertEqual(client.host, '@abstract') + + # Test password and abstract socket extraction + os.environ['MPD_HOST'] = 'pass@@abstract' + client = musicpd.MPDClient() + self.assertEqual(client.pwd, 'pass') + self.assertEqual(client.host, '@abstract') # Test unix socket fallback os.environ.pop('MPD_HOST', None) @@ -62,6 +104,9 @@ class testEnvVar(unittest.TestCase): with mock.patch('os.path.exists', return_value=True): client = musicpd.MPDClient() self.assertEqual(client.host, '/run/mpd/socket') + os.environ['XDG_RUNTIME_DIR'] = '/run/user/1000' + client = musicpd.MPDClient() + self.assertEqual(client.host, '/run/user/1000/mpd/socket') os.environ.pop('MPD_HOST', None) os.environ.pop('MPD_PORT', None) @@ -70,12 +115,15 @@ class testEnvVar(unittest.TestCase): client = musicpd.MPDClient() self.assertEqual(client.host, '/run/user/1000/mpd/socket') + # Test MPD_TIMEOUT os.environ.pop('MPD_TIMEOUT', None) client = musicpd.MPDClient() - self.assertEqual(client.mpd_timeout, 30) + self.assertEqual(client.mpd_timeout, musicpd.CONNECTION_TIMEOUT) os.environ['MPD_TIMEOUT'] = 'garbage' client = musicpd.MPDClient() - self.assertEqual(client.mpd_timeout, 30) + self.assertEqual(client.mpd_timeout, + musicpd.CONNECTION_TIMEOUT, + 'Garbage\'s not silently ignore to use default value') os.environ['MPD_TIMEOUT'] = '42' client = musicpd.MPDClient() self.assertEqual(client.mpd_timeout, 42) @@ -461,6 +509,21 @@ class TestMPDClient(unittest.TestCase): res = self.client.albumart('muse/Raised Fist/2002-Dedication/', 0) self.assertEqual(res.get('data'), data) + def test_command_list(self): + self.MPDWillReturn('updating_db: 42\n', + f'{musicpd.NEXT}\n', + 'repeat: 0\n', + 'random: 0\n', + f'{musicpd.NEXT}\n', + f'{musicpd.NEXT}\n', + 'OK\n') + self.client.command_list_ok_begin() + self.client.update() + self.client.status() + self.client.repeat(1) + self.client.command_list_end() + self.assertMPDReceived('command_list_end\n') + if __name__ == '__main__': unittest.main()