Class: Brut::CLI::Apps::Scaffold::Test

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



17
18
19
20
21
22
23
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/brut/cli/apps/scaffold.rb', line 17

def execute
  if args.empty?
    err.puts "'test' requires one or more files to scaffold a test for"
    return 1
  end
  files_to_test_files = args.map { |arg|
    Pathname(arg).expand_path
  }.map { |pathname|
    relative = pathname.relative_path_from(Brut.container.app_src_dir)
    test_file = Brut.container.app_specs_dir / relative.dirname / "#{relative.basename(relative.extname)}.spec.rb"
    [ pathname, test_file ]
  }.to_h

  non_existent_sources  = files_to_test_files.keys.select   { |pathname| !pathname.exist? }
  existent_destinations = files_to_test_files.values.select { |pathname| pathname.exist? }

  if non_existent_sources.any?
    relative_paths = non_existent_sources.map { |pathname| pathname.relative_path_from(Brut.container.project_root) }
    err.puts "Not all input files exist:"
    relative_paths.each do |file|
      err.puts file
    end
    return 1
  end

  if existent_destinations.any? && !global_options.overwrite?
    relative_paths = existent_destinations.map { |pathname| pathname.relative_path_from(Brut.container.project_root) }
    err.puts "Some files to be generated already exist. Set --overwrite to overwrite them:"
    relative_paths.each do |file|
      err.puts file
    end
    return 1
  end

  files_to_test_files.each do |source,destination|
    result = Prism.parse_file(source.to_s)
    if !result
      raise "For some reason Prism did not parse #{source.to_s}"
    end
    classes = find_classes(result.value).map { |(module_nodes,class_node)|
      (module_nodes.map(&:constant_path).map(&:full_name).map(&:to_s) + [class_node.constant_path.full_name.to_s]).compact.join("::")
    }


    out.puts "#{destination} will contain tests for:\n#{classes.join("\n")}\n\n"

    code = ["require \"spec_helper\"\n"] + classes.map { |class_name|
      %{RSpec.describe #{class_name} do
  it "should have tests" do
expect(false).to eq(true)
  end
end}
    }

    if global_options.dry_run?
      out.puts code
    else
      FileUtils.mkdir_p destination.dirname
      File.open(destination,"w") do |file|
        file.puts code
      end
    end
  end

  0
end