Class: Goodcheck::Commands::Check

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

Constant Summary collapse

DEFAULT_EXCLUSIONS =
[".git", ".svn", ".hg"].freeze

Constants included from ExitStatus

ExitStatus::EXIT_ERROR, ExitStatus::EXIT_MATCH, ExitStatus::EXIT_SUCCESS, ExitStatus::EXIT_TEST_FAILED

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(config_path:, rules:, targets:, reporter:, stderr:, home_path:, force_download:) ⇒ Check

Returns a new instance of Check.



18
19
20
21
22
23
24
25
26
# File 'lib/goodcheck/commands/check.rb', line 18

def initialize(config_path:, rules:, targets:, reporter:, stderr:, home_path:, force_download:)
  @config_path = config_path
  @rules = rules
  @targets = targets
  @reporter = reporter
  @stderr = stderr
  @force_download = force_download
  @home_path = home_path
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



6
7
8
# File 'lib/goodcheck/commands/check.rb', line 6

def config_path
  @config_path
end

#force_downloadObject (readonly)

Returns the value of attribute force_download.



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

def force_download
  @force_download
end

#home_pathObject (readonly)

Returns the value of attribute home_path.



12
13
14
# File 'lib/goodcheck/commands/check.rb', line 12

def home_path
  @home_path
end

#reporterObject (readonly)

Returns the value of attribute reporter.



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

def reporter
  @reporter
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#targetsObject (readonly)

Returns the value of attribute targets.



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

def targets
  @targets
end

Instance Method Details

#each_checkObject



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
# File 'lib/goodcheck/commands/check.rb', line 71

def each_check
  targets.each do |target|
    Goodcheck.logger.info "Checking target: #{target}"
    each_file target, immediate: true do |path|
      Goodcheck.logger.debug "Checking file: #{path}"
      reporter.file(path) do
        buffers = {}

        config.rules_for_path(path, rules_filter: rules) do |rule, glob, trigger|
          Goodcheck.logger.debug "Checking rule: #{rule.id}"
          begin
            encoding = glob&.encoding || Encoding.default_external.name

            if buffers[encoding]
              buffer = buffers[encoding]
            else
              content = path.read(encoding: encoding).encode(Encoding.default_internal || Encoding::UTF_8)
              buffer = Buffer.new(path: path, content: content)
              buffers[encoding] = buffer
            end

            yield buffer, rule, trigger
          rescue ArgumentError => exn
            stderr.puts "#{path}: #{exn.inspect}"
          end
        end
      end
    end
  end
end

#each_file(path, immediate: false, &block) ⇒ Object



102
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
# File 'lib/goodcheck/commands/check.rb', line 102

def each_file(path, immediate: false, &block)
  case
  when path.symlink?
    # noop
  when path.directory?
    case
    when DEFAULT_EXCLUSIONS.include?(path.basename.to_s)
      # noop
    when immediate || !excluded?(path)
      path.children.sort.each do |child|
        each_file(child, &block)
      end
    end
  when path.file?
    case
    when path == config_path
      # Skip the config file unless explicitly given by command line
      yield path if immediate
    when excluded?(path)
      # Skip excluded files unless explicitly given by command line
      yield path if immediate
    else
      yield path
    end
  end
end

#excluded?(path) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/goodcheck/commands/check.rb', line 129

def excluded?(path)
  config.exclude_path?(path)
end

#missing_rulesObject



64
65
66
67
68
69
# File 'lib/goodcheck/commands/check.rb', line 64

def missing_rules
  @missing_rules ||= begin
    config_rule_ids = config.rules.map(&:id)
    rules.reject { |rule| config_rule_ids.include?(rule) }
  end
end

#runObject



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
# File 'lib/goodcheck/commands/check.rb', line 28

def run
  handle_config_errors(stderr) do
    issue_reported = false

    reporter.analysis do
      load_config!(force_download: force_download, cache_path: cache_dir_path)

      unless missing_rules.empty?
        missing_rules.each do |rule|
          stderr.puts "missing rule: #{rule}"
        end
        return EXIT_ERROR
      end

      each_check do |buffer, rule, trigger|
        reported_issues = Set[]

        reporter.rule(rule) do
          analyzer = Analyzer.new(rule: rule, buffer: buffer, trigger: trigger)
          analyzer.scan do |issue|
            next if issue.location && buffer.line_disabled?(issue.location.start_line)
            if reported_issues.add?(issue)
              issue_reported = true
              reporter.issue(issue)
            end
          end
        end
      end
    end

    reporter.summary

    issue_reported ? EXIT_MATCH : EXIT_SUCCESS
  end
end