From ab29963ee4e18a0ab04e4ef236dcaf41d8b7b5f1 Mon Sep 17 00:00:00 2001 From: Fletcher Nichol Date: Sun, 15 Jan 2012 19:09:52 -0700 Subject: [PATCH] Expand template path under user's home directory. * File.join won't expand `~` into `ENV['HOME']` (http://ruby-doc.org/core-1.9.3/File.html#method-c-expand_path) * The FakeFS File.exists? implementation calls FileSystem#find (https://github.com/defunkt/fakefs/blob/master/lib/fakefs/file_system.rb#L22-33) containing a call to FileSystem#normalize_path which expands the path variable passed in (https://github.com/defunkt/fakefs/blob/master/lib/fakefs/file_system.rb#L91-98) * The file system mocking library sets up a false expectation that `~` will be expanded in the #export_template method and consequently the production code can't use the template directory * To guard against future regressions such as fixes/updates to FakeFS or using an alternate file system mocking library, the specs were updated to explicitly set `ENV['HOME']` --- lib/foreman/export/base.rb | 2 +- spec/foreman/export/upstart_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/foreman/export/base.rb b/lib/foreman/export/base.rb index 1c38c1f..7bebd43 100644 --- a/lib/foreman/export/base.rb +++ b/lib/foreman/export/base.rb @@ -26,7 +26,7 @@ private ###################################################################### def export_template(exporter, file, template_root) if template_root && File.exist?(file_path = File.join(template_root, file)) File.read(file_path) - elsif File.exist?(file_path = File.join("~/.foreman/templates", file)) + elsif File.exist?(file_path = File.expand_path(File.join("~/.foreman/templates", file))) File.read(file_path) else File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__)) diff --git a/spec/foreman/export/upstart_spec.rb b/spec/foreman/export/upstart_spec.rb index 62b3d91..6ad23c1 100644 --- a/spec/foreman/export/upstart_spec.rb +++ b/spec/foreman/export/upstart_spec.rb @@ -38,13 +38,19 @@ describe Foreman::Export::Upstart do end context "with alternate templates from home dir" do - let(:default_template_root) {File.expand_path("~/.foreman/templates")} + let(:default_template_root) {File.expand_path("#{ENV['HOME']}/.foreman/templates")} before do + ENV['_FOREMAN_SPEC_HOME'] = ENV['HOME'] + ENV['HOME'] = "/home/appuser" FileUtils.mkdir_p default_template_root File.open("#{default_template_root}/master.conf.erb", "w") { |f| f.puts "default_alternate_template" } end + after do + ENV['HOME'] = ENV.delete('_FOREMAN_SPEC_HOME') + end + it "can export with alternate template files" do upstart.export("/tmp/init")