Class: Goodcheck::Commands::Test

Inherits:
Object
  • Object
show all
Includes:
ConfigLoading, HomePath
Defined in:
lib/goodcheck/commands/test.rb

Instance Attribute Summary collapse

Attributes included from ConfigLoading

#config

Instance Method Summary collapse

Methods included from HomePath

#cache_dir_path

Methods included from ConfigLoading

#handle_config_errors, #load_config!

Constructor Details

#initialize(stdout:, stderr:, config_path:, force_download:, home_path:) ⇒ Test

Returns a new instance of Test.



13
14
15
16
17
18
19
# File 'lib/goodcheck/commands/test.rb', line 13

def initialize(stdout:, stderr:, config_path:, force_download:, home_path:)
  @stdout = stdout
  @stderr = stderr
  @config_path = config_path
  @force_download = force_download
  @home_path = home_path
end

Instance Attribute Details

#config_path ⇒ Object (readonly)

Returns the value of attribute config_path.



9
10
11
# File 'lib/goodcheck/commands/test.rb', line 9

def config_path
  @config_path
end

#force_download ⇒ Object (readonly)

Returns the value of attribute force_download.



11
12
13
# File 'lib/goodcheck/commands/test.rb', line 11

def force_download
  @force_download
end

#home_path ⇒ Object (readonly)

Returns the value of attribute home_path.



10
11
12
# File 'lib/goodcheck/commands/test.rb', line 10

def home_path
  @home_path
end

#stderr ⇒ Object (readonly)

Returns the value of attribute stderr.



8
9
10
# File 'lib/goodcheck/commands/test.rb', line 8

def stderr
  @stderr
end

#stdout ⇒ Object (readonly)

Returns the value of attribute stdout.



7
8
9
# File 'lib/goodcheck/commands/test.rb', line 7

def stdout
  @stdout
end

Instance Method Details

#rule_matches_example?(rule, trigger, example) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
# File 'lib/goodcheck/commands/test.rb', line 114

def rule_matches_example?(rule, trigger, example)
  buffer = Buffer.new(path: Pathname("-"), content: example)
  analyzer = Analyzer.new(rule: rule, buffer: buffer, trigger: trigger)
  analyzer.scan.count > 0
end

#run ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/goodcheck/commands/test.rb', line 21

def run
  handle_config_errors stderr do
    load_config!(cache_path: cache_dir_path, force_download: force_download)

    validate_rule_uniqueness or return 1
    validate_rules or return 1

    0
  end
end

#validate_rule_uniqueness ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/goodcheck/commands/test.rb', line 32

def validate_rule_uniqueness
  stdout.puts "Validating rule id uniqueness..."

  duplicated_ids = []

  config.rules.group_by(&:id).each do |id, rules|
    if rules.size > 1
      duplicated_ids << id
    end
  end

  if duplicated_ids.empty?
    stdout.puts "  OK!👍"
    true
  else
    stdout.puts(Rainbow("  Found #{duplicated_ids.size} duplications.😞").red)
    duplicated_ids.each do |id|
      stdout.puts "    #{id}"
    end
    false
  end
end

#validate_rules ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/goodcheck/commands/test.rb', line 55

def validate_rules
  test_pass = true

  config.rules.each do |rule|
    if rule.triggers.any? {|trigger| !trigger.passes.empty? || !trigger.fails.empty?}
      stdout.puts "Testing rule #{rule.id}..."

      rule_ok = true

      rule.triggers.each.with_index do |trigger, index|
        if !trigger.passes.empty? || !trigger.fails.empty?
          if trigger.by_pattern?
            stdout.puts "  Testing pattern..."
          else
            stdout.puts "  Testing #{(index+1).ordinalize} trigger..."
          end

          pass_errors = trigger.passes.each.with_index.select do |pass, _|
            rule_matches_example?(rule, trigger, pass)
          end

          fail_errors = trigger.fails.each.with_index.reject do |fail, _|
            rule_matches_example?(rule, trigger, fail)
          end

          unless pass_errors.empty?
            test_pass = false
            rule_ok = false

            pass_errors.each do |_, index|
              stdout.puts "    #{(index+1).ordinalize} pass example matched.😱"
            end
          end

          unless fail_errors.empty?
            test_pass = false
            rule_ok = false

            fail_errors.each do |_, index|
              stdout.puts "    #{(index+1).ordinalize} fail example didn't match.😱"
            end
          end
        end
      end

      if rule.triggers.any?(&:skips_fail_examples?)
        stdout.puts "  🚨 The rule contains a `pattern` with `glob`, which is not supported by the test command."
        stdout.puts "    Skips testing `fail` examples."
      end

      if rule_ok
        stdout.puts "  OK!🎉"
      end
    end
  end

  test_pass
end