From: Richard Kettlewell Date: Fri, 21 Mar 2008 16:03:01 +0000 (+0000) Subject: New tests/recode.py converts filenames back and forth between UTF-8 X-Git-Tag: 3.0~16 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/031f8feb8a0ce8c536e5121f51634ec0565ed403?ds=sidebyside New tests/recode.py converts filenames back and forth between UTF-8 and ISO-8859-1 to make sure the server copes. MDW reported a bug in this area; it may be something that's already been fixed in 2.1 since the test works for me. This test won't run on Darwin since filenames must be UTF-8 there. --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 694b0ab..a845799 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -33,4 +33,4 @@ clean-local: rm -rf testroot *.log *.pyc EXTRA_DIST=alltests dtest.py dbversion.py search.py \ - queue.py dump.py play.py cookie.py user-upgrade.py user.py + queue.py dump.py play.py cookie.py user-upgrade.py user.py recode.py diff --git a/tests/dtest.py b/tests/dtest.py index 6a275a8..3937eb6 100644 --- a/tests/dtest.py +++ b/tests/dtest.py @@ -166,11 +166,11 @@ def bindable(p): except: return False -def default_config(): +def default_config(encoding="UTF-8"): """Write the default config""" open("%s/config" % testroot, "w").write( """home %s -collection fs UTF-8 %s/tracks +collection fs %s %s/tracks scratch %s/scratch.ogg gap 0 stopword 01 02 03 04 05 06 07 08 09 10 @@ -195,7 +195,7 @@ api network broadcast 127.0.0.1 %d broadcast_from 127.0.0.1 %d mail_sender no.such.user.sorry@greenend.org.uk -""" % (testroot, testroot, testroot, top_builddir, top_builddir, +""" % (testroot, encoding, testroot, testroot, top_builddir, top_builddir, port, port + 1)) def common_setup(): diff --git a/tests/recode.py b/tests/recode.py new file mode 100755 index 0000000..1539da0 --- /dev/null +++ b/tests/recode.py @@ -0,0 +1,73 @@ +#! /usr/bin/env python +# +# This file is part of DisOrder. +# Copyright (C) 2008 Richard Kettlewell +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA +# +import dtest,disorder,sys,os,unicodedata + +def recode(name, f, t): + recoded = 0 + # First recode basename + d = os.path.dirname(name) + b = os.path.basename(name) + nb = unicodedata.normalize("NFC", unicode(b, f)).encode(t) + if b != nb: + print " %s -> %s" % (repr(b), repr(nb)) + name = os.path.join(d, nb) + os.rename(os.path.join(d, b), name) + recoded += 1 + # Recurse into directories + if os.path.isdir(name): + for c in os.listdir(name): + recoded += recode(os.path.join(name, c), f, t) + return recoded + +def test(): + """Test encoding conversion""" + if sys.platform == "darwin": + print "Sorry, cannot run this test on Darwin" + # ...because local fs is always UTF-8 + return + dtest.start_daemon() + dtest.create_user() + dtest.rescan() + dtest.check_files() + dtest.stop_daemon() + print " recoding as ISO-8859-1" + recoded = recode(dtest.tracks, "UTF-8", "ISO-8859-1") + print " ...recoded %d filenames" % recoded + print " regenerating config" + dtest.default_config(encoding="ISO-8859-1") + print " checking recoded files" + dtest.start_daemon() + dtest.rescan() + dtest.check_files() + dtest.stop_daemon() + print " recoding as UTF-8" + recoded = recode(dtest.tracks, "ISO-8859-1", "UTF-8") + print " ...recoded %d filenames" % recoded + print " regenerating config" + dtest.default_config(encoding="UTF-8") + print " checking recoded files" + dtest.start_daemon() + dtest.rescan() + dtest.check_files() + + +if __name__ == '__main__': + dtest.run()