chiark / gitweb /
New tests/recode.py converts filenames back and forth between UTF-8
authorRichard Kettlewell <rjk@greenend.org.uk>
Fri, 21 Mar 2008 16:03:01 +0000 (16:03 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Fri, 21 Mar 2008 16:03:01 +0000 (16:03 +0000)
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.

tests/Makefile.am
tests/dtest.py
tests/recode.py [new file with mode: 0755]

index 694b0ab2580f7848070f41baf32b8c8161dae2d9..a845799eab433ef315f14f5c3819a691eee18845 100644 (file)
@@ -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
index 6a275a890bb0a6fbebd1b6cd70530c0a4a3137f0..3937eb6f7568ed03144269e7ee6e5c9c6100b7c3 100644 (file)
@@ -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 (executable)
index 0000000..1539da0
--- /dev/null
@@ -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()