Commit | Line | Data |
---|---|---|
c5dbcd79 RK |
1 | #-*-python-*- |
2 | ||
3 | """Utility module used by tests""" | |
4 | ||
eee9d4b3 | 5 | import os,os.path,subprocess,sys,disorder |
c5dbcd79 RK |
6 | |
7 | def copyfile(a,b): | |
8 | """copyfile(A, B) | |
9 | Copy A to B.""" | |
10 | open(b,"w").write(open(a).read()) | |
11 | ||
12 | def maketrack(s): | |
13 | """maketrack(S) | |
14 | ||
15 | Make track with relative path S exist""" | |
f31e414d | 16 | trackpath = "%s/tracks/%s" % (testroot, s) |
c5dbcd79 RK |
17 | trackdir = os.path.dirname(trackpath) |
18 | if not os.path.exists(trackdir): | |
19 | os.makedirs(trackdir) | |
20 | copyfile("%s/sounds/slap.ogg" % topsrcdir, trackpath) | |
21 | ||
22 | def stdtracks(): | |
f9635e06 RK |
23 | # We create some tracks with non-ASCII characters in the name and |
24 | # we (currently) force UTF-8. | |
25 | # | |
26 | # On a traditional UNIX filesystem, that treats filenames as byte strings | |
27 | # with special significant for '/', this should just work, though the | |
28 | # names will look wrong to ls(1) in a non UTF-8 locale. | |
29 | # | |
30 | # On Apple HFS+ filenames normalized to a decomposed form that isn't quite | |
31 | # NFD, so our attempts to have both normalized and denormalized filenames | |
32 | # is frustrated. Provided we test on traditional filesytsems too this | |
33 | # shouldn't be a problem. | |
34 | # (See http://developer.apple.com/qa/qa2001/qa1173.html) | |
35 | ||
36 | # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE | |
37 | # (in NFC) | |
38 | maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg") | |
39 | ||
c5dbcd79 | 40 | maketrack("Joe Bloggs/First Album/02:Second track.ogg") |
f9635e06 RK |
41 | |
42 | # CC 81 = 0301 COMBINING ACUTE ACCENT | |
43 | # (giving an NFD i-acute) | |
44 | maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg") | |
45 | # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE | |
c5dbcd79 RK |
46 | maketrack("Joe Bloggs/First Album/04:Fourth track.ogg") |
47 | maketrack("Joe Bloggs/First Album/05:Fifth track.ogg") | |
48 | maketrack("Joe Bloggs/First Album/05:Fifth track.ogg") | |
49 | maketrack("Joe Bloggs/Second Album/01:First track.ogg") | |
50 | maketrack("Joe Bloggs/Second Album/02:Second track.ogg") | |
51 | maketrack("Joe Bloggs/Second Album/03:Third track.ogg") | |
52 | maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg") | |
53 | maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg") | |
54 | maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg") | |
f9635e06 RK |
55 | maketrack("Joe Bloggs/Third Album/01:First track.ogg") |
56 | maketrack("Joe Bloggs/Third Album/02:Second track.ogg") | |
57 | maketrack("Joe Bloggs/Third Album/03:Third track.ogg") | |
58 | maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg") | |
59 | maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg") | |
c5dbcd79 RK |
60 | maketrack("Fred Smith/Boring/01:Dull.ogg") |
61 | maketrack("Fred Smith/Boring/02:Tedious.ogg") | |
62 | maketrack("Fred Smith/Boring/03:Drum Solo.ogg") | |
63 | maketrack("Fred Smith/Boring/04:Yawn.ogg") | |
64 | maketrack("misc/blahblahblah.ogg") | |
65 | maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg") | |
66 | maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg") | |
67 | ||
68 | def notracks(): | |
69 | pass | |
70 | ||
f9635e06 RK |
71 | def common_setup(): |
72 | remove_dir(testroot) | |
73 | os.mkdir(testroot) | |
74 | open("%s/config" % testroot, "w").write( | |
75 | """player *.ogg shell 'echo "$TRACK" >> %s/played.log' | |
b6995afb RK |
76 | home %s |
77 | collection fs UTF-8 %s/tracks | |
78 | scratch %s/scratch.ogg | |
79 | gap 0 | |
80 | stopword 01 02 03 04 05 06 07 08 09 10 | |
81 | stopword 1 2 3 4 5 6 7 8 9 | |
82 | stopword 11 12 13 14 15 16 17 18 19 20 | |
83 | stopword 21 22 23 24 25 26 27 28 29 30 | |
84 | stopword the a an and to too in on of we i am as im for is | |
85 | username fred | |
86 | password fredpass | |
87 | allow fred fredpass | |
88 | plugins ../plugins | |
89 | player *.mp3 execraw disorder-decode | |
90 | player *.ogg execraw disorder-decode | |
91 | player *.wav execraw disorder-decode | |
92 | player *.flac execraw disorder-decode | |
93 | tracklength *.mp3 disorder-tracklength | |
94 | tracklength *.ogg disorder-tracklength | |
95 | tracklength *.wav disorder-tracklength | |
96 | tracklength *.flac disorder-tracklength | |
97 | """ % (testroot, testroot, testroot, testroot)) | |
f9635e06 RK |
98 | copyfile("%s/sounds/scratch.ogg" % topsrcdir, |
99 | "%s/scratch.ogg" % testroot) | |
100 | ||
101 | def start_daemon(test): | |
102 | """start_daemon(TEST) | |
c5dbcd79 RK |
103 | Start the daemon for test called TEST.""" |
104 | global daemon | |
105 | assert daemon == None | |
106 | if test == None: | |
107 | errs = sys.stderr | |
108 | else: | |
f9635e06 | 109 | errs = open("%s.log" % test, "w") |
c5dbcd79 RK |
110 | server = None |
111 | print " starting daemon" | |
112 | daemon = subprocess.Popen(["disorderd", | |
113 | "--foreground", | |
114 | "--config", "%s/config" % testroot], | |
115 | stderr=errs) | |
eee9d4b3 RK |
116 | disorder._configfile = "%s/config" % testroot |
117 | disorder._userconf = False | |
c5dbcd79 | 118 | |
f9635e06 RK |
119 | def stop_daemon(): |
120 | """stop_daemon() | |
c5dbcd79 RK |
121 | |
122 | Stop the daemon if it has not stopped already""" | |
123 | global daemon | |
124 | rc = daemon.poll() | |
125 | if rc == None: | |
eee9d4b3 | 126 | print " stopping daemon" |
c5dbcd79 RK |
127 | os.kill(daemon.pid, 15) |
128 | rc = daemon.wait() | |
129 | print " daemon has stopped" | |
130 | daemon = None | |
131 | ||
132 | def run(test, setup=None, report=True, name=None): | |
133 | global tests | |
134 | tests += 1 | |
135 | if setup == None: | |
136 | setup = stdtracks | |
f9635e06 | 137 | common_setup() |
c5dbcd79 | 138 | setup() |
f9635e06 | 139 | start_daemon(name) |
c5dbcd79 | 140 | try: |
eee9d4b3 RK |
141 | try: |
142 | test() | |
143 | except AssertionError, e: | |
144 | global failures | |
145 | failures += 1 | |
146 | print e | |
147 | finally: | |
f9635e06 | 148 | stop_daemon() |
c5dbcd79 RK |
149 | if report: |
150 | if failures: | |
151 | print " FAILED" | |
152 | sys.exit(1) | |
153 | else: | |
154 | print " OK" | |
155 | ||
156 | def remove_dir(d): | |
157 | """remove_dir(D) | |
158 | ||
159 | Recursively delete directory D""" | |
160 | if os.path.lexists(d): | |
161 | if os.path.isdir(d): | |
162 | for dd in os.listdir(d): | |
163 | remove_dir("%s/%s" % (d, dd)) | |
164 | os.rmdir(d) | |
165 | else: | |
166 | os.remove(d) | |
167 | ||
168 | # ----------------------------------------------------------------------------- | |
169 | # Common setup | |
170 | ||
171 | tests = 0 | |
172 | failures = 0 | |
173 | daemon = None | |
174 | testroot = "%s/testroot" % os.getcwd() | |
175 | topsrcdir = os.path.abspath(os.getenv("topsrcdir")) |