Class: MkSpec
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #create_directory(mod) ⇒ Object
- #create_file(dir, mod, meth, name) ⇒ Object
-
#initialize ⇒ MkSpec
constructor
A new instance of MkSpec.
- #options(argv = ARGV) ⇒ Object
-
#ruby ⇒ Object
Determine and return the path of the ruby executable.
- #run ⇒ Object
- #write_requires(dir, file) ⇒ Object
- #write_spec(file, meth, exists) ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/mspec/commands/mkspec.rb', line 13 def config @config end |
Class Method Details
.main ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/mspec/commands/mkspec.rb', line 132 def self.main ENV['MSPEC_RUNNER'] = '1' script = new script. script.run end |
Instance Method Details
#create_directory(mod) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mspec/commands/mkspec.rb', line 54 def create_directory(mod) subdir = @map.dir_name mod, config[:base] if File.exist? subdir unless File.directory? subdir puts "#{subdir} already exists and is not a directory." return nil end else FileUtils.mkdir_p subdir end subdir end |
#create_file(dir, mod, meth, name) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/mspec/commands/mkspec.rb', line 99 def create_file(dir, mod, meth, name) file = File.join dir, @map.file_name(meth, mod) exists = File.exist? file write_requires dir, file unless exists write_spec file, name, exists end |
#options(argv = ARGV) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mspec/commands/mkspec.rb', line 24 def (argv=ARGV) = MSpecOptions.new "mkspec [options]" .on("-c", "--constant", "CONSTANT", "Class or Module to generate spec stubs for") do |name| config[:constants] << name end .on("-b", "--base", "DIR", "Directory to generate specs into") do |directory| config[:base] = File. directory end .on("-r", "--require", "LIBRARY", "A library to require") do |file| config[:requires] << file end .version MSpec::VERSION .help .doc "\n How might this work in the real world?\n" .doc " 1. To create spec stubs for every class or module in Object\n" .doc " $ mkspec\n" .doc " 2. To create spec stubs for Fixnum\n" .doc " $ mkspec -c Fixnum\n" .doc " 3. To create spec stubs for Complex in 'superspec/complex'\n" .doc " $ mkspec -c Complex -rcomplex -b superspec" .doc "" .parse argv end |
#ruby ⇒ Object
Determine and return the path of the ruby executable.
123 124 125 126 127 128 129 130 |
# File 'lib/mspec/commands/mkspec.rb', line 123 def ruby ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) ruby.gsub! File::SEPARATOR, File::ALT_SEPARATOR if File::ALT_SEPARATOR return ruby end |
#run ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mspec/commands/mkspec.rb', line 107 def run config[:requires].each { |lib| require lib } constants = config[:constants] constants = @map.filter(Object.constants) if constants.empty? @map.map({}, constants).each do |mod, methods| name = mod.chop next unless dir = create_directory(name) methods.each { |method| create_file dir, name, method, mod + method } end end |
#write_requires(dir, file) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mspec/commands/mkspec.rb', line 69 def write_requires(dir, file) /\A#{Regexp.escape config[:base]}\/?(.*)/ =~ dir parents = '../' * ($1.split('/').length + 1) File.open file, 'w' do |f| f.puts "require File.dirname(__FILE__) + '/#{parents}spec_helper'" config[:requires].each do |lib| f.puts "require '#{lib}'" end end end |
#write_spec(file, meth, exists) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mspec/commands/mkspec.rb', line 81 def write_spec(file, meth, exists) if exists out = `#{ruby} #{MSPEC_HOME}/bin/mspec-run --dry-run -fs -e '#{meth}' #{file}` return if out =~ /#{Regexp.escape meth}/ end File.open file, 'a' do |f| f.puts <<-EOS describe "#{meth}" do it "needs to be reviewed for spec completeness" end EOS end puts file end |