Skip to content

Commit

Permalink
Merge pull request #678 from hanami/fix/eliminate-preload-dependency-…
Browse files Browse the repository at this point in the history
…for-environment-rake-task

Ensure `:environment` Rake task to load the entire project
  • Loading branch information
jodosha committed Nov 30, 2016
2 parents 670d231 + e3b57b0 commit 18ea4e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/hanami/rake_helper.rb
Expand Up @@ -21,8 +21,10 @@ def self.install_tasks
# rubocop:disable Metrics/MethodLength
def install
desc "Load the full project"
task environment: :preload do
Components.resolve('apps')
task :environment do
require 'hanami/environment'
Hanami::Environment.new.require_project_environment
Components.resolve('all')
end

# Ruby ecosystem compatibility
Expand Down
19 changes: 19 additions & 0 deletions spec/isolation/rake/environment_spec.rb
@@ -0,0 +1,19 @@
RSpec.describe "Rake: environment", type: :cli do
it "loads the project" do
with_project do
generate_migrations
generate_model "user"
hanami "db prepare"

append "Rakefile", <<-EOF
task database_counts: :environment do
puts "users: \#{UserRepository.new.all.count}"
end
EOF

bundle_exec "rake database_counts"

expect(out).to eq("users: 0")
end
end
end
7 changes: 7 additions & 0 deletions spec/support/files.rb
Expand Up @@ -33,6 +33,13 @@ def unshift(path, line)
rewrite(path, content)
end

def append(path, contents)
content = ::File.readlines(path)
content << "#{contents}\n"

rewrite(path, content)
end

def open(path, mode, *content)
::File.open(path, mode) do |file|
file.write(Array(content).flatten.join)
Expand Down

0 comments on commit 18ea4e4

Please sign in to comment.