/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_dir_m17n.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Ruby · 239 lines · 220 code · 15 blank · 4 comment · 47 complexity · fb60a4451cdc2954f69bb96c2f2ac735 MD5 · raw file

  1. require 'test/unit'
  2. require 'tmpdir'
  3. require_relative 'envutil'
  4. class TestDir_M17N < Test::Unit::TestCase
  5. def with_tmpdir
  6. Dir.mktmpdir {|dir|
  7. Dir.chdir(dir) {
  8. yield dir
  9. }
  10. }
  11. end
  12. ## UTF-8 default_external, no default_internal
  13. def test_filename_extutf8
  14. with_tmpdir {|d|
  15. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  16. filename = "\u3042"
  17. File.open(filename, "w") {}
  18. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  19. ents = Dir.entries(".", opts)
  20. exit ents.include?(filename)
  21. EOS
  22. }
  23. end
  24. def test_filename_extutf8_invalid
  25. with_tmpdir {|d|
  26. assert_ruby_status(%w[-EASCII-8BIT], <<-'EOS', nil, :chdir=>d)
  27. filename = "\xff".force_encoding("ASCII-8BIT") # invalid byte sequence as UTF-8
  28. File.open(filename, "w") {}
  29. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  30. ents = Dir.entries(".", opts)
  31. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%FF"))
  32. EOS
  33. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  34. filename = "\xff".force_encoding("UTF-8") # invalid byte sequence as UTF-8
  35. File.open(filename, "w") {}
  36. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  37. ents = Dir.entries(".", opts)
  38. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%FF"))
  39. EOS
  40. }
  41. end unless /mswin|mingw/ =~ RUBY_PLATFORM
  42. def test_filename_as_bytes_extutf8
  43. with_tmpdir {|d|
  44. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  45. filename = "\xc2\xa1".force_encoding("utf-8")
  46. File.open(filename, "w") {}
  47. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  48. ents = Dir.entries(".", opts)
  49. exit ents.include?(filename)
  50. EOS
  51. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  52. if /mswin|mingw/ =~ RUBY_PLATFORM
  53. filename = "\x8f\xa2\xc2".force_encoding("euc-jp")
  54. else
  55. filename = "\xc2\xa1".force_encoding("euc-jp")
  56. end
  57. begin
  58. open(filename) {}
  59. exit true
  60. rescue Errno::ENOENT
  61. exit false
  62. end
  63. EOS
  64. # no meaning test on windows
  65. unless /mswin|mingw/ =~ RUBY_PLATFORM
  66. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  67. filename1 = "\xc2\xa1".force_encoding("utf-8")
  68. filename2 = "\xc2\xa1".force_encoding("euc-jp")
  69. filename3 = filename1.encode("euc-jp")
  70. filename4 = filename2.encode("utf-8")
  71. s1 = File.stat(filename1) rescue nil
  72. s2 = File.stat(filename2) rescue nil
  73. s3 = File.stat(filename3) rescue nil
  74. s4 = File.stat(filename4) rescue nil
  75. exit((s1 && s2 && !s3 && !s4) ? true : false)
  76. EOS
  77. end
  78. }
  79. end
  80. ## UTF-8 default_external, EUC-JP default_internal
  81. def test_filename_extutf8_inteucjp_representable
  82. with_tmpdir {|d|
  83. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  84. filename = "\u3042"
  85. File.open(filename, "w") {}
  86. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  87. ents = Dir.entries(".", opts)
  88. exit ents.include?(filename)
  89. EOS
  90. assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
  91. filename = "\xA4\xA2".force_encoding("euc-jp")
  92. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  93. ents = Dir.entries(".", opts)
  94. exit ents.include?(filename)
  95. EOS
  96. assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
  97. filename = "\xA4\xA2".force_encoding("euc-jp")
  98. begin
  99. open(filename) {}
  100. exit true
  101. rescue Errno::ENOENT
  102. exit false
  103. end
  104. EOS
  105. }
  106. end
  107. def test_filename_extutf8_inteucjp_unrepresentable
  108. with_tmpdir {|d|
  109. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  110. filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
  111. filename2 = "\u3042" # HIRAGANA LETTER A which is representable in EUC-JP
  112. File.open(filename1, "w") {}
  113. File.open(filename2, "w") {}
  114. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  115. ents = Dir.entries(".", opts)
  116. exit ents.include?(filename1) && ents.include?(filename2)
  117. EOS
  118. assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
  119. filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
  120. filename2 = "\xA4\xA2".force_encoding("euc-jp") # HIRAGANA LETTER A in EUC-JP
  121. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  122. ents = Dir.entries(".", opts)
  123. exit ents.include?(filename1) && ents.include?(filename2)
  124. EOS
  125. assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
  126. filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
  127. filename2 = "\u3042" # HIRAGANA LETTER A which is representable in EUC-JP
  128. filename3 = "\xA4\xA2".force_encoding("euc-jp") # HIRAGANA LETTER A in EUC-JP
  129. s1 = File.stat(filename1) rescue nil
  130. s2 = File.stat(filename2) rescue nil
  131. s3 = File.stat(filename3) rescue nil
  132. exit((s1 && s2 && s3) ? true : false)
  133. EOS
  134. }
  135. end
  136. ## others
  137. def test_filename_bytes_euc_jp
  138. with_tmpdir {|d|
  139. assert_ruby_status(%w[-EEUC-JP], <<-'EOS', nil, :chdir=>d)
  140. filename = "\xA4\xA2".force_encoding("euc-jp")
  141. File.open(filename, "w") {}
  142. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  143. ents = Dir.entries(".", opts)
  144. ents.each {|e| e.force_encoding("ASCII-8BIT") }
  145. exit ents.include?(filename.force_encoding("ASCII-8BIT")) ||
  146. ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%A4%A2".force_encoding("ASCII-8BIT")))
  147. EOS
  148. }
  149. end
  150. def test_filename_euc_jp
  151. with_tmpdir {|d|
  152. assert_ruby_status(%w[-EEUC-JP], <<-'EOS', nil, :chdir=>d)
  153. filename = "\xA4\xA2".force_encoding("euc-jp")
  154. File.open(filename, "w") {}
  155. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  156. ents = Dir.entries(".", opts)
  157. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%A4%A2".force_encoding("euc-jp")))
  158. EOS
  159. assert_ruby_status(%w[-EASCII-8BIT], <<-'EOS', nil, :chdir=>d)
  160. filename = "\xA4\xA2"
  161. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  162. ents = Dir.entries(".", opts)
  163. exit ents.include?(filename) ||
  164. ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%A4%A2".force_encoding("ASCII-8BIT"))) ||
  165. ((RUBY_PLATFORM =~ /mswin|mingw/) != nil && ents.include?("\x82\xA0".force_encoding("ASCII-8BIT")))
  166. EOS
  167. }
  168. end
  169. def test_filename_utf8_raw_name
  170. with_tmpdir {|d|
  171. assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
  172. filename = "\u3042".force_encoding("utf-8")
  173. File.open(filename, "w") {}
  174. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  175. ents = Dir.entries(".", opts)
  176. exit ents.include?(filename)
  177. EOS
  178. assert_ruby_status(%w[-EASCII-8BIT], <<-'EOS', nil, :chdir=>d)
  179. filename = "\u3042".force_encoding("ASCII-8BIT")
  180. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  181. ents = Dir.entries(".", opts)
  182. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /mswin|mingw/) != nil && ents.include?("\x82\xA0".force_encoding("ASCII-8BIT")))
  183. EOS
  184. }
  185. end
  186. def test_filename_ext_euc_jp_and_int_utf_8
  187. with_tmpdir {|d|
  188. assert_ruby_status(%w[-EEUC-JP], <<-'EOS', nil, :chdir=>d)
  189. filename = "\xA4\xA2".force_encoding("euc-jp")
  190. File.open(filename, "w") {}
  191. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  192. ents = Dir.entries(".", opts)
  193. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%A4%A2".force_encoding("euc-jp")))
  194. EOS
  195. assert_ruby_status(%w[-EEUC-JP:UTF-8], <<-'EOS', nil, :chdir=>d)
  196. filename = "\u3042"
  197. opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
  198. ents = Dir.entries(".", opts)
  199. exit ents.include?(filename) || ((RUBY_PLATFORM =~ /darwin/) != nil && ents.include?("%A4%A2"))
  200. EOS
  201. }
  202. end
  203. def test_error_nonascii
  204. bug6071 = '[ruby-dev:45279]'
  205. paths = ["\u{3042}".encode("sjis"), "\u{ff}".encode("iso-8859-1")]
  206. encs = with_tmpdir {
  207. paths.map {|path|
  208. Dir.open(path) rescue $!.message.encoding
  209. }
  210. }
  211. assert_equal(paths.map(&:encoding), encs, bug6071)
  212. end
  213. def test_inspect_nonascii
  214. bug6072 = '[ruby-dev:45280]'
  215. paths = ["\u{3042}".encode("sjis"), "\u{ff}".encode("iso-8859-1")]
  216. encs = with_tmpdir {
  217. paths.map {|path|
  218. Dir.mkdir(path)
  219. Dir.open(path) {|d| d.inspect.encoding}
  220. }
  221. }
  222. assert_equal(paths.map(&:encoding), encs, bug6072)
  223. end
  224. end