1
0

console.thor 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'pry'
  2. class ConsoleCLI < Thor
  3. def self.to_s
  4. 'Console'
  5. end
  6. def initialize(*args)
  7. trap('INT') { puts; exit } # exit on ^C
  8. super
  9. end
  10. default_command :default
  11. desc '', 'Start a REPL'
  12. def default
  13. Pry.start
  14. end
  15. desc 'docs', 'Start a REPL in the "Docs" module'
  16. def docs
  17. require 'docs'
  18. Docs.pry
  19. end
  20. end
  21. Pry::Commands.create_command 'test' do
  22. description 'Run tests in the "test" directory'
  23. group 'Testing'
  24. banner <<-BANNER
  25. Usage: test [<path>]
  26. If <path> is a file, run it ("_test.rb" suffix is optional).
  27. If <path> is a directory, run all test files inside it.
  28. Default to all test files.
  29. BANNER
  30. def process
  31. if pattern = args.first
  32. pattern.prepend 'test/'
  33. if File.directory?(pattern)
  34. pattern << '/**/*_test.rb'
  35. elsif File.extname(pattern).empty?
  36. pattern << '*_test.rb'
  37. end
  38. else
  39. pattern = 'test/**/*_test.rb'
  40. end
  41. paths = Dir.glob(pattern).map(&File.method(:expand_path))
  42. if paths.empty?
  43. output.puts 'No test files found.'
  44. return
  45. end
  46. pid = fork do
  47. begin
  48. $LOAD_PATH.unshift 'test'
  49. paths.each(&method(:require))
  50. rescue Exception => e
  51. _pry_.last_exception = e
  52. run 'wtf?'
  53. exit!
  54. end
  55. end
  56. Process.wait(pid)
  57. end
  58. end