# encoding: utf-8 require 'spec_helper' require 'fedux_org_stdlib/template_directory' RSpec.describe TemplateDirectory do context '#preferred_template_directory' do it 'has a default template file which is the preferred place to store the template' do touch_file '.config/my_application/templates/tests.d/test.tt' template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.preferred_template_directory).to eq File.expand_path('~/.config/my_application/templates/tests.d') end end it 'works with nested module names' do touch_file '.config/my_application/my_sub/templates/tests.d/test.tt' template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication::MySub' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.preferred_template_directory).to eq File.expand_path('~/.config/my_application/my_sub/templates/tests.d') end end end context 'template files' do it 'looks at ~/.config/my_application/templates/tests.d/test.tt' do write_file '.config/my_application/templates/tests.d/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.template_files).to include absolute_path('.config/my_application/templates/tests.d/test.tt') end end it 'looks at ~/.my_application/templates/tests.d/test.tt' do write_file '.my_application/templates/tests.d/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.template_files).to include absolute_path('.my_application/templates/tests.d/test.tt') end end it 'returns templates in subdirectories as well' do write_file '.my_application/templates/tests.d/blub/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.template_files).to include absolute_path('.my_application/templates/tests.d/blub/test.tt') end end end context '#template_files' do it 'returns template_files in template directory' do write_file '.my_application/templates/tests.d/test1.tt', <<-EOS.strip_heredoc <%= hello world %> EOS write_file '.my_application/templates/tests.d/test2.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(TemplateDirectory) do def class_name 'TestTemplateDirectory' end def module_name 'MyApplication' end end with_env 'HOME' => absolute_path('.') do template = template_klass.new expect(template.template_files).to include absolute_path('.my_application/templates/tests.d/test1.tt') expect(template.template_files).to include absolute_path('.my_application/templates/tests.d/test2.tt') end end end end