Class: Pedant::Check
- Inherits:
-
Object
show all
- Defined in:
- lib/pedant/check.rb
Direct Known Subclasses
CheckArityOfBuiltins, CheckConditionalContainsAssignment, CheckConditionalOrLoopIsEmpty, CheckConfusingVariableNames, CheckContainsDisplay, CheckContainsIpAddressLiterals, CheckContainsNoCarriageReturns, CheckContainsNoDuplicateWords, CheckContainsNoTabs, CheckContainsRegistrationSection, CheckContainsUnreachableCode, CheckEndsWithNewline, CheckEqualityWithRegex, CheckFilesParseWithoutErrors, CheckFlippedOperandsOnMatchOrSubstring, CheckGetByteUsed, CheckLocalVariableUnused, CheckNonsenseComparison, CheckParseTestCode, CheckPluginTypeNotSpecified, CheckScriptCategory, CheckScriptDoesNotUseAuditDotInc, CheckScriptFamilyNotSpecified, CheckScriptId, CheckScriptName, CheckScriptNotSignedAndUsingSecretKBItem, CheckScriptNotSignedAndUsingTrustedFunction, CheckScriptSummary, CheckSocketLeak, CheckUsesOctalIntegers
Constant Summary
collapse
- @@statuses =
{
:fatal => Rainbow('DIED').color(:red),
:fail => Rainbow('FAIL').color(:red),
:pass => Rainbow('PASS').color(:green),
:skip => Rainbow('SKIP').color(:green),
:warn => Rainbow('WARN').color(:yellow),
:void => Rainbow('VOID').color(:magenta)
}
- @@levels =
[:error, :warn, :info]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(kb) ⇒ Check
Returns a new instance of Check.
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/pedant/check.rb', line 46
def initialize(kb)
@report = []
@result = :void
@kb = kb
return unless @kb[:test_mode]
Check.run_checks_in_dependency_order(kb, self.class.depends)
end
|
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
29
30
31
|
# File 'lib/pedant/check.rb', line 29
def result
@result
end
|
Class Method Details
.all ⇒ Object
61
62
63
|
# File 'lib/pedant/check.rb', line 61
def self.all
(@_all ||= [])
end
|
.depends ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/pedant/check.rb', line 83
def self.depends
keys = self.requires
Check.all.reject do |cls|
(cls.provides & keys).empty?
end
end
|
.friendly_name ⇒ Object
145
146
147
148
|
# File 'lib/pedant/check.rb', line 145
def self.friendly_name
self.name.gsub(/.*::/, '').gsub(/^Check/, '').gsub(/[A-Z][^A-Z]*/, ' \&').strip
end
|
.inherited(cls) ⇒ Object
65
66
67
|
# File 'lib/pedant/check.rb', line 65
def self.inherited(cls)
all << cls
end
|
.initialize! ⇒ Object
42
43
44
|
# File 'lib/pedant/check.rb', line 42
def self.initialize!
Dir.glob(Pedant.lib + 'pedant/checks/*.rb').each { |f| load(f) }
end
|
.list ⇒ Object
57
58
59
|
# File 'lib/pedant/check.rb', line 57
def self.list
all.map{ |cls| cls.friendly_name }.sort
end
|
.provides ⇒ Object
69
70
71
|
# File 'lib/pedant/check.rb', line 69
def self.provides
return []
end
|
.ready?(kb) ⇒ Boolean
77
78
79
80
81
|
# File 'lib/pedant/check.rb', line 77
def self.ready?(kb)
self.requires.reduce(true) do |stat, req|
stat && kb.has_key?(req)
end
end
|
.requires ⇒ Object
73
74
75
|
# File 'lib/pedant/check.rb', line 73
def self.requires
return []
end
|
.run_checks_in_dependency_order(kb, checks) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/pedant/check.rb', line 91
def self.run_checks_in_dependency_order(kb, checks)
fatal = false
run_checks = []
until checks.empty? || fatal
ready = checks.select { |cls| cls.ready?(kb) }
break if ready.empty?
ready.each do |cls|
chk = cls.new(kb)
checks.delete(cls)
chk.run
run_checks << chk
if chk.result == :fatal
fatal = true
break
end
end
end
run_checks
end
|
Instance Method Details
#fail ⇒ Object
150
151
152
|
# File 'lib/pedant/check.rb', line 150
def fail
@result = :fail
end
|
#fatal ⇒ Object
154
155
156
157
|
# File 'lib/pedant/check.rb', line 154
def fatal
report(:error, "This is a fatal error.")
@result = :fatal
end
|
#pass ⇒ Object
159
160
161
|
# File 'lib/pedant/check.rb', line 159
def pass
@result = :pass
end
|
#report(level, text = nil) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/pedant/check.rb', line 122
def report(level, text=nil)
unless text.nil?
if @@levels.index(level).nil?
raise "Reporting level #{level} is not known."
end
@report << [level, text]
return
end
level = @@levels.index(level) if level.is_a?(Symbol)
msg = @report.select { |l, t| @@levels.index(l) <= level }.map { |l, t| t }.join("\n")
msg << "\n" unless msg.empty?
msg = "[#{@@statuses[@result]}] #{self.class.friendly_name}\n#{msg}"
return msg
end
|
#skip ⇒ Object
163
164
165
|
# File 'lib/pedant/check.rb', line 163
def skip
@result = :skip
end
|
#warn ⇒ Object
167
168
169
|
# File 'lib/pedant/check.rb', line 167
def warn
@result = :warn
end
|