@@ -45,10 +45,10 @@ def run
4545 config [ :cookbook_path ] = Chef ::Config [ :cookbook_path ]
4646 end
4747
48- if config [ :all ]
48+ if config [ :all ]
4949 cl = Chef ::CookbookLoader . new
50- cl . each do |cookbook |
51- test_cookbook ( cookbook . name . to_s )
50+ cl . each do |key , cookbook |
51+ test_cookbook ( key )
5252 end
5353 else
5454 @name_args . each do |cb |
@@ -60,42 +60,21 @@ def run
6060 def test_cookbook ( cookbook )
6161 Chef ::Log . info ( "Running syntax check on #{ cookbook } " )
6262 Array ( config [ :cookbook_path ] ) . reverse . each do |path |
63- cookbook_dir = File . expand_path ( File . join ( path , cookbook ) )
64- test_ruby ( cookbook_dir )
65- test_templates ( cookbook_dir )
63+ syntax_checker = Chef :: Cookbook :: SyntaxCheck . for_cookbook ( cookbook , path )
64+ test_ruby ( syntax_checker )
65+ test_templates ( syntax_checker )
6666 end
6767 end
6868
69- def test_ruby ( cookbook_dir )
70- cache = Chef ::Cache ::Checksum . instance
71- Dir [ File . join ( cookbook_dir , '**' , '*.rb' ) ] . each do |ruby_file |
72- key = cache . generate_key ( ruby_file , "chef-test" )
73- fstat = File . stat ( ruby_file )
7469
75- if cache . lookup_checksum ( key , fstat )
76- Chef ::Log . info ( "No change in checksum of #{ ruby_file } " )
77- else
78- Chef ::Log . info ( "Testing #{ ruby_file } for syntax errors..." )
79- Chef ::Mixin ::Command . run_command ( :command => "ruby -c #{ ruby_file } " , :output_on_failure => true )
80- cache . generate_checksum ( key , ruby_file , fstat )
81- end
82- end
70+ def test_ruby ( syntax_checker )
71+ Chef ::Log . info ( "Validating ruby files" )
72+ exit ( 1 ) unless syntax_checker . validate_ruby_files
8373 end
84-
85- def test_templates ( cookbook_dir )
86- cache = Chef ::Cache ::Checksum . instance
87- Dir [ File . join ( cookbook_dir , '**' , '*.erb' ) ] . each do |erb_file |
88- key = cache . generate_key ( erb_file , "chef-test" )
89- fstat = File . stat ( erb_file )
90-
91- if cache . lookup_checksum ( key , fstat )
92- Chef ::Log . info ( "No change in checksum of #{ erb_file } " )
93- else
94- Chef ::Log . info ( "Testing template #{ erb_file } for syntax errors..." )
95- Chef ::Mixin ::Command . run_command ( :command => "sh -c 'erubis -x #{ erb_file } | ruby -c'" , :output_on_failure => true )
96- cache . generate_checksum ( key , erb_file , fstat )
97- end
98- end
74+
75+ def test_templates ( syntax_checker )
76+ Chef ::Log . info ( "Validating templates" )
77+ exit ( 1 ) unless syntax_checker . validate_templates
9978 end
10079
10180 end
0 commit comments