method
validate
v2_5_5 -
Show latest stable
- Class:
Gem::Specification
validate(packaging = true)public
Checks that the specification contains all required fields, and does a very basic sanity check.
Raises InvalidSpecificationException if the spec does not pass the checks..
# File lib/rubygems/specification.rb, line 2680
def validate packaging = true
@warnings = 0
require 'rubygems/user_interaction'
extend Gem::UserInteraction
normalize
nil_attributes = self.class.non_nil_attributes.find_all do |attrname|
instance_variable_get("@#{attrname}").nil?
end
unless nil_attributes.empty? then
raise Gem::InvalidSpecificationException,
"#{nil_attributes.join ', '} must not be nil"
end
if packaging and rubygems_version != Gem::VERSION then
raise Gem::InvalidSpecificationException,
"expected RubyGems version #{Gem::VERSION}, was #{rubygems_version}"
end
@@required_attributes.each do |symbol|
unless self.send symbol then
raise Gem::InvalidSpecificationException,
"missing value for attribute #{symbol}"
end
end
if !name.is_a?(String) then
raise Gem::InvalidSpecificationException,
"invalid value for attribute name: \"#{name.inspect}\" must be a string"
elsif name !~ /[a-zA-Z]/ then
raise Gem::InvalidSpecificationException,
"invalid value for attribute name: #{name.dump} must include at least one letter"
elsif name !~ VALID_NAME_PATTERN then
raise Gem::InvalidSpecificationException,
"invalid value for attribute name: #{name.dump} can only include letters, numbers, dashes, and underscores"
end
if raw_require_paths.empty? then
raise Gem::InvalidSpecificationException,
'specification must have at least one require_path'
end
@files.delete_if { |x| File.directory?(x) && !File.symlink?(x) }
@test_files.delete_if { |x| File.directory?(x) && !File.symlink?(x) }
@executables.delete_if { |x| File.directory?(File.join(@bindir, x)) }
@extra_rdoc_files.delete_if { |x| File.directory?(x) && !File.symlink?(x) }
@extensions.delete_if { |x| File.directory?(x) && !File.symlink?(x) }
non_files = files.reject { |x| File.file?(x) || File.symlink?(x) }
unless not packaging or non_files.empty? then
raise Gem::InvalidSpecificationException,
"[\"#{non_files.join "\", \""}\"] are not files"
end
if files.include? file_name then
raise Gem::InvalidSpecificationException,
"#{full_name} contains itself (#{file_name}), check your files list"
end
unless specification_version.is_a?(Integer)
raise Gem::InvalidSpecificationException,
'specification_version must be an Integer (did you mean version?)'
end
case platform
when Gem::Platform, Gem::Platform::RUBY then # ok
else
raise Gem::InvalidSpecificationException,
"invalid platform #{platform.inspect}, see Gem::Platform"
end
self.class.array_attributes.each do |field|
val = self.send field
klass = case field
when :dependencies
Gem::Dependency
else
String
end
unless Array === val and val.all? { |x| x.kind_of?(klass) } then
raise(Gem::InvalidSpecificationException,
"#{field} must be an Array of #{klass}")
end
end
[:authors].each do |field|
val = self.send field
raise Gem::InvalidSpecificationException, "#{field} may not be empty" if
val.empty?
end
unless Hash === metadata
raise Gem::InvalidSpecificationException,
'metadata must be a hash'
end
validate_metadata
licenses.each { |license|
if license.length > 64
raise Gem::InvalidSpecificationException,
"each license must be 64 characters or less"
end
if !Gem::Licenses.match?(license)
suggestions = Gem::Licenses.suggestions(license)
message = license value '#{license}' is invalid. Use a license identifier fromhttp://spdx.org/licenses or '#{Gem::Licenses::NONSTANDARD}' for a nonstandard license.
message += "Did you mean #{suggestions.map { |s| "'#{s}'"}.join(', ')}?\n" unless suggestions.nil?
warning(message)
end
}
warning licenses is empty, but is recommended. Use a license identifier fromhttp://spdx.org/licenses or '#{Gem::Licenses::NONSTANDARD}' for a nonstandard license. if licenses.empty?
validate_permissions
# reject lazy developers:
lazy = '"FIxxxXME" or "TOxxxDO"'.gsub(/xxx/, '')
unless authors.grep(/FI XME|TO DO/).empty? then
raise Gem::InvalidSpecificationException, "#{lazy} is not an author"
end
unless Array(email).grep(/FI XME|TO DO/).empty? then
raise Gem::InvalidSpecificationException, "#{lazy} is not an email"
end
if description =~ /FI XME|TO DO/ then
raise Gem::InvalidSpecificationException, "#{lazy} is not a description"
end
if summary =~ /FI XME|TO DO/ then
raise Gem::InvalidSpecificationException, "#{lazy} is not a summary"
end
# Make sure a homepage is valid HTTP/HTTPS URI
if homepage and not homepage.empty?
begin
homepage_uri = URI.parse(homepage)
unless [URI::HTTP, URI::HTTPS].member? homepage_uri.class
raise Gem::InvalidSpecificationException, "\"#{homepage}\" is not a valid HTTP URI"
end
rescue URI::InvalidURIError
raise Gem::InvalidSpecificationException, "\"#{homepage}\" is not a valid HTTP URI"
end
end
# Warnings
]author homepage summary files].each do |attribute|
value = self.send attribute
warning "no #{attribute} specified" if value.nil? or value.empty?
end
if description == summary then
warning 'description and summary are identical'
end
# TODO: raise at some given date
warning "deprecated autorequire specified" if autorequire
executables.each do |executable|
executable_path = File.join(bindir, executable)
shebang = File.read(executable_path, 2) == '#!'
warning "#{executable_path} is missing #! line" unless shebang
end
files.each do |file|
next unless File.symlink?(file)
warning "#{file} is a symlink, which is not supported on all platforms"
end
validate_dependencies
true
ensure
if $! or @warnings > 0 then
alert_warning "See http://guides.rubygems.org/specification-reference/ for help"
end
end Related methods
- Instance methods
- <=>
- ==
- _dump
- abbreviate
- activate
- activate_dependencies
- add_bindir
- add_dependency
- add_development_dependency
- add_runtime_dependency
- add_self_to_load_path
- author
- author=
- authors
- authors=
- base_dir
- bin_dir
- bin_file
- build_args
- build_extensions
- build_info_dir
- build_info_file
- bundled_gem_in_old_ruby?
- cache_dir
- cache_file
- conficts_when_loaded_with?
- conflicts
- date
- date=
- default_executable
- default_value
- dependencies
- dependent_gems
- dependent_specs
- description=
- development_dependencies
- doc_dir
- encode_with
- eql?
- executable
- executable=
- executables
- executables=
- extensions
- extensions=
- extra_rdoc_files
- extra_rdoc_files=
- file_name
- files
- files=
- for_cache
- full_name
- gem_dir
- gems_dir
- has_conflicts?
- has_rdoc
- has_rdoc=
- has_rdoc?
- has_unit_tests?
- hash
- init_with
- initialize_copy
- inspect
- installed_by_version
- installed_by_version=
- internal_init
- lib_files
- license
- license=
- licenses
- licenses=
- mark_version
- method_missing
- missing_extensions?
- name_tuple
- normalize
- original_name
- original_platform
- platform
- platform=
- pretty_print
- raise_if_conflicts
- raw_require_paths
- rdoc_options
- rdoc_options=
- require_path
- require_path=
- require_paths=
- required_ruby_version=
- required_rubygems_version=
- requirements
- requirements=
- reset_nil_attributes_to_default
- respond_to_missing?
- ri_dir
- runtime_dependencies
- sanitize
- sanitize_string
- satisfies_requirement?
- sort_obj
- source
- spec_dir
- spec_file
- spec_name
- stubbed?
- summary=
- test_file
- test_file=
- test_files
- test_files=
- to_ruby
- to_ruby_for_cache
- to_s
- to_spec
- to_yaml
- traverse
- validate
- validate_dependencies
- validate_metadata
- validate_permissions
- version=
- warning
- yaml_initialize
- Class methods
- _all
- _clear_load_cache
- _latest_specs
- _load
- _resort!
- add_spec
- add_specs
- all
- all=
- all_names
- array_attributes
- attribute_names
- dirs
- dirs=
- each
- each_gemspec
- each_spec
- find_active_stub_by_path
- find_all_by_full_name
- find_all_by_name
- find_by_name
- find_by_path
- find_in_unresolved
- find_in_unresolved_tree
- find_inactive_by_path
- from_yaml
- latest_specs
- load
- load_defaults
- new
- non_nil_attributes
- normalize_yaml_input
- outdated
- outdated_and_latest_version
- remove_spec
- required_attribute?
- required_attributes
- reset
- stubs
- stubs_for
- unresolved_deps
- Private methods
-
default_stubs -
gemspec_stubs_in -
installed_stubs -
map_stubs -
sort_by! -
uniq_by -
add_dependency_with_type -
check_version_conflict -
find_all_satisfiers -
invalidate_memoized_attributes -
ruby_code -
same_attributes?