chiark / gitweb /
signame.c test coverage -> 100%
[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
213b4064 24import os,os.path,subprocess,sys,re,time,unicodedata,random
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
de5ccb1a
RK
69def to_unicode(s):
70 """Convert UTF-8 to unicode. A no-op if already unicode."""
71 if type(s) == unicode:
72 return s
73 else:
74 return unicode(s, "UTF-8")
75
76def nfc(s):
77 """Convert UTF-8 string or unicode to NFC unicode."""
78 return unicodedata.normalize("NFC", to_unicode(s))
79
c5dbcd79
RK
80def maketrack(s):
81 """maketrack(S)
82
83Make track with relative path S exist"""
121e3654 84 trackpath = "%s/%s" % (tracks, s)
c5dbcd79
RK
85 trackdir = os.path.dirname(trackpath)
86 if not os.path.exists(trackdir):
87 os.makedirs(trackdir)
3bfecfac 88 copyfile("%s/sounds/slap.ogg" % top_srcdir, trackpath)
121e3654 89 # We record the tracks we created so they can be tested against
7bbe944b
RK
90 # server responses. We put them into NFC since that's what the server
91 # uses internally.
de5ccb1a 92 bits = nfc(s).split('/')
121e3654
RK
93 dp = tracks
94 for d in bits [0:-1]:
95 dd = "%s/%s" % (dp, d)
96 if dp not in dirs_by_dir:
97 dirs_by_dir[dp] = []
98 if dd not in dirs_by_dir[dp]:
99 dirs_by_dir[dp].append(dd)
100 dp = "%s/%s" % (dp, d)
101 if dp not in files_by_dir:
102 files_by_dir[dp] = []
103 files_by_dir[dp].append("%s/%s" % (dp, bits[-1]))
c5dbcd79
RK
104
105def stdtracks():
f9635e06
RK
106 # We create some tracks with non-ASCII characters in the name and
107 # we (currently) force UTF-8.
108 #
109 # On a traditional UNIX filesystem, that treats filenames as byte strings
110 # with special significant for '/', this should just work, though the
111 # names will look wrong to ls(1) in a non UTF-8 locale.
112 #
113 # On Apple HFS+ filenames normalized to a decomposed form that isn't quite
114 # NFD, so our attempts to have both normalized and denormalized filenames
115 # is frustrated. Provided we test on traditional filesytsems too this
116 # shouldn't be a problem.
117 # (See http://developer.apple.com/qa/qa2001/qa1173.html)
121e3654
RK
118
119 global dirs_by_dir, files_by_dir
120 dirs_by_dir={}
121 files_by_dir={}
f9635e06
RK
122
123 # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE
124 # (in NFC)
125 maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg")
126
c5dbcd79 127 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
f9635e06
RK
128
129 # CC 81 = 0301 COMBINING ACUTE ACCENT
130 # (giving an NFD i-acute)
131 maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg")
132 # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE
c5dbcd79
RK
133 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
134 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
c5dbcd79
RK
135 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
136 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
137 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
138 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
139 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
de5ccb1a
RK
140 maketrack("Joe Bloggs/Third Album/01:First_track.ogg")
141 maketrack("Joe Bloggs/Third Album/02:Second_track.ogg")
142 maketrack("Joe Bloggs/Third Album/03:Third_track.ogg")
143 maketrack("Joe Bloggs/Third Album/04:Fourth_track.ogg")
144 maketrack("Joe Bloggs/Third Album/05:Fifth_track.ogg")
c5dbcd79
RK
145 maketrack("Fred Smith/Boring/01:Dull.ogg")
146 maketrack("Fred Smith/Boring/02:Tedious.ogg")
147 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
148 maketrack("Fred Smith/Boring/04:Yawn.ogg")
149 maketrack("misc/blahblahblah.ogg")
150 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
151 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
0db97c0d 152
f9635e06
RK
153def common_setup():
154 remove_dir(testroot)
155 os.mkdir(testroot)
213b4064
RK
156 global port
157 port = random.randint(49152, 65535)
f9635e06 158 open("%s/config" % testroot, "w").write(
213b4064 159 """home %s
b6995afb
RK
160collection fs UTF-8 %s/tracks
161scratch %s/scratch.ogg
162gap 0
163stopword 01 02 03 04 05 06 07 08 09 10
164stopword 1 2 3 4 5 6 7 8 9
165stopword 11 12 13 14 15 16 17 18 19 20
166stopword 21 22 23 24 25 26 27 28 29 30
167stopword the a an and to too in on of we i am as im for is
168username fred
169password fredpass
170allow fred fredpass
40c30921 171plugins
deaaa115 172plugins %s/plugins
40c30921 173plugins %s/plugins/.libs
b6995afb
RK
174player *.mp3 execraw disorder-decode
175player *.ogg execraw disorder-decode
176player *.wav execraw disorder-decode
177player *.flac execraw disorder-decode
178tracklength *.mp3 disorder-tracklength
179tracklength *.ogg disorder-tracklength
180tracklength *.wav disorder-tracklength
181tracklength *.flac disorder-tracklength
213b4064
RK
182speaker_backend network
183broadcast 127.0.0.1 %d
184broadcast_from 127.0.0.1 %d
185""" % (testroot, testroot, testroot, top_builddir, top_builddir,
186 port, port + 1))
fbcfb257 187 copyfile("%s/sounds/scratch.ogg" % top_srcdir,
f9635e06
RK
188 "%s/scratch.ogg" % testroot)
189
1c8f3db8
RK
190def start_daemon():
191 """start_daemon()
c5dbcd79 192
1c8f3db8 193Start the daemon."""
fc9c24e3 194 global daemon, errs
213b4064 195 assert daemon == None, "no daemon running"
c5dbcd79 196 print " starting daemon"
1a4a6350
RK
197 # remove the socket if it exists
198 socket = "%s/socket" % testroot
199 try:
200 os.remove(socket)
201 except:
202 pass
c5dbcd79
RK
203 daemon = subprocess.Popen(["disorderd",
204 "--foreground",
205 "--config", "%s/config" % testroot],
206 stderr=errs)
1a4a6350
RK
207 # Wait for the socket to be created
208 waited = 0
209 while not os.path.exists(socket):
210 rc = daemon.poll()
211 if rc is not None:
212 print "FATAL: daemon failed to start up"
213 sys.exit(1)
214 waited += 1
215 if waited == 1:
216 print " waiting for socket..."
217 elif waited >= 60:
218 print "FATAL: took too long for socket to appear"
219 sys.exit(1)
220 time.sleep(1)
221 if waited > 0:
222 print " took about %ds for socket to appear" % waited
c5dbcd79 223
f9635e06
RK
224def stop_daemon():
225 """stop_daemon()
c5dbcd79
RK
226
227Stop the daemon if it has not stopped already"""
228 global daemon
5c07ba71
RK
229 if daemon == None:
230 return
c5dbcd79
RK
231 rc = daemon.poll()
232 if rc == None:
eee9d4b3 233 print " stopping daemon"
c5dbcd79 234 os.kill(daemon.pid, 15)
1a4a6350 235 print " waiting for daemon"
c5dbcd79 236 rc = daemon.wait()
1a4a6350
RK
237 print " daemon has stopped"
238 else:
239 print " daemon already stopped"
c5dbcd79
RK
240 daemon = None
241
5c07ba71
RK
242def run(module=None, report=True):
243 """dtest.run(MODULE)
244
245 Run the test in MODULE. This can be a string (in which case the module
246 will be imported) or a module object."""
c5dbcd79
RK
247 global tests
248 tests += 1
deaaa115 249 # Locate the test module
5c07ba71
RK
250 if module is None:
251 # We're running a test stand-alone
252 import __main__
253 module = __main__
254 name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
255 else:
256 # We've been passed a module or a module name
257 if type(module) == str:
258 module = __import__(module)
259 name = module.__name__
deaaa115 260 # Open the error log
5c07ba71
RK
261 global errs
262 errs = open("%s.log" % name, "w")
deaaa115 263 # Ensure that disorder.py uses the test installation
1c8f3db8
RK
264 disorder._configfile = "%s/config" % testroot
265 disorder._userconf = False
3514766b 266 # Make config file etc
f9635e06 267 common_setup()
deaaa115
RK
268 # Create some standard tracks
269 stdtracks()
c5dbcd79 270 try:
eee9d4b3 271 try:
5c07ba71 272 module.test()
eee9d4b3
RK
273 except AssertionError, e:
274 global failures
275 failures += 1
213b4064 276 print "assertion failed: %s" % e.message
eee9d4b3 277 finally:
7ebd22d9 278 stop_daemon()
c5dbcd79
RK
279 if report:
280 if failures:
281 print " FAILED"
282 sys.exit(1)
283 else:
284 print " OK"
285
286def remove_dir(d):
287 """remove_dir(D)
288
289Recursively delete directory D"""
290 if os.path.lexists(d):
291 if os.path.isdir(d):
292 for dd in os.listdir(d):
293 remove_dir("%s/%s" % (d, dd))
294 os.rmdir(d)
295 else:
296 os.remove(d)
297
a4d8ba8f
RK
298def check_files():
299 c = disorder.client()
300 failures = 0
301 for d in dirs_by_dir:
302 xdirs = dirs_by_dir[d]
303 dirs = c.directories(d)
304 xdirs.sort()
305 dirs.sort()
306 if dirs != xdirs:
307 print
308 print "directory: %s" % d
309 print "expected: %s" % xdirs
310 print "got: %s" % dirs
311 failures += 1
312 for d in files_by_dir:
313 xfiles = files_by_dir[d]
314 files = c.files(d)
315 xfiles.sort()
316 files.sort()
317 if files != xfiles:
318 print
319 print "directory: %s" % d
320 print "expected: %s" % xfiles
321 print "got: %s" % files
322 failures += 1
323 return failures
324
c5dbcd79
RK
325# -----------------------------------------------------------------------------
326# Common setup
327
328tests = 0
329failures = 0
330daemon = None
3bfecfac 331testroot = "%s/tests/testroot" % top_builddir
121e3654 332tracks = "%s/tracks" % testroot