chiark / gitweb /
need python >= 2.4 for tests
[disorder] / tests / dtest.py
CommitLineData
c5dbcd79
RK
1#-*-python-*-
2
3"""Utility module used by tests"""
4
eee9d4b3 5import os,os.path,subprocess,sys,disorder
c5dbcd79
RK
6
7def copyfile(a,b):
8 """copyfile(A, B)
9Copy A to B."""
10 open(b,"w").write(open(a).read())
11
12def maketrack(s):
13 """maketrack(S)
14
15Make 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
22def stdtracks():
23 maketrack("Joe Bloggs/First Album/01:First track.ogg")
24 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
25 maketrack("Joe Bloggs/First Album/03:Third track.ogg")
26 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
27 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
28 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
29 maketrack("Joe Bloggs/Second Album/01:First track.ogg")
30 maketrack("Joe Bloggs/Second Album/02:Second track.ogg")
31 maketrack("Joe Bloggs/Second Album/03:Third track.ogg")
32 maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg")
33 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
34 maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg")
35 maketrack("Joe Bloggs/First Album/01:First track.ogg")
36 maketrack("Joe Bloggs/First Album/02:Second track.ogg")
37 maketrack("Joe Bloggs/First Album/03:Third track.ogg")
38 maketrack("Joe Bloggs/First Album/04:Fourth track.ogg")
39 maketrack("Joe Bloggs/First Album/05:Fifth track.ogg")
40 maketrack("Fred Smith/Boring/01:Dull.ogg")
41 maketrack("Fred Smith/Boring/02:Tedious.ogg")
42 maketrack("Fred Smith/Boring/03:Drum Solo.ogg")
43 maketrack("Fred Smith/Boring/04:Yawn.ogg")
44 maketrack("misc/blahblahblah.ogg")
45 maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
46 maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
47
48def notracks():
49 pass
50
51def start(test):
52 """start(TEST)
53
54Start the daemon for test called TEST."""
55 global daemon
56 assert daemon == None
57 if test == None:
58 errs = sys.stderr
59 else:
60 errs = open("%s/%s.log" % (testroot, test), "w")
61 server = None
62 print " starting daemon"
63 daemon = subprocess.Popen(["disorderd",
64 "--foreground",
65 "--config", "%s/config" % testroot],
66 stderr=errs)
eee9d4b3
RK
67 disorder._configfile = "%s/config" % testroot
68 disorder._userconf = False
c5dbcd79
RK
69
70def stop():
71 """stop()
72
73Stop the daemon if it has not stopped already"""
74 global daemon
75 rc = daemon.poll()
76 if rc == None:
eee9d4b3 77 print " stopping daemon"
c5dbcd79
RK
78 os.kill(daemon.pid, 15)
79 rc = daemon.wait()
80 print " daemon has stopped"
81 daemon = None
82
83def run(test, setup=None, report=True, name=None):
84 global tests
85 tests += 1
86 if setup == None:
87 setup = stdtracks
88 setup()
89 start(name)
90 try:
eee9d4b3
RK
91 try:
92 test()
93 except AssertionError, e:
94 global failures
95 failures += 1
96 print e
97 finally:
98 stop()
c5dbcd79
RK
99 if report:
100 if failures:
101 print " FAILED"
102 sys.exit(1)
103 else:
104 print " OK"
105
106def remove_dir(d):
107 """remove_dir(D)
108
109Recursively delete directory D"""
110 if os.path.lexists(d):
111 if os.path.isdir(d):
112 for dd in os.listdir(d):
113 remove_dir("%s/%s" % (d, dd))
114 os.rmdir(d)
115 else:
116 os.remove(d)
117
118# -----------------------------------------------------------------------------
119# Common setup
120
121tests = 0
122failures = 0
123daemon = None
124testroot = "%s/testroot" % os.getcwd()
125topsrcdir = os.path.abspath(os.getenv("topsrcdir"))
126remove_dir(testroot)
127os.mkdir(testroot)
902b7f9d
RK
128open("%s/config" % testroot, "w").write(
129"""player *.ogg shell 'echo "$TRACK" >> %s/played.log'
c5dbcd79
RK
130home %s
131collection fs ASCII %s/tracks
132scratch %s/scratch.ogg
133gap 0
134stopword 01 02 03 04 05 06 07 08 09 10
135stopword 1 2 3 4 5 6 7 8 9
136stopword 11 12 13 14 15 16 17 18 19 20
137stopword 21 22 23 24 25 26 27 28 29 30
138stopword the a an and to too in on of we i am as im for is
eee9d4b3
RK
139username fred
140password fredpass
141allow fred fredpass
be3ebd90
RK
142plugins ../plugins
143player *.mp3 execraw disorder-decode
144player *.ogg execraw disorder-decode
145player *.wav execraw disorder-decode
146player *.flac execraw disorder-decode
147tracklength *.mp3 disorder-tracklength
148tracklength *.ogg disorder-tracklength
149tracklength *.wav disorder-tracklength
150tracklength *.flac disorder-tracklength
c5dbcd79
RK
151""" % (testroot, testroot, testroot, testroot))
152copyfile("%s/sounds/scratch.ogg" % topsrcdir,
153 "%s/scratch.ogg" % testroot)