genTestRes.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # Generates files and directories in test-res used for Unicode sys tests.
  3. # The test vector printf'ed into data.bin, as well as the names in filenames()
  4. # should correspond exactly to the sequences in UnicodeSequences.valid.
  5. # Run with:
  6. # python3 genTestRes.py
  7. # Or:
  8. # python3 genTestRes.py TEST_INVALID_UNICODE_FS
  9. # The latter will attempt to create filenames which contain invalid Unicode
  10. # codepoints; this does not work on some filesystems, e.g. APFS.
  11. import os
  12. import shutil
  13. import sys
  14. MODE = " ".join(sys.argv[1:])
  15. TESTDIR = "test-res"
  16. # delete previous, if any
  17. if os.path.isdir(TESTDIR):
  18. shutil.rmtree(TESTDIR)
  19. os.mkdir(TESTDIR)
  20. # Unicode test vectors
  21. allUnicode = [
  22. [0x01], # will not work on NTFS
  23. [0x7F],
  24. [0xC2, 0x80],
  25. [0xDF, 0xBF],
  26. [0xE0, 0xA0, 0x80],
  27. [0xED, 0x9F, 0xBF], # will not work on APFS
  28. [0xEE, 0x80, 0x80],
  29. [0xEF, 0xBF, 0xBD],
  30. [0xF0, 0x90, 0x80, 0x80],
  31. [0xF0, 0x9F, 0xBF, 0xBF], # will not work on APFS
  32. [0xF3, 0xBF, 0xBF, 0xBF], # will not work on APFS
  33. [0xF4, 0x80, 0x80, 0x80],
  34. [0xF4, 0x8F, 0xBF, 0xBF], # will not work on APFS
  35. [0xF0, 0x9F, 0x98, 0x82, 0xF0, 0x9F, 0x98, 0x84, 0xF0, 0x9F, 0x98, 0x99],
  36. [0xC8, 0xA7],
  37. [0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, 0xEF, 0xBC, 0x8C, 0xE3, 0x81, 0xAB, 0xE3, 0x81, 0xBB, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0x94]
  38. ]
  39. allStrings = [ bytes(data).decode("utf-8") for data in allUnicode ]
  40. allFilenames = allStrings[:]
  41. # Windows does not allow codepoints in the U+0000 - U+001F range
  42. # see https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
  43. if os.name == "nt":
  44. allFilenames.remove(bytes([0x01]).decode("utf-8"))
  45. # on APFS (macOS 10.13+), filenames must consist of valid Unicode codepoints
  46. if MODE != "TEST_INVALID_UNICODE_FS":
  47. allFilenames.remove(bytes([0xED, 0x9F, 0xBF]).decode("utf-8"))
  48. allFilenames.remove(bytes([0xF0, 0x9F, 0xBF, 0xBF]).decode("utf-8"))
  49. allFilenames.remove(bytes([0xF3, 0xBF, 0xBF, 0xBF]).decode("utf-8"))
  50. allFilenames.remove(bytes([0xF4, 0x8F, 0xBF, 0xBF]).decode("utf-8"))
  51. allBinary = b""
  52. for data in allUnicode:
  53. allBinary += bytes(data) + b"\n"
  54. # generate a file with Unicode data
  55. with open(os.path.join(TESTDIR, "data.bin"), "wb") as f:
  56. f.write(allBinary)
  57. # generate sub-directories with symlinks
  58. os.mkdir(os.path.join(TESTDIR, "a"))
  59. for data in allFilenames:
  60. os.mkdir(os.path.join(TESTDIR, data))
  61. os.mkdir(os.path.join(TESTDIR, "a", data))
  62. if os.name != "nt":
  63. for target, name in [
  64. ("../../bin/cpp/UtilityProcess-debug", "bin-cpp-debug"),
  65. ("../../bin/cpp/UtilityProcess", "bin-cpp"),
  66. ("../../bin/cs/bin/UtilityProcess-Debug.exe", "bin-cs-debug"),
  67. ("../../bin/cs/bin/UtilityProcess.exe", "bin-cs"),
  68. ("../../bin/hl/UtilityProcess.hl", "bin-hl"),
  69. ("../../bin/lua/UtilityProcess.lua", "bin-lua"),
  70. ("../../bin/java/UtilityProcess-Debug.jar", "bin-java-debug"),
  71. ("../../bin/java/UtilityProcess.jar", "bin-java"),
  72. ("../../bin/jvm/UtilityProcess.jar", "bin-jvm"),
  73. ("../../bin/neko/UtilityProcess.n", "bin-neko"),
  74. ("../../bin/php/UtilityProcess/index.php", "bin-php"),
  75. ("../../bin/python/UtilityProcess.py", "bin-py"),
  76. ("../../src/UtilityProcess.hx", "bin-eval")
  77. ]:
  78. os.symlink(target, os.path.join(TESTDIR, data, name), target_is_directory = False)
  79. # files
  80. os.mkdir(os.path.join(TESTDIR, "b"))
  81. for data in allFilenames:
  82. with open(os.path.join(TESTDIR, "b", data), "wb") as f:
  83. f.write(allBinary)