From 3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Fri, 25 Jul 2014 13:23:54 -0500 Subject: Fail profiler fast when input is not a ruby file [ci skip] --- tools/profile | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tools') diff --git a/tools/profile b/tools/profile index 62daa2f0bd..a35dd18b77 100755 --- a/tools/profile +++ b/tools/profile @@ -6,9 +6,11 @@ ENV['RAILS_ENV'] ||= 'development' module CodeTools class Profiler + Error = Class.new(StandardError) attr_reader :path, :mode def initialize(path, mode=nil) + assert_ruby_file_exists(path) @path, @mode = path, mode require 'benchmark' end @@ -60,6 +62,16 @@ module CodeTools puts "%8.1f ms %d KB RSS" % [elapsed * 1000, after_rss - before_rss] end + private + + def assert_ruby_file_exists(path) + fail Error.new("No such file") unless File.exists?(path) + fail Error.new("#{path} is a directory") if File.directory?(path) + ruby_extension = File.extname(path) == '.rb' + ruby_executable = File.open(path, 'rb') {|f| f.readline } =~ [/\A#!.*ruby/] + fail Error.new("Not a ruby file") unless ruby_extension or ruby_executable + end + module RequireProfiler private def require(file, *args) RequireProfiler.profile(file) { super } end -- cgit v1.2.3