chiark / gitweb /
borrow some test naming conventions from disorder.unicode
[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
3bfecfac
RK
24import os,os.path,subprocess,sys,re
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"""
f31e414d 73 trackpath = "%s/tracks/%s" % (testroot, 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)
c5dbcd79
RK
78
79def stdtracks():
80 maketrack("Joe Bloggs/First Album/01:First track.ogg")
81 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
82 maketrack("Joe Bloggs/First Album/03:Third track.ogg")
83 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
84 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
85 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
86 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
87 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
88 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
89 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
90 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
91 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
92 maketrack("Joe Bloggs/First Album/01:First track.ogg")
93 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
94 maketrack("Joe Bloggs/First Album/03:Third track.ogg")
95 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
96 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
97 maketrack("Fred Smith/Boring/01:Dull.ogg")
98 maketrack("Fred Smith/Boring/02:Tedious.ogg")
99 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
100 maketrack("Fred Smith/Boring/04:Yawn.ogg")
101 maketrack("misc/blahblahblah.ogg")
102 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
103 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
104
105def notracks():
106 pass
107
7ebd22d9
RK
108def start_daemon():
109 """start_daemon()
c5dbcd79 110
5c07ba71 111Start the daemon."""
c5dbcd79
RK
112 global daemon
113 assert daemon == None
c5dbcd79
RK
114 print " starting daemon"
115 daemon = subprocess.Popen(["disorderd",
116 "--foreground",
117 "--config", "%s/config" % testroot],
118 stderr=errs)
eee9d4b3
RK
119 disorder._configfile = "%s/config" % testroot
120 disorder._userconf = False
c5dbcd79 121
7ebd22d9
RK
122def stop_daemon():
123 """stop_daemon()
c5dbcd79
RK
124
125Stop the daemon if it has not stopped already"""
126 global daemon
5c07ba71
RK
127 if daemon == None:
128 return
c5dbcd79
RK
129 rc = daemon.poll()
130 if rc == None:
eee9d4b3 131 print " stopping daemon"
c5dbcd79
RK
132 os.kill(daemon.pid, 15)
133 rc = daemon.wait()
134 print " daemon has stopped"
135 daemon = None
136
5c07ba71
RK
137def run(module=None, report=True):
138 """dtest.run(MODULE)
139
140 Run the test in MODULE. This can be a string (in which case the module
141 will be imported) or a module object."""
c5dbcd79
RK
142 global tests
143 tests += 1
5c07ba71
RK
144 if module is None:
145 # We're running a test stand-alone
146 import __main__
147 module = __main__
148 name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
149 else:
150 # We've been passed a module or a module name
151 if type(module) == str:
152 module = __import__(module)
153 name = module.__name__
154 global errs
155 errs = open("%s.log" % name, "w")
156 setup = stdtracks
c5dbcd79 157 setup()
c5dbcd79 158 try:
eee9d4b3 159 try:
5c07ba71 160 module.test()
eee9d4b3
RK
161 except AssertionError, e:
162 global failures
163 failures += 1
164 print e
165 finally:
7ebd22d9 166 stop_daemon()
c5dbcd79
RK
167 if report:
168 if failures:
169 print " FAILED"
170 sys.exit(1)
171 else:
172 print " OK"
173
174def remove_dir(d):
175 """remove_dir(D)
176
177Recursively delete directory D"""
178 if os.path.lexists(d):
179 if os.path.isdir(d):
180 for dd in os.listdir(d):
181 remove_dir("%s/%s" % (d, dd))
182 os.rmdir(d)
183 else:
184 os.remove(d)
185
186# -----------------------------------------------------------------------------
187# Common setup
188
189tests = 0
190failures = 0
191daemon = None
3bfecfac 192testroot = "%s/tests/testroot" % top_builddir
c5dbcd79
RK
193remove_dir(testroot)
194os.mkdir(testroot)
902b7f9d
RK
195open("%s/config" % testroot, "w").write(
196"""player *.ogg shell 'echo "$TRACK" >> %s/played.log'
c5dbcd79
RK
197home %s
198collection fs ASCII %s/tracks
199scratch %s/scratch.ogg
200gap 0
201stopword 01 02 03 04 05 06 07 08 09 10
202stopword 1 2 3 4 5 6 7 8 9
203stopword 11 12 13 14 15 16 17 18 19 20
204stopword 21 22 23 24 25 26 27 28 29 30
205stopword the a an and to too in on of we i am as im for is
eee9d4b3
RK
206username fred
207password fredpass
208allow fred fredpass
ad08c366 209plugins %s/plugins
be3ebd90
RK
210player *.mp3 execraw disorder-decode
211player *.ogg execraw disorder-decode
212player *.wav execraw disorder-decode
213player *.flac execraw disorder-decode
214tracklength *.mp3 disorder-tracklength
215tracklength *.ogg disorder-tracklength
216tracklength *.wav disorder-tracklength
217tracklength *.flac disorder-tracklength
ad08c366 218""" % (testroot, testroot, testroot, testroot, top_builddir))
3bfecfac 219copyfile("%s/sounds/scratch.ogg" % top_srcdir,
c5dbcd79 220 "%s/scratch.ogg" % testroot)