Commit | Line | Data |
---|---|---|
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 |
24 | import os,os.path,subprocess,sys,re |
25 | ||
26 | def 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 | |
32 | cwd = os.getcwd() | |
33 | if os.path.exists("config.h"): | |
34 | top_builddir = cwd | |
35 | elif os.path.exists("alltests"): | |
36 | top_builddir = os.path.dirname(cwd) | |
37 | else: | |
38 | fatal("cannot identify build directory") | |
39 | ||
40 | # Make sure the Python build directory is on the module search path | |
41 | sys.path.insert(0, os.path.join(top_builddir, "python")) | |
42 | import disorder | |
43 | ||
44 | # Make sure the server build directory is on the executable search path | |
45 | ospath = os.environ["PATH"].split(os.pathsep) | |
46 | ospath.insert(0, os.path.join(top_builddir, "server")) | |
47 | os.environ["PATH"] = os.pathsep.join(ospath) | |
48 | ||
49 | # Parse the makefile in the current directory to identify the source directory | |
50 | top_srcdir = None | |
51 | for l in file("Makefile"): | |
52 | r = re.match("top_srcdir *= *(.*)", l) | |
53 | if r: | |
54 | top_srcdir = r.group(1) | |
55 | break | |
56 | if 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 | |
64 | def copyfile(a,b): | |
65 | """copyfile(A, B) | |
66 | Copy A to B.""" | |
67 | open(b,"w").write(open(a).read()) | |
68 | ||
69 | def maketrack(s): | |
70 | """maketrack(S) | |
71 | ||
72 | Make track with relative path S exist""" | |
fc9c24e3 | 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) |
c5dbcd79 RK |
78 | |
79 | def 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") | |
c5dbcd79 RK |
85 | maketrack("Joe Bloggs/Second Album/01:First track.ogg") |
86 | maketrack("Joe Bloggs/Second Album/02:Second track.ogg") | |
87 | maketrack("Joe Bloggs/Second Album/03:Third track.ogg") | |
88 | maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg") | |
89 | maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg") | |
fc9c24e3 RK |
90 | maketrack("Joe Bloggs/Third Album/01:First track.ogg") |
91 | maketrack("Joe Bloggs/Third Album/02:Second track.ogg") | |
92 | maketrack("Joe Bloggs/Third Album/03:Third track.ogg") | |
93 | maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg") | |
94 | maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg") | |
c5dbcd79 RK |
95 | maketrack("Fred Smith/Boring/01:Dull.ogg") |
96 | maketrack("Fred Smith/Boring/02:Tedious.ogg") | |
97 | maketrack("Fred Smith/Boring/03:Drum Solo.ogg") | |
98 | maketrack("Fred Smith/Boring/04:Yawn.ogg") | |
99 | maketrack("misc/blahblahblah.ogg") | |
100 | maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg") | |
101 | maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg") | |
102 | ||
deaaa115 RK |
103 | def common_setup(): |
104 | remove_dir(testroot) | |
105 | os.mkdir(testroot) | |
106 | open("%s/config" % testroot, "w").write( | |
107 | """player *.ogg shell 'echo "$TRACK" >> %s/played.log' | |
108 | home %s | |
109 | collection fs UTF-8 %s/tracks | |
110 | scratch %s/scratch.ogg | |
111 | gap 0 | |
112 | stopword 01 02 03 04 05 06 07 08 09 10 | |
113 | stopword 1 2 3 4 5 6 7 8 9 | |
114 | stopword 11 12 13 14 15 16 17 18 19 20 | |
115 | stopword 21 22 23 24 25 26 27 28 29 30 | |
116 | stopword the a an and to too in on of we i am as im for is | |
117 | username fred | |
118 | password fredpass | |
119 | allow fred fredpass | |
120 | plugins %s/plugins | |
121 | player *.mp3 execraw disorder-decode | |
122 | player *.ogg execraw disorder-decode | |
123 | player *.wav execraw disorder-decode | |
124 | player *.flac execraw disorder-decode | |
125 | tracklength *.mp3 disorder-tracklength | |
126 | tracklength *.ogg disorder-tracklength | |
127 | tracklength *.wav disorder-tracklength | |
128 | tracklength *.flac disorder-tracklength | |
129 | """ % (testroot, testroot, testroot, testroot, top_builddir)) | |
130 | copyfile("%s/sounds/scratch.ogg" % top_srcdir, | |
131 | "%s/scratch.ogg" % testroot) | |
c5dbcd79 | 132 | |
7ebd22d9 RK |
133 | def start_daemon(): |
134 | """start_daemon() | |
c5dbcd79 | 135 | |
5c07ba71 | 136 | Start the daemon.""" |
fc9c24e3 | 137 | global daemon, errs |
c5dbcd79 | 138 | assert daemon == None |
c5dbcd79 RK |
139 | print " starting daemon" |
140 | daemon = subprocess.Popen(["disorderd", | |
141 | "--foreground", | |
142 | "--config", "%s/config" % testroot], | |
143 | stderr=errs) | |
eee9d4b3 RK |
144 | disorder._configfile = "%s/config" % testroot |
145 | disorder._userconf = False | |
c5dbcd79 | 146 | |
7ebd22d9 RK |
147 | def stop_daemon(): |
148 | """stop_daemon() | |
c5dbcd79 RK |
149 | |
150 | Stop the daemon if it has not stopped already""" | |
151 | global daemon | |
5c07ba71 RK |
152 | if daemon == None: |
153 | return | |
c5dbcd79 RK |
154 | rc = daemon.poll() |
155 | if rc == None: | |
eee9d4b3 | 156 | print " stopping daemon" |
c5dbcd79 RK |
157 | os.kill(daemon.pid, 15) |
158 | rc = daemon.wait() | |
159 | print " daemon has stopped" | |
160 | daemon = None | |
161 | ||
5c07ba71 RK |
162 | def run(module=None, report=True): |
163 | """dtest.run(MODULE) | |
164 | ||
165 | Run the test in MODULE. This can be a string (in which case the module | |
166 | will be imported) or a module object.""" | |
c5dbcd79 RK |
167 | global tests |
168 | tests += 1 | |
deaaa115 | 169 | # Locate the test module |
5c07ba71 RK |
170 | if module is None: |
171 | # We're running a test stand-alone | |
172 | import __main__ | |
173 | module = __main__ | |
174 | name = os.path.splitext(os.path.basename(sys.argv[0]))[0] | |
175 | else: | |
176 | # We've been passed a module or a module name | |
177 | if type(module) == str: | |
178 | module = __import__(module) | |
179 | name = module.__name__ | |
deaaa115 | 180 | # Open the error log |
5c07ba71 RK |
181 | global errs |
182 | errs = open("%s.log" % name, "w") | |
deaaa115 RK |
183 | # Ensure that disorder.py uses the test installation |
184 | disorder._configfile = "%s/config" % testroot | |
185 | disorder._userconf = False | |
3514766b RK |
186 | # Make config file etc |
187 | common_setup() | |
deaaa115 RK |
188 | # Create some standard tracks |
189 | stdtracks() | |
c5dbcd79 | 190 | try: |
eee9d4b3 | 191 | try: |
5c07ba71 | 192 | module.test() |
eee9d4b3 RK |
193 | except AssertionError, e: |
194 | global failures | |
195 | failures += 1 | |
196 | print e | |
197 | finally: | |
7ebd22d9 | 198 | stop_daemon() |
c5dbcd79 RK |
199 | if report: |
200 | if failures: | |
201 | print " FAILED" | |
202 | sys.exit(1) | |
203 | else: | |
204 | print " OK" | |
205 | ||
206 | def remove_dir(d): | |
207 | """remove_dir(D) | |
208 | ||
209 | Recursively delete directory D""" | |
210 | if os.path.lexists(d): | |
211 | if os.path.isdir(d): | |
212 | for dd in os.listdir(d): | |
213 | remove_dir("%s/%s" % (d, dd)) | |
214 | os.rmdir(d) | |
215 | else: | |
216 | os.remove(d) | |
217 | ||
218 | # ----------------------------------------------------------------------------- | |
219 | # Common setup | |
220 | ||
221 | tests = 0 | |
222 | failures = 0 | |
223 | daemon = None | |
3bfecfac | 224 | testroot = "%s/tests/testroot" % top_builddir |
deaaa115 | 225 | tracks = "%s/tracks" % testroot |