Class: MinitestToRspec::CLI

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

Overview

Command-Line Interface (CLI) instantiated by ‘bin/mt2rspec`

Constant Summary collapse

E_USAGE =
1
E_FILE_NOT_FOUND =
2
E_FILE_ALREADY_EXISTS =
3
E_CONVERT_FAIL =
4
E_CANNOT_CREATE_TARGET_DIR =
5
<<~EOS
  Usage: mt2rspec [--rails] [--mocha] source_file [target_file]

  Reads source_file, writes target_file. If target_file is omitted,
  its location will be inferred. For example, test/fruit/banana_test.rb
  implies spec/fruit/banana_spec.rb. If the target directory doesn't
  exist, it will be created.

  Options:
EOS
OPT_MOCHA =
'Convert mocha to rspec-mocks. (Experimental)'
OPT_RAILS =
<<~EOS.tr("\n", ' ').freeze
  Requires rails_helper instead of spec_helper.
  Passes :type metadatum to RSpec.describe.
EOS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/minitest_to_rspec/cli.rb', line 34

def initialize(args)
  opts = parse_args(args)
  @rails = opts[:rails]
  @mocha = opts[:mocha]
  case args.length
  when 2
    @source, @target = args
  when 1
    @source = args[0]
    @target = infer_target_from @source
  else
    warn 'Please specify source file'
    exit E_USAGE
  end
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



32
33
34
# File 'lib/minitest_to_rspec/cli.rb', line 32

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



32
33
34
# File 'lib/minitest_to_rspec/cli.rb', line 32

def target
  @target
end

Instance Method Details

#runObject



50
51
52
53
54
55
56
57
58
# File 'lib/minitest_to_rspec/cli.rb', line 50

def run
  assert_file_exists(source)
  assert_file_does_not_exist(target)
  ensure_target_directory(target)
  write_target(converter.convert(read_source, source))
rescue Error => e
  warn "Failed to convert: #{e}"
  exit E_CONVERT_FAIL
end