| 
									
										
										
										
											2018-09-02 23:30:07 +02:00
										 |  |  | require "rubocops/extend/formula_cop" | 
					
						
							| 
									
										
										
										
											2017-09-04 13:47:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							|  |  |  |       class ClassName < FormulaCop | 
					
						
							|  |  |  |         DEPRECATED_CLASSES = %w[
 | 
					
						
							|  |  |  |           GithubGistFormula | 
					
						
							|  |  |  |           ScriptFileFormula | 
					
						
							|  |  |  |           AmazonWebServicesFormula | 
					
						
							|  |  |  |         ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, parent_class_node, _body_node) | 
					
						
							|  |  |  |           parent_class = class_name(parent_class_node) | 
					
						
							|  |  |  |           return unless DEPRECATED_CLASSES.include?(parent_class) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 13:47:05 +05:30
										 |  |  |           problem "#{parent_class} is deprecated, use Formula instead" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def autocorrect(node) | 
					
						
							|  |  |  |           lambda do |corrector| | 
					
						
							|  |  |  |             corrector.replace(node.source_range, "Formula") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-08-15 19:29:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |       class TestCalls < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							| 
									
										
										
										
											2018-08-16 16:38:41 -04:00
										 |  |  |           test = find_block(body_node, :test) | 
					
						
							|  |  |  |           return unless test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           test_calls(test) do |node, params| | 
					
						
							| 
									
										
										
										
											2018-08-15 19:29:22 -04:00
										 |  |  |             p1, p2 = params | 
					
						
							|  |  |  |             if match = string_content(p1).match(%r{(/usr/local/(s?bin))}) | 
					
						
							|  |  |  |               offending_node(p1) | 
					
						
							|  |  |  |               problem "use \#{#{match[2]}} instead of #{match[1]} in #{node}" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if node == :shell_output && node_equals?(p2, 0) | 
					
						
							|  |  |  |               offending_node(p2) | 
					
						
							|  |  |  |               problem "Passing 0 to shell_output() is redundant" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 12:42:45 -04:00
										 |  |  |         def autocorrect(node) | 
					
						
							|  |  |  |           lambda do |corrector| | 
					
						
							|  |  |  |             case node.type | 
					
						
							|  |  |  |             when :str, :dstr | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |               corrector.replace(node.source_range, | 
					
						
							|  |  |  |                                 node.source.to_s.sub(%r{(/usr/local/(s?bin))}, | 
					
						
							|  |  |  |                                                      '#{\2}')) | 
					
						
							| 
									
										
										
										
											2018-08-16 12:42:45 -04:00
										 |  |  |             when :int | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |               corrector.remove( | 
					
						
							|  |  |  |                 range_with_surrounding_comma( | 
					
						
							|  |  |  |                   range_with_surrounding_space(range: node.source_range, | 
					
						
							|  |  |  |                                                side: :left), | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |               ) | 
					
						
							| 
									
										
										
										
											2018-08-16 12:42:45 -04:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 19:29:22 -04:00
										 |  |  |         def_node_search :test_calls, <<~EOS | 
					
						
							|  |  |  |           (send nil? ${:system :shell_output :pipe_output} $...) | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-09-04 13:47:05 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     module FormulaAuditStrict | 
					
						
							| 
									
										
										
										
											2018-08-07 03:04:42 +01:00
										 |  |  |       # - `test do ..end` should be meaningfully defined in the formula | 
					
						
							| 
									
										
										
										
											2017-09-04 13:47:05 +05:30
										 |  |  |       class Test < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							| 
									
										
										
										
											2018-08-07 03:04:42 +01:00
										 |  |  |           test = find_block(body_node, :test) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           unless test | 
					
						
							|  |  |  |             problem "A `test do` test block should be added" | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if test.body.nil? | 
					
						
							|  |  |  |             problem "`test do` should not be empty" | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return unless test.body.single_line? && | 
					
						
							|  |  |  |                         test.body.source.to_s == "true" | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 03:04:42 +01:00
										 |  |  |           problem "`test do` should contain a real test" | 
					
						
							| 
									
										
										
										
											2017-09-04 13:47:05 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |