diff --git a/Gemfile b/Gemfile index 5aad2c4d..dfbbdfd9 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ group :test do gem "rspec-mocks" gem "nokogiri" gem "rspec" + gem "test-theme", path: File.expand_path("./test/fixtures/test-theme", File.dirname(__FILE__)) end # diff --git a/lib/jekyll/theme.rb b/lib/jekyll/theme.rb index 45a9aa3c..13bed36c 100644 --- a/lib/jekyll/theme.rb +++ b/lib/jekyll/theme.rb @@ -4,11 +4,11 @@ module Jekyll def initialize(name) @name = name.downcase.strip - raise MissingDependencyException unless gemspec + raise Jekyll::Errors::MissingDependencyException unless gemspec end def root - @root ||= gemspec.gem_dir + @root ||= gemspec.full_gem_path end def version @@ -34,7 +34,8 @@ module Jekyll private def path_for(folder) - Jekyll.sanitized_path root, "_#{folder}" + path = Jekyll.sanitized_path root, "_#{folder}" + path if Dir.exists?(path) end def gemspec diff --git a/test/fixtures/test-theme/_assets/application.coffee b/test/fixtures/test-theme/_assets/application.coffee new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-theme/_assets/script.js b/test/fixtures/test-theme/_assets/script.js new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-theme/_includes/include.html b/test/fixtures/test-theme/_includes/include.html new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-theme/_layouts/default.html b/test/fixtures/test-theme/_layouts/default.html new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-theme/_sass/style.scss b/test/fixtures/test-theme/_sass/style.scss new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/test-theme/test-theme.gemspec b/test/fixtures/test-theme/test-theme.gemspec new file mode 100644 index 00000000..5f950ae1 --- /dev/null +++ b/test/fixtures/test-theme/test-theme.gemspec @@ -0,0 +1,9 @@ +Gem::Specification.new do |s| + s.name = 'test-theme' + s.version = '0.1.0' + s.licenses = ['MIT'] + s.summary = "This is a theme used to test Jekyll" + s.authors = ["Jekyll"] + s.files = ["lib/example.rb"] + s.homepage = 'https://github.com/jekyll/jekyll' +end diff --git a/test/test_theme.rb b/test/test_theme.rb new file mode 100644 index 00000000..e996ac6f --- /dev/null +++ b/test/test_theme.rb @@ -0,0 +1,55 @@ +require 'helper' + +class TestTheme < JekyllUnitTest + def setup + @theme = Theme.new('test-theme') + @expected_root = File.expand_path "./fixtures/test-theme", File.dirname(__FILE__) + end + + context "initializing" do + should "normalize the theme name" do + theme = Theme.new(' Test-Theme ') + assert_equal "test-theme", theme.name + end + + should "know the theme root" do + assert_equal @expected_root, @theme.root + end + + should "know the theme version" do + assert_equal Gem::Version.new("0.1.0"), @theme.version + end + + should "raise an error for invalid themes" do + assert_raises Jekyll::Errors::MissingDependencyException do + Theme.new("foo") + end + end + end + + context "path generation" do + ["assets", "layouts", "includes", "sass"].each do |folder| + should "know the #{folder} path" do + expected = File.expand_path("_#{folder}", @expected_root) + assert_equal expected, @theme.public_send("#{folder}_path") + end + end + + should "generate folder paths" do + expected = File.expand_path("_assets", @expected_root) + assert_equal expected, @theme.send(:path_for, "assets") + end + + should "not allow paths outside of the theme root" do + assert_equal nil, @theme.send(:path_for, "../../source") + end + + should "return nil for paths that don't exist" do + assert_equal nil, @theme.send(:path_for, "foo") + end + end + + should "retrieve the gemspec" do + assert_equal "test-theme-0.1.0", @theme.send(:gemspec).full_name + end +end