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