Changeset 88

Show
Ignore:
Timestamp:
04/16/08 22:25:45 (7 months ago)
Author:
jweiss
Message:

apply #77 with modifications: check syntax of recipes

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.txt

    r87 r88  
    11 
    22* SVN * 
     3 
     4* Check syntax of recipes. Provided by Mathias Meyer 
    35 
    46* Ajax-Preview on recipe edit. Provided by Mathias Meyer 
  • trunk/app/models/recipe.rb

    r51 r88  
    99   
    1010  tz_time_attributes :created_at, :updated_at 
     11   
     12  
     13 def validate 
     14   check_syntax 
     15 end 
     16  
     17  def check_syntax 
     18   return if self.body.blank? 
     19 
     20   result = "" 
     21   Open4::popen4 "ruby -wc" do |pid, stdin, stdout, stderr| 
     22     stdin.write self.body 
     23     stdin.close 
     24     output = stdout.read 
     25     errors = stderr.read 
     26     result = output.any? ? output : errors 
     27   end 
     28 
     29   unless result == "Syntax OK" 
     30     line = $1.to_i if result =~ /^-:(\d+):/ 
     31     errors.add(:body, "syntax error at line: #{line}") unless line.nil? 
     32   end 
     33  rescue => e 
     34    RAILS_DEFAULT_LOGGER.error "Error while validating recipe syntax of recipe #{self.id}: #{e.inspect} - #{e.backtrace.join("\n")}" 
     35  end 
     36  
    1137end 
  • trunk/test/unit/recipe_test.rb

    r1 r88  
    5151    assert !recipe.valid? 
    5252  end 
     53 
     54  def test_validate_invalid_syntax_on_create 
     55    recipe = Recipe.create(:name => "Copy Config files", 
     56                           :description => "Recipe body intentionally erronous", 
     57                           :body => "set config_files, database.yml'") 
     58    assert !recipe.valid? 
     59    assert_equal "syntax error at line: 1", recipe.errors.on(:body) 
     60  end 
    5361   
     62  def test_validate_valid_syntax_on_create 
     63    recipe = Recipe.create(:name => "Copy Config files", 
     64                           :description => "Recipe body intentionally erronous", 
     65                           :body => "set :config_files, 'database.yml'") 
     66    assert !recipe.errors.on(:body) 
     67  end 
     68   
     69  def test_validate_with_open4_error 
     70    Open4.expects(:popen4).raises(RuntimeError) 
     71    recipe = Recipe.create(:name => "Copy Config files", 
     72                           :description => "Recipe body intentionally erronous", 
     73                           :body => "set :config_files, 'database.yml'") 
     74    assert !recipe.errors.on(:body) 
     75  end 
    5476end