Class: Jackal::Utils::Spec::Generator

Inherits:
Bogo::Cli::Command
  • Object
show all
Defined in:
lib/jackal/utils/spec/generator.rb

Overview

Test files generator

Constant Summary collapse

TEST_DIR =

Test directory path

'test/specs'
TEST_CONFIG_DIR =

Test configuration directory path

'test/specs/config'
GEMFILE_LINES =

Lines required within Gemfile

[
  "gem 'carnivore-actor'",
  "gem 'minitest'",
  "gem 'pry'"
]

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ self

Create new instance



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jackal/utils/spec/generator.rb', line 25

def initialize(*_)
  super

  @orig_service_name = options[:service_name].downcase
  @service_name = Bogo::Utility.snake(@orig_service_name)
  @service_class_name = Bogo::Utility.camel(@service_name)

  @module_name = options[:module_name].downcase
  @module_class_name = Bogo::Utility.camel(@module_name)

  @callback_type   = 'jackal'
  @supervisor_name = "jackal_#{@service_name}_input"
end

Instance Method Details

#config_file_contentString

Configuration file content

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jackal/utils/spec/generator.rb', line 78

def config_file_content
  "Configuration.new do\n  jackal do\n    require [\"carnivore-actor\", \"jackal-\#{@orig_service_name}\"]\n\n    mail do\n      config do\n      end\n\n      sources do\ninput  { type 'actor' }\noutput { type 'spec' }\n      end\n\n      callbacks ['Jackal::\#{@service_class_name}::\#{@module_class_name}']\n    end\n  end\nend\n"
end

#execute!TrueClass

Generate test files

Returns:

  • (TrueClass)


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
# File 'lib/jackal/utils/spec/generator.rb', line 42

def execute!
  ui.info 'Generating jackal test files'

  # ensure dependencies are present in Gemfile
  run_action 'Update Gemfile contents' do
    update_gemfile
    nil
  end

  run_action 'Create testing directory structure' do
    # Create test directory structure
    [TEST_DIR, TEST_CONFIG_DIR].each do |dir|
      FileUtils.mkdir_p(dir)
    end
    nil
  end

  run_action 'Write test configuration file' do
    conf_path = File.join(Dir.pwd, TEST_CONFIG_DIR, "#{@module_name}.rb")
    write_file(conf_path, config_file_content)
    nil
  end

  run_action 'Write default test spec file' do
    spec_path = File.join(Dir.pwd, TEST_DIR, "#{@service_name}_spec.rb")
    write_file(spec_path, spec_file_content)
    nil
  end

  ui.info 'Jackal test file generation complete!'
  true
end

#spec_file_contentString

Spec file content

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jackal/utils/spec/generator.rb', line 103

def spec_file_content
  "require '\#{@callback_type}-\#{@orig_service_name}'\nrequire 'pry'\n\n# To stub out an api call for your callback\nclass \#{callback_class}::\#{@service_class_name}::\#{@module_class_name}\n  attr_accessor :test_payload\n\n  #def api_call(args)\n  #  test_payload.set(:args, args)\n  #end\nend\n\ndescribe \#{callback_class}::\#{@service_class_name}::\#{@module_class_name} do\n\n  before do\n    @runner = run_setup(:\#{@module_name})\n    track_execution(\#{callback_class}::\#{@service_class_name}::\#{@module_class_name})\n  end\n\n  after do\n    @runner.terminate\n  end\n\n  let(:actor) { Carnivore::Supervisor.supervisor[:\#{@supervisor_name}] }\n\n  it 'executes with empty payload' do\n    result = transmit_and_wait(actor, payload)\n    (!!result).must_equal true\n    callback_executed?(result).must_equal true\n  end\n\n  private\n\n  # payload to send for callback execution\n  def payload\n    Jackal::Utils.new_payload(:test, Smash.new)\n  end\n\nend\n"
end