1# Copyright 2014 Nick Boultbee
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8import os
9
10from senf import fsnative, expanduser
11
12from quodlibet import config
13from quodlibet.util.library import split_scan_dirs, set_scan_dirs, \
14    get_exclude_dirs, get_scan_dirs
15from quodlibet.util import is_windows
16from quodlibet.util.path import get_home_dir, unexpand
17
18from tests import TestCase
19
20
21STANDARD_PATH = fsnative(u"/home/user/Music")
22OTHER_PATH = fsnative(u"/opt/party")
23GVFS_PATH = fsnative(u"/run/user/12345/gvfs/smb-share"
24                      ":server=12.23.34.45,share=/foo/bar/baz/path")
25GVFS_PATH_ESCAPED = fsnative(u"/run/user/12345/gvfs/smb-share"
26                              "\\:server=12.23.34.45,share=/foo/bar/baz/path")
27
28
29class Tlibrary_utils(TestCase):
30
31    def test_basic(self):
32        if is_windows():
33            res = split_scan_dirs(u":Z:\\foo:C:/windows:")
34            self.assertEquals(res, [u"Z:\\foo", u"C:/windows"])
35        else:
36            res = split_scan_dirs(":%s:%s:" % (STANDARD_PATH, OTHER_PATH))
37            self.assertEquals(res, [STANDARD_PATH, OTHER_PATH])
38
39    def test_colon_paths(self):
40        if not is_windows():
41            res = split_scan_dirs(
42                ":%s:%s" % (STANDARD_PATH, GVFS_PATH_ESCAPED))
43            self.assertEquals(res, [STANDARD_PATH, GVFS_PATH])
44
45    def test_get_exclude_dirs(self):
46        some_path = os.path.join(unexpand(get_home_dir()), "foo")
47        config.set('library', 'exclude', some_path)
48        assert expanduser(some_path) in get_exclude_dirs()
49
50        assert all([isinstance(p, fsnative) for p in get_exclude_dirs()])
51
52    def test_get_scan_dirs(self):
53        some_path = os.path.join(unexpand(get_home_dir()), "foo")
54        config.set('settings', 'scan', some_path)
55        assert expanduser(some_path) in get_scan_dirs()
56
57        assert all([isinstance(p, fsnative) for p in get_scan_dirs()])
58
59
60class Tset_scan_dirs(TestCase):
61
62    @property
63    def scan_dirs(self):
64        return config.get('settings', 'scan')
65
66    def test_set_scan_dirs_empty(self):
67        set_scan_dirs([])
68        self.assertEqual(self.scan_dirs, "")
69
70    def test_set_scan_dirs_single(self):
71        set_scan_dirs([STANDARD_PATH])
72        self.assertEqual(self.scan_dirs, STANDARD_PATH)
73
74    def test_set_scan_dirs_multiple(self):
75        set_scan_dirs([OTHER_PATH, STANDARD_PATH])
76        self.assertEqual(self.scan_dirs,
77                         "%s:%s" % (OTHER_PATH, STANDARD_PATH))
78
79    def test_set_scan_dirs_colons(self):
80        set_scan_dirs([STANDARD_PATH, GVFS_PATH])
81        expected = GVFS_PATH if is_windows() else GVFS_PATH_ESCAPED
82        self.assertEqual(self.scan_dirs, "%s:%s" % (STANDARD_PATH, expected))
83