| 1 | #-*-python-*- |
| 2 | |
| 3 | """Utility module used by tests""" |
| 4 | |
| 5 | import os,os.path,subprocess,sys,disorder,unicodedata |
| 6 | |
| 7 | def copyfile(a,b): |
| 8 | """copyfile(A, B) |
| 9 | Copy A to B.""" |
| 10 | open(b,"w").write(open(a).read()) |
| 11 | |
| 12 | def maketrack(s): |
| 13 | """maketrack(S) |
| 14 | |
| 15 | Make track with relative path S exist""" |
| 16 | trackpath = "%s/%s" % (tracks, s) |
| 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 | # We record the tracks we created so they can be tested against |
| 22 | # server responses. We put them into NFC since that's what the server |
| 23 | # uses internally. |
| 24 | bits = unicodedata.normalize("NFC", |
| 25 | unicode(s, "UTF-8")).split('/') |
| 26 | dp = tracks |
| 27 | for d in bits [0:-1]: |
| 28 | dd = "%s/%s" % (dp, d) |
| 29 | if dp not in dirs_by_dir: |
| 30 | dirs_by_dir[dp] = [] |
| 31 | if dd not in dirs_by_dir[dp]: |
| 32 | dirs_by_dir[dp].append(dd) |
| 33 | dp = "%s/%s" % (dp, d) |
| 34 | if dp not in files_by_dir: |
| 35 | files_by_dir[dp] = [] |
| 36 | files_by_dir[dp].append("%s/%s" % (dp, bits[-1])) |
| 37 | |
| 38 | def stdtracks(): |
| 39 | # We create some tracks with non-ASCII characters in the name and |
| 40 | # we (currently) force UTF-8. |
| 41 | # |
| 42 | # On a traditional UNIX filesystem, that treats filenames as byte strings |
| 43 | # with special significant for '/', this should just work, though the |
| 44 | # names will look wrong to ls(1) in a non UTF-8 locale. |
| 45 | # |
| 46 | # On Apple HFS+ filenames normalized to a decomposed form that isn't quite |
| 47 | # NFD, so our attempts to have both normalized and denormalized filenames |
| 48 | # is frustrated. Provided we test on traditional filesytsems too this |
| 49 | # shouldn't be a problem. |
| 50 | # (See http://developer.apple.com/qa/qa2001/qa1173.html) |
| 51 | |
| 52 | global dirs_by_dir, files_by_dir |
| 53 | dirs_by_dir={} |
| 54 | files_by_dir={} |
| 55 | |
| 56 | # C3 8C = 00CC LATIN CAPITAL LETTER I WITH GRAVE |
| 57 | # (in NFC) |
| 58 | maketrack("Joe Bloggs/First Album/01:F\xC3\x8Crst track.ogg") |
| 59 | |
| 60 | maketrack("Joe Bloggs/First Album/02:Second track.ogg") |
| 61 | |
| 62 | # CC 81 = 0301 COMBINING ACUTE ACCENT |
| 63 | # (giving an NFD i-acute) |
| 64 | maketrack("Joe Bloggs/First Album/03:ThI\xCC\x81rd track.ogg") |
| 65 | # ...hopefuly giving C3 8D = 00CD LATIN CAPITAL LETTER I WITH ACUTE |
| 66 | maketrack("Joe Bloggs/First Album/04:Fourth track.ogg") |
| 67 | maketrack("Joe Bloggs/First Album/05:Fifth track.ogg") |
| 68 | maketrack("Joe Bloggs/Second Album/01:First track.ogg") |
| 69 | maketrack("Joe Bloggs/Second Album/02:Second track.ogg") |
| 70 | maketrack("Joe Bloggs/Second Album/03:Third track.ogg") |
| 71 | maketrack("Joe Bloggs/Second Album/04:Fourth track.ogg") |
| 72 | maketrack("Joe Bloggs/Second Album/05:Fifth track.ogg") |
| 73 | maketrack("Joe Bloggs/Third Album/01:First track.ogg") |
| 74 | maketrack("Joe Bloggs/Third Album/02:Second track.ogg") |
| 75 | maketrack("Joe Bloggs/Third Album/03:Third track.ogg") |
| 76 | maketrack("Joe Bloggs/Third Album/04:Fourth track.ogg") |
| 77 | maketrack("Joe Bloggs/Third Album/05:Fifth track.ogg") |
| 78 | maketrack("Fred Smith/Boring/01:Dull.ogg") |
| 79 | maketrack("Fred Smith/Boring/02:Tedious.ogg") |
| 80 | maketrack("Fred Smith/Boring/03:Drum Solo.ogg") |
| 81 | maketrack("Fred Smith/Boring/04:Yawn.ogg") |
| 82 | maketrack("misc/blahblahblah.ogg") |
| 83 | maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg") |
| 84 | maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg") |
| 85 | |
| 86 | def notracks(): |
| 87 | pass |
| 88 | |
| 89 | def common_setup(): |
| 90 | remove_dir(testroot) |
| 91 | os.mkdir(testroot) |
| 92 | open("%s/config" % testroot, "w").write( |
| 93 | """player *.ogg shell 'echo "$TRACK" >> %s/played.log' |
| 94 | home %s |
| 95 | collection fs UTF-8 %s/tracks |
| 96 | scratch %s/scratch.ogg |
| 97 | gap 0 |
| 98 | stopword 01 02 03 04 05 06 07 08 09 10 |
| 99 | stopword 1 2 3 4 5 6 7 8 9 |
| 100 | stopword 11 12 13 14 15 16 17 18 19 20 |
| 101 | stopword 21 22 23 24 25 26 27 28 29 30 |
| 102 | stopword the a an and to too in on of we i am as im for is |
| 103 | username fred |
| 104 | password fredpass |
| 105 | allow fred fredpass |
| 106 | plugins ../plugins |
| 107 | player *.mp3 execraw disorder-decode |
| 108 | player *.ogg execraw disorder-decode |
| 109 | player *.wav execraw disorder-decode |
| 110 | player *.flac execraw disorder-decode |
| 111 | tracklength *.mp3 disorder-tracklength |
| 112 | tracklength *.ogg disorder-tracklength |
| 113 | tracklength *.wav disorder-tracklength |
| 114 | tracklength *.flac disorder-tracklength |
| 115 | """ % (testroot, testroot, testroot, testroot)) |
| 116 | copyfile("%s/sounds/scratch.ogg" % topsrcdir, |
| 117 | "%s/scratch.ogg" % testroot) |
| 118 | |
| 119 | def start_daemon(test): |
| 120 | """start_daemon(TEST) |
| 121 | Start the daemon for test called TEST.""" |
| 122 | global daemon |
| 123 | assert daemon == None |
| 124 | if test == None: |
| 125 | errs = sys.stderr |
| 126 | else: |
| 127 | errs = open("%s.log" % test, "w") |
| 128 | server = None |
| 129 | print " starting daemon" |
| 130 | daemon = subprocess.Popen(["disorderd", |
| 131 | "--foreground", |
| 132 | "--config", "%s/config" % testroot], |
| 133 | stderr=errs) |
| 134 | disorder._configfile = "%s/config" % testroot |
| 135 | disorder._userconf = False |
| 136 | |
| 137 | def stop_daemon(): |
| 138 | """stop_daemon() |
| 139 | |
| 140 | Stop the daemon if it has not stopped already""" |
| 141 | global daemon |
| 142 | rc = daemon.poll() |
| 143 | if rc == None: |
| 144 | print " stopping daemon" |
| 145 | os.kill(daemon.pid, 15) |
| 146 | rc = daemon.wait() |
| 147 | print " daemon has stopped" |
| 148 | daemon = None |
| 149 | |
| 150 | def run(test, setup=None, report=True, name=None): |
| 151 | global tests |
| 152 | tests += 1 |
| 153 | if setup == None: |
| 154 | setup = stdtracks |
| 155 | common_setup() |
| 156 | setup() |
| 157 | start_daemon(name) |
| 158 | try: |
| 159 | try: |
| 160 | test() |
| 161 | except AssertionError, e: |
| 162 | global failures |
| 163 | failures += 1 |
| 164 | print e |
| 165 | finally: |
| 166 | stop_daemon() |
| 167 | if report: |
| 168 | if failures: |
| 169 | print " FAILED" |
| 170 | sys.exit(1) |
| 171 | else: |
| 172 | print " OK" |
| 173 | |
| 174 | def remove_dir(d): |
| 175 | """remove_dir(D) |
| 176 | |
| 177 | Recursively 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 | |
| 189 | tests = 0 |
| 190 | failures = 0 |
| 191 | daemon = None |
| 192 | testroot = "%s/testroot" % os.getcwd() |
| 193 | tracks = "%s/tracks" % testroot |
| 194 | topsrcdir = os.path.abspath(os.getenv("topsrcdir")) |