Class: Robotag

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Robotag

Returns a new instance of Robotag.



6
7
8
9
10
# File 'lib/robotag.rb', line 6

def initialize(opts)
  self.repo = CQL::Repository.new(opts[:repo])
  self.write_directives = {}
  self
end

Instance Attribute Details

#chain_stateObject

Returns the value of attribute chain_state.



4
5
6
# File 'lib/robotag.rb', line 4

def chain_state
  @chain_state
end

#repoObject

Returns the value of attribute repo.



4
5
6
# File 'lib/robotag.rb', line 4

def repo
  @repo
end

#write_directivesObject

Returns the value of attribute write_directives.



4
5
6
# File 'lib/robotag.rb', line 4

def write_directives
  @write_directives
end

Instance Method Details

#add_tag(test, tag) ⇒ Object



50
51
52
53
54
# File 'lib/robotag.rb', line 50

def add_tag(test, tag)
  return if has_tag?(test, tag)

  tag_test(test, tag)
end

#all_have_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/robotag.rb', line 21

def all_have_tag?(tag)
  self.chain_state = self.chain_state.map do |query_result|
    query_result[:self].tags.map do |cm_tag|
      cm_tag.map(&:name)
    end
  end.all? do |mapped_qr|
    mapped_qr.flatten.any? do |test_tag|
      test_tag == tag
    end
  end
  self
end

#go!Object



94
95
96
97
98
99
100
101
102
# File 'lib/robotag.rb', line 94

def go!
  if !self.write_directives.empty?
    self.write_directives.each do |file_path, joined_file_lines|
      File.open(file_path, 'w+') { |file| file.print joined_file_lines }
    end
  else
    self.chain_state
  end
end

#has_tag?(test, tag) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/robotag.rb', line 77

def has_tag?(test, tag)
  test.tags.map(&:name).include?(tag)
end

#previewObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/robotag.rb', line 81

def preview
  unless self.write_directives.empty?
    dirname = "robotag_preview"
    FileUtils.rm_rf(dirname)
    self.write_directives.each do |filepath, file_contents|
      FileUtils.mkdir_p(File.dirname("#{dirname}/#{filepath}"))
      File.open("#{dirname}/#{filepath}", 'w+') do |tmp_file|
        tmp_file.print file_contents
      end
    end
  end
end

#tag_all_with(tag) ⇒ Object

This is heavily borrowed from raw.githubusercontent.com/enkessler/cuke_cataloger/master/lib/cuke_cataloger/unique_test_case_tagger.rb Specifically it is an adapted version of #tag_tests



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

def tag_all_with(tag)
  warn('This script will potentially rewrite all of your feature files. Please be patient and remember to tip your source control system.') # rubocop:disable Metrics/LineLength

  # Analysis and output
  self.chain_state.map { |query_result| query_result[:self] }.each do |test|
    if (test.is_a?(CukeModeler::Scenario) || test.is_a?(CukeModeler::Outline))
      add_tag(test, tag)
    else
      raise("Unknown test type: #{test.class}")
    end
  end
  self
end

#tag_test(test, tag) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/robotag.rb', line 56

def tag_test(test, tag)
  feature_file = test.get_ancestor(:feature_file)
  file_path = feature_file.path

  tag_index = (test.source_line - 2)

  file_lines = if self.write_directives[file_path].nil?
                File.readlines(file_path)
              else
                self.write_directives[file_path].split("\n").map {|line| line + "\n"}
              end
  file_lines[tag_index] = "#{file_lines[tag_index].chomp} #{tag}\n"

  self.write_directives[file_path] = file_lines.join

  new_tag = CukeModeler::Tag.new
  new_tag.name = tag
  test.tags << new_tag
  self
end

#tests_with_steps_that_match(my_regex) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/robotag.rb', line 12

def tests_with_steps_that_match(my_regex)
  self.chain_state = self.repo.query do
    select
    from scenarios, outlines
    with { |scenario| scenario.steps.map(&:text).any? { |step| step =~ my_regex } }
  end
  self
end