PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/build.eclipse/externals/ruby1.9/rake/test_fileutils.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 262 lines | 225 code | 33 blank | 4 comment | 2 complexity | 625fd604536c72302f4627f1abd0b71d MD5 | raw file
  1. require 'rake'
  2. require 'test/unit'
  3. require_relative 'filecreation'
  4. require 'fileutils'
  5. require 'stringio'
  6. class Rake::TestFileUtils < Test::Unit::TestCase
  7. include FileCreation
  8. BASEDIR = File.dirname(__FILE__)
  9. ShellCommand = "#{BASEDIR}/shellcommand.rb"
  10. ENV_RUBY = ENV['RUBY']
  11. def setup
  12. if ruby = ENV_RUBY
  13. @oldruby = FileUtils.class_eval {remove_const :RUBY}
  14. FileUtils.class_eval {const_set(:RUBY, ruby)}
  15. else
  16. @oldruby = nil
  17. end
  18. end
  19. def teardown
  20. FileUtils.rm_rf("testdata")
  21. FileUtils::LN_SUPPORTED[0] = true
  22. if @oldruby
  23. ruby = @oldruby
  24. FileUtils.class_eval {remove_const :RUBY}
  25. FileUtils.class_eval {const_set(:RUBY, ruby)}
  26. end
  27. end
  28. def test_rm_one_file
  29. create_file("testdata/a")
  30. FileUtils.rm_rf "testdata/a"
  31. assert ! File.exist?("testdata/a")
  32. end
  33. def test_rm_two_files
  34. create_file("testdata/a")
  35. create_file("testdata/b")
  36. FileUtils.rm_rf ["testdata/a", "testdata/b"]
  37. assert ! File.exist?("testdata/a")
  38. assert ! File.exist?("testdata/b")
  39. end
  40. def test_rm_filelist
  41. list = Rake::FileList.new << "testdata/a" << "testdata/b"
  42. list.each { |fn| create_file(fn) }
  43. FileUtils.rm_r list
  44. assert ! File.exist?("testdata/a")
  45. assert ! File.exist?("testdata/b")
  46. end
  47. def test_ln
  48. create_dir("testdata")
  49. open("testdata/a", "w") { |f| f.puts "TEST_LN" }
  50. RakeFileUtils.safe_ln("testdata/a", "testdata/b", :verbose => false)
  51. assert_equal "TEST_LN\n", open("testdata/b") { |f| f.read }
  52. end
  53. class BadLink
  54. include RakeFileUtils
  55. attr_reader :cp_args
  56. def initialize(klass)
  57. @failure_class = klass
  58. end
  59. def cp(*args)
  60. @cp_args = args
  61. end
  62. def ln(*args)
  63. fail @failure_class, "ln not supported"
  64. end
  65. public :safe_ln
  66. end
  67. def test_safe_ln_failover_to_cp_on_standard_error
  68. FileUtils::LN_SUPPORTED[0] = true
  69. c = BadLink.new(StandardError)
  70. c.safe_ln "a", "b"
  71. assert_equal ['a', 'b'], c.cp_args
  72. c.safe_ln "x", "y"
  73. assert_equal ['x', 'y'], c.cp_args
  74. end
  75. def test_safe_ln_failover_to_cp_on_not_implemented_error
  76. FileUtils::LN_SUPPORTED[0] = true
  77. c = BadLink.new(NotImplementedError)
  78. c.safe_ln "a", "b"
  79. assert_equal ['a', 'b'], c.cp_args
  80. end
  81. def test_safe_ln_fails_on_script_error
  82. FileUtils::LN_SUPPORTED[0] = true
  83. c = BadLink.new(ScriptError)
  84. assert_raise(ScriptError) do c.safe_ln "a", "b" end
  85. end
  86. def test_verbose
  87. verbose true
  88. assert_equal true, verbose
  89. verbose false
  90. assert_equal false, verbose
  91. verbose(true) {
  92. assert_equal true, verbose
  93. }
  94. assert_equal false, verbose
  95. end
  96. def test_nowrite
  97. nowrite true
  98. assert_equal true, nowrite
  99. nowrite false
  100. assert_equal false, nowrite
  101. nowrite(true){
  102. assert_equal true, nowrite
  103. }
  104. assert_equal false, nowrite
  105. end
  106. def test_file_utils_methods_are_available_at_top_level
  107. create_file("testdata/a")
  108. verbose(false) do
  109. rm_rf "testdata/a"
  110. end
  111. assert ! File.exist?("testdata/a")
  112. end
  113. def test_fileutils_methods_dont_leak
  114. obj = Object.new
  115. assert_raise(NoMethodError) { obj.copy } # from FileUtils
  116. assert_raise(NoMethodError) { obj.ruby } # from RubyFileUtils
  117. end
  118. def test_sh
  119. verbose(false) { sh %{#{RUBY} #{ShellCommand}} }
  120. assert true, "should not fail"
  121. end
  122. # If the :sh method is invoked directly from a test unit instance
  123. # (under mini/test), the mini/test version of fail is invoked rather
  124. # than the kernel version of fail. So we run :sh from within a
  125. # non-test class to avoid the problem.
  126. class Sh
  127. include FileUtils
  128. def run(*args)
  129. sh(*args)
  130. end
  131. def self.run(*args)
  132. new.run(*args)
  133. end
  134. end
  135. def test_sh_with_a_single_string_argument
  136. ENV['RAKE_TEST_SH'] = 'someval'
  137. verbose(false) {
  138. sh %{#{RUBY} #{BASEDIR}/check_expansion.rb #{env_var} someval}
  139. }
  140. end
  141. def test_sh_with_multiple_arguments
  142. ENV['RAKE_TEST_SH'] = 'someval'
  143. verbose(false) {
  144. Sh.run RUBY, File.expand_path('../check_no_expansion.rb', __FILE__), env_var, 'someval'
  145. }
  146. end
  147. def test_sh_failure
  148. assert_raise(RuntimeError) {
  149. verbose(false) { Sh.run "#{RUBY} #{File.expand_path('../shellcommand.rb', __FILE__)} 1" }
  150. }
  151. end
  152. def test_sh_special_handling
  153. count = 0
  154. verbose(false) {
  155. sh(%{#{RUBY} #{ShellCommand}}) do |ok, res|
  156. assert(ok)
  157. assert_equal 0, res.exitstatus
  158. count += 1
  159. end
  160. sh(%{#{RUBY} #{ShellCommand} 1}) do |ok, res|
  161. assert(!ok)
  162. assert_equal 1, res.exitstatus
  163. count += 1
  164. end
  165. }
  166. assert_equal 2, count, "Block count should be 2"
  167. end
  168. def test_sh_noop
  169. verbose(false) { sh %{#{ShellCommand} 1}, :noop=>true }
  170. assert true, "should not fail"
  171. end
  172. def test_sh_bad_option
  173. ex = assert_raise(ArgumentError) {
  174. verbose(false) { sh %{#{ShellCommand}}, :bad_option=>true }
  175. }
  176. assert_match(/bad_option/, ex.message)
  177. end
  178. def test_sh_verbose
  179. out = redirect_stderr {
  180. verbose(true) {
  181. sh %{#{ShellCommand}}, :noop=>true
  182. }
  183. }
  184. assert_match(/^#{Regexp.quote(ShellCommand)}$/o, out)
  185. end
  186. def test_sh_no_verbose
  187. out = redirect_stderr {
  188. verbose(false) {
  189. sh %{#{ShellCommand}}, :noop=>true
  190. }
  191. }
  192. assert_equal '', out
  193. end
  194. def test_ruby_with_a_single_string_argument
  195. ENV['RAKE_TEST_SH'] = 'someval'
  196. verbose(false) {
  197. ruby %{#{BASEDIR}/check_expansion.rb #{env_var} someval}
  198. }
  199. end
  200. def test_ruby_with_multiple_arguments
  201. ENV['RAKE_TEST_SH'] = 'someval'
  202. verbose(false) {
  203. ruby "#{BASEDIR}/check_no_expansion.rb", env_var, 'someval'
  204. }
  205. end
  206. def test_split_all
  207. assert_equal ['a'], RakeFileUtils.split_all('a')
  208. assert_equal ['..'], RakeFileUtils.split_all('..')
  209. assert_equal ['/'], RakeFileUtils.split_all('/')
  210. assert_equal ['a', 'b'], RakeFileUtils.split_all('a/b')
  211. assert_equal ['/', 'a', 'b'], RakeFileUtils.split_all('/a/b')
  212. assert_equal ['..', 'a', 'b'], RakeFileUtils.split_all('../a/b')
  213. end
  214. private
  215. def redirect_stderr
  216. old_err = $stderr
  217. $stderr = StringIO.new
  218. yield
  219. $stderr.string
  220. ensure
  221. $stderr = old_err
  222. end
  223. def windows?
  224. ! File::ALT_SEPARATOR.nil?
  225. end
  226. def env_var
  227. windows? ? '%RAKE_TEST_SH%' : '$RAKE_TEST_SH'
  228. end
  229. end