# 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 with_environment 'HOME' => working_directory do create_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 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 with_environment 'HOME' => working_directory do create_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 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 with_environment 'HOME' => working_directory do file = create_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 template = template_klass.new expect(template.template_files).to include file end end it 'looks at ~/.my_application/templates/tests.d/test.tt' do with_environment 'HOME' => working_directory do file = create_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 template = template_klass.new expect(template.template_files).to include file end end it 'returns templates in subdirectories as well' do with_environment 'HOME' => working_directory do file = create_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 template = template_klass.new expect(template.template_files).to include file end end end context '#template_files' do it 'returns template_files in template directory' do with_environment 'HOME' => working_directory do file1 = create_file '.my_application/templates/tests.d/test1.tt', <<-EOS.strip_heredoc <%= hello world %> EOS file2 = create_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 template = template_klass.new expect(template.template_files).to include file1 expect(template.template_files).to include file2 end end end end