Sha256: 15ed202881c40b6bf71122ca2f79fd696ea2786d184b65c4deb62882aa6d8090
Contents?: true
Size: 1.8 KB
Versions: 17
Compression:
Stored size: 1.8 KB
Contents
require "bundler/gem_tasks" require "rspec/core/rake_task" require "logger" @logger = Logger.new(STDOUT) RSpec::Core::RakeTask.new(:spec) desc "Check if source can be required and is correctly required" task :require do dir = File.dirname(__FILE__) gemspec = load_gemspec(dir) # Order is important, assert_can_be_required should be run first assert_can_be_required(dir, gemspec) check_source_files_required(dir, gemspec) check_source_files_included(dir, gemspec) end def load_gemspec(dir) require "bundler/setup" Gem::Specification.find do |spec| spec.full_gem_path.include?(File.dirname(__FILE__)) end end def check_source_files_required(dir, gemspec) require_gem(gemspec) ruby_files = Dir.glob("#{dir}/**/*.rb").reject { |f| f.include?("/spec/") } required_ruby_files = $LOADED_FEATURES.select { |f| f.include?(dir) } (ruby_files - required_ruby_files).each do |file| @logger.warn("Source file not required when loading gem: #{file.sub("#{dir}/", '')}") end end def check_source_files_included(dir, gemspec) ruby_files_relative = Dir.glob("#{dir}/**/*.rb").reject { |f| f.include?("/spec/") }.map { |f| f.sub("#{dir}/", "") } (ruby_files_relative - gemspec.files.select { |f| f.end_with?(".rb") }).each do |file| @logger.warn("File ignored when building gem because it's not added to git: #{file}") end end def assert_can_be_required(dir, gemspec) local_load_paths = $LOAD_PATH.select { |path| path.include?(dir) } $LOAD_PATH.reject! { |path| local_load_paths.include?(path) } begin require_gem(gemspec) rescue LoadError => e @logger.error("Gem source cannot be required relatively: #{e}") raise ensure $LOAD_PATH.push(*local_load_paths) end end def require_gem(gemspec) require_relative "lib/#{gemspec.name.gsub('-', '/')}" end task default: [:require, :spec]
Version data entries
17 entries across 17 versions & 1 rubygems