chiark / gitweb /
sync with disorder.dev
[disorder] / tests / dtest.py
CommitLineData
c5dbcd79 1#-*-python-*-
aa896435
RK
2#
3# This file is part of DisOrder.
4# Copyright (C) 2007 Richard Kettlewell
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19# USA
20#
c5dbcd79
RK
21
22"""Utility module used by tests"""
23
fbcfb257 24import os,os.path,subprocess,sys,re,unicodedata
3bfecfac
RK
25
26def fatal(s):
27 """Write an error message and exit"""
28 sys.stderr.write("ERROR: %s\n" % s)
29 sys.exit(1)
30
31# Identify the top build directory
32cwd = os.getcwd()
33if os.path.exists("config.h"):
34 top_builddir = cwd
35elif os.path.exists("alltests"):
36 top_builddir = os.path.dirname(cwd)
37else:
38 fatal("cannot identify build directory")
39
40# Make sure the Python build directory is on the module search path
41sys.path.insert(0, os.path.join(top_builddir, "python"))
42import disorder
43
44# Make sure the server build directory is on the executable search path
45ospath = os.environ["PATH"].split(os.pathsep)
46ospath.insert(0, os.path.join(top_builddir, "server"))
47os.environ["PATH"] = os.pathsep.join(ospath)
48
49# Parse the makefile in the current directory to identify the source directory
50top_srcdir = None
51for l in file("Makefile"):
52 r = re.match("top_srcdir *= *(.*)", l)
53 if r:
54 top_srcdir = r.group(1)
55 break
56if not top_srcdir:
57 fatal("cannot identify source directory")
58
59# The tests source directory must be on the module search path already since
60# we found dtest.py
61
62# -----------------------------------------------------------------------------
c5dbcd79
RK
63
64def copyfile(a,b):
65 """copyfile(A, B)
66Copy A to B."""
67 open(b,"w").write(open(a).read())
68
69def maketrack(s):
70 """maketrack(S)
71
72Make track with relative path S exist"""
121e3654 73 trackpath = "%s/%s" % (tracks, s)
c5dbcd79
RK
74 trackdir = os.path.dirname(trackpath)
75 if not os.path.exists(trackdir):
76 os.makedirs(trackdir)
3bfecfac 77 copyfile("%s/sounds/slap.ogg" % top_srcdir, trackpath)
121e3654 78 # We record the tracks we created so they can be tested against
7bbe944b
RK
79 # server responses. We put them into NFC since that's what the server
80 # uses internally.
81 bits = unicodedata.normalize("NFC",
82 unicode(s, "UTF-8")).split('/')
121e3654
RK
83 dp = tracks
84 for d in bits [0:-1]:
85 dd = "%s/%s" % (dp, d)
86 if dp not in dirs_by_dir:
87 dirs_by_dir[dp] = []
88 if dd not in dirs_by_dir[dp]:
89 dirs_by_dir[dp].append(dd)
90 dp = "%s/%s" % (dp, d)
91 if dp not in files_by_dir:
92 files_by_dir[dp] = []
93 files_by_dir[dp].append("%s/%s" % (dp, bits[-1]))
c5dbcd79
RK
94
95def stdtracks():
f9635e06
RK
96 # We create some tracks with non-ASCII characters in the name and
97 # we (currently) force UTF-8.
98 #
99 # On a traditional UNIX filesystem, that treats filenames as byte strings
100 # with special significant for '/', this should just work, though the
101 # names will look wrong to ls(1) in a non UTF-8 locale.
102 #
103 # On Apple HFS+ filenames normalized to a decomposed form that isn't quite
104 # NFD, so our attempts to have both normalized and denormalized filenames
105 # is frustrated. Provided we test on traditional filesytsems too this
106 # shouldn't be a problem.
107 # (See http://developer.apple.com/qa/qa2001/qa1173.html)
121e3654
RK
108
109 global dirs_by_dir, files_by_dir
110 dirs_by_dir={}
111 files_by_dir={}
f9635e06
RK
112
113 # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE
114 # (in NFC)
115 maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg")
116
c5dbcd79 117 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
f9635e06
RK
118
119 # CC 81 = 0301 COMBINING ACUTE ACCENT
120 # (giving an NFD i-acute)
121 maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg")
122 # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE
c5dbcd79
RK
123 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
124 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
c5dbcd79
RK
125 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
126 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
127 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
128 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
129 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
f9635e06
RK
130 maketrack("Joe Bloggs/Third Album/01:First track.ogg")
131 maketrack("Joe Bloggs/Third Album/02:Second track.ogg")
132 maketrack("Joe Bloggs/Third Album/03:Third track.ogg")
133 maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg")
134 maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg")
c5dbcd79
RK
135 maketrack("Fred Smith/Boring/01:Dull.ogg")
136 maketrack("Fred Smith/Boring/02:Tedious.ogg")
137 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
138 maketrack("Fred Smith/Boring/04:Yawn.ogg")
139 maketrack("misc/blahblahblah.ogg")
140 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
141 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
142
143def notracks():
144 pass
145
f9635e06
RK
146def common_setup():
147 remove_dir(testroot)
148 os.mkdir(testroot)
149 open("%s/config" % testroot, "w").write(
150 """player *.ogg shell 'echo "$TRACK" >> %s/played.log'
b6995afb
RK
151home %s
152collection fs UTF-8 %s/tracks
153scratch %s/scratch.ogg
154gap 0
155stopword 01 02 03 04 05 06 07 08 09 10
156stopword 1 2 3 4 5 6 7 8 9
157stopword 11 12 13 14 15 16 17 18 19 20
158stopword 21 22 23 24 25 26 27 28 29 30
159stopword the a an and to too in on of we i am as im for is
160username fred
161password fredpass
162allow fred fredpass
163plugins ../plugins
164player *.mp3 execraw disorder-decode
165player *.ogg execraw disorder-decode
166player *.wav execraw disorder-decode
167player *.flac execraw disorder-decode
168tracklength *.mp3 disorder-tracklength
169tracklength *.ogg disorder-tracklength
170tracklength *.wav disorder-tracklength
171tracklength *.flac disorder-tracklength
172""" % (testroot, testroot, testroot, testroot))
fbcfb257 173 copyfile("%s/sounds/scratch.ogg" % top_srcdir,
f9635e06
RK
174 "%s/scratch.ogg" % testroot)
175
1c8f3db8
RK
176def start_daemon():
177 """start_daemon()
178Start the daemon."""
179 global daemon,errs
180 assert daemon is None
c5dbcd79
RK
181 server = None
182 print " starting daemon"
183 daemon = subprocess.Popen(["disorderd",
184 "--foreground",
185 "--config", "%s/config" % testroot],
186 stderr=errs)
187
f9635e06
RK
188def stop_daemon():
189 """stop_daemon()
c5dbcd79
RK
190
191Stop the daemon if it has not stopped already"""
192 global daemon
1c8f3db8 193 assert daemon is not None
c5dbcd79
RK
194 rc = daemon.poll()
195 if rc == None:
eee9d4b3 196 print " stopping daemon"
c5dbcd79
RK
197 os.kill(daemon.pid, 15)
198 rc = daemon.wait()
199 print " daemon has stopped"
200 daemon = None
201
202def run(test, setup=None, report=True, name=None):
1c8f3db8 203 global tests,errs
c5dbcd79
RK
204 tests += 1
205 if setup == None:
206 setup = stdtracks
fbcfb257 207 errs = open("%s.log" % test.__name__, "w")
1c8f3db8
RK
208 disorder._configfile = "%s/config" % testroot
209 disorder._userconf = False
f9635e06 210 common_setup()
c5dbcd79 211 setup()
c5dbcd79 212 try:
eee9d4b3
RK
213 try:
214 test()
215 except AssertionError, e:
216 global failures
217 failures += 1
218 print e
219 finally:
1c8f3db8
RK
220 if daemon is not None:
221 stop_daemon()
c5dbcd79
RK
222 if report:
223 if failures:
224 print " FAILED"
225 sys.exit(1)
226 else:
227 print " OK"
228
229def remove_dir(d):
230 """remove_dir(D)
231
232Recursively delete directory D"""
233 if os.path.lexists(d):
234 if os.path.isdir(d):
235 for dd in os.listdir(d):
236 remove_dir("%s/%s" % (d, dd))
237 os.rmdir(d)
238 else:
239 os.remove(d)
240
241# -----------------------------------------------------------------------------
242# Common setup
243
244tests = 0
245failures = 0
246daemon = None
3bfecfac 247testroot = "%s/tests/testroot" % top_builddir
121e3654 248tracks = "%s/tracks" % testroot