Class: Transpec::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/transpec/cli.rb

Constant Summary collapse

CONFIG_ATTRS_FOR_CLI_TYPES =
{
  expect_to_matcher: :convert_to_expect_to_matcher=,
  expect_to_receive: :convert_to_expect_to_receive=,
   allow_to_receive: :convert_to_allow_to_receive=,
         deprecated: :replace_deprecated_method=
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



26
27
28
29
# File 'lib/transpec/cli.rb', line 26

def initialize
  @configuration = Configuration.new
  @forced = false
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



19
20
21
# File 'lib/transpec/cli.rb', line 19

def configuration
  @configuration
end

#forcedObject (readonly) Also known as: forced?

Returns the value of attribute forced.



19
20
21
# File 'lib/transpec/cli.rb', line 19

def forced
  @forced
end

Class Method Details

.run(args = ARGV) ⇒ Object



22
23
24
# File 'lib/transpec/cli.rb', line 22

def self.run(args = ARGV)
  new.run(args)
end

Instance Method Details

#parse_options(args) ⇒ Object

rubocop:disable MethodLength



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/transpec/cli.rb', line 61

def parse_options(args)
  parser = OptionParser.new
  parser.banner = "Usage: transpec [options] [files or directories]\n\n"

  parser.on(
    '-f', '--force',
    'Force processing even if the current Git',
    'repository is not clean.'
  ) do
    @forced = true
  end

  parser.on(
    '-d', '--disable TYPE[,TYPE...]',
    'Disable specific conversions.',
    'Available conversion types:',
    '  expect_to_matcher (from `should`)',
    '  expect_to_receive (from `should_receive`)',
    '  allow_to_receive  (from `stub`)',
    '  deprecated (e.g. from `stub!` to `stub`)',
    'These are all enabled by default.'
  ) do |types|
    types.split(',').each do |type|
      config_attr = CONFIG_ATTRS_FOR_CLI_TYPES[type.to_sym]
      fail ArgumentError, "Unknown conversion type #{type.inspect}" unless config_attr
      @configuration.send(config_attr, false)
    end
  end

  parser.on(
    '-n', '--negative-form FORM',
    'Specify negative form of `to` that is used',
    'in `expect(...).to` syntax.',
    'Either `not_to` or `to_not`.',
    'Default: not_to'
  ) do |form|
    @configuration.negative_form_of_to = form
  end

  parser.on(
    '-p', '--no-parentheses-matcher-arg',
    'Suppress parenthesizing argument of matcher',
    'when converting operator to non-operator',
    'in `expect` syntax. Note that it will be',
    'parenthesized even if this option is',
    'specified when parentheses are necessary to',
    'keep the meaning of the expression.',
    'By default, arguments of the following',
    'operator matchers will be parenthesized.',
    '  `== 10` to `eq(10)`',
    '  `=~ /pattern/` to `match(/pattern/)`',
    '  `=~ [1, 2]` to `match_array([1, 2])`'
  ) do
    @configuration.parenthesize_matcher_arg = false
  end

  parser.on('--version', 'Show Transpec version.') do
    puts Version.to_s
    exit
  end

  args = args.dup
  parser.parse!(args)
  args
end

#run(args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/transpec/cli.rb', line 31

def run(args)
  non_option_args = parse_options(args)

  fail_if_should_not_continue!

  paths = non_option_args

  if paths.empty?
    if Dir.exists?('spec')
      paths = ['spec']
    else
      fail ArgumentError, 'Specify target files or directories.'
    end
  end

  target_files(paths).each do |file_path|
    puts "Processing #{file_path}"
    rewriter = Rewriter.new(@configuration)
    rewriter.rewrite_file!(file_path)
  end

  # TODO: Print summary

  true
rescue => error
  warn error.message
  false
end

#target_files(paths) ⇒ Object

rubocop:enable MethodLength



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/transpec/cli.rb', line 128

def target_files(paths)
  paths.reduce([]) do |file_paths, path|
    if File.directory?(path)
      file_paths.concat(ruby_files_in_directory(path))
    elsif File.file?(path)
      file_paths << path
    elsif !File.exists?(path)
      fail ArgumentError, "No such file or directory #{path.inspect}"
    end
  end
end