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

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#args_descriptionObject



15
# File 'lib/brut/cli/apps/scaffold.rb', line 15

def args_description = "source_file_paths..."

#descriptionObject



14
# File 'lib/brut/cli/apps/scaffold.rb', line 14

def description = "Create the shell of a unit test based on an existing source file"

#runObject



16
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
# File 'lib/brut/cli/apps/scaffold.rb', line 16

def run
  if argv.empty?
    stderr.puts "'test' requires one or more files to scaffold a test for"
    return 1
  end
  files_to_test_files = argv.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) }
    stderr.puts "Not all input files exist:"
    relative_paths.each do |file|
      stderr.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) }
    stderr.puts "Some files to be generated already exist. Set --overwrite to overwrite them:"
    relative_paths.each do |file|
      stderr.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}"
    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("::")
    }


    stdout.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?
      stdout.puts code
    else
      FileUtils.mkdir_p destination.dirname
      File.open(destination,"w") do |file|
        file.puts code
      end
    end
  end

  0
end