Skip to content

Commit

Permalink
ThemeAssetsReader: fix tests so everything passes.
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Sep 18, 2016
1 parent 6d7f305 commit 74baeb8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Lint/UnreachableCode:
Lint/UselessAccessModifier:
Enabled: false
Metrics/AbcSize:
Max: 20
Max: 21
Metrics/ClassLength:
Exclude:
- !ruby/regexp /features\/.*.rb$/
Expand Down
2 changes: 1 addition & 1 deletion features/theme.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: Writing themes
Scenario: A theme with SCSS
Given I have a configuration file with "theme" set to "test-theme"
And I have a css directory
And I have a "css/main.scss" page that contains "@import 'style';"
And I have a "css/main.scss" page that contains "@import 'test-theme-black';"
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
Expand Down
10 changes: 5 additions & 5 deletions lib/jekyll/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def initialize(site, base, dir, name)
@base = base
@dir = dir
@name = name
if site.in_theme_dir(base) == base # we're in a theme
@path = site.in_theme_dir(base, dir, name)
else
@path = site.in_source_dir(base, dir, name)
end
@path = if site.in_theme_dir(base) == base # we're in a theme
site.in_theme_dir(base, dir, name)
else
site.in_source_dir(base, dir, name)
end

process(name)
read_yaml(File.join(base, dir), name)
Expand Down
38 changes: 27 additions & 11 deletions lib/jekyll/readers/theme_assets_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ def read
if File.symlink?(path)
Jekyll.logger.warn "Theme reader:", "Ignored symlinked asset: #{path}"
else
base = site.theme.root
dir = File.dirname(path.sub("#{site.theme.root}/", ""))
name = File.basename(path)
relative_path = File.join(*[dir, name].compact)
if Utils.has_yaml_header?(path)
next if site.pages.any? { |file| file.relative_path == relative_path }
site.pages << Jekyll::Page.new(site, base, dir, name)
else
next if site.static_files.any? { |file| file.relative_path == relative_path }
site.static_files << Jekyll::StaticFile.new(site, base, dir, name)
end
read_theme_asset(path)
end
end
end

private
def read_theme_asset(path)
base = site.theme.root
dir = File.dirname(path.sub("#{site.theme.root}/", ""))
name = File.basename(path)

if Utils.has_yaml_header?(path)
append_unless_exists site.pages,
Jekyll::Page.new(site, base, dir, name)
else
append_unless_exists site.static_files,
Jekyll::StaticFile.new(site, base, dir, name)
end
end

def append_unless_exists(haystack, new_item)
if haystack.any? { |file| file.relative_path == new_item.relative_path }
Jekyll.logger.debug "Theme:",
"Ignoring #{new_item.relative_path} in theme due to existing file " \
"with that path in site."
return
end

haystack << new_item
end
end
end
3 changes: 3 additions & 0 deletions test/fixtures/test-theme/_sass/test-theme-red.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sample {
color: red;
}
2 changes: 1 addition & 1 deletion test/fixtures/test-theme/assets/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
---
@import "test-theme-{{ site.theme-color }}";
@import "test-theme-{{ site.theme-color | default: "red" }}";
2 changes: 1 addition & 1 deletion test/test_theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setup
[:assets, :_layouts, :_includes, :_sass].each do |folder|
should "know the #{folder} path" do
expected = File.expand_path(folder.to_s, @expected_root)
assert_equal expected, @theme.public_send("#{folder}_path")
assert_equal expected, @theme.public_send("#{folder.to_s.tr("_", "")}_path")
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/test_theme_assets_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
class TestThemeAssetsReader < JekyllUnitTest
def setup
@site = fixture_site(
"theme" => "test-theme",
"theme" => "test-theme",
"theme-color" => "black"
)
assert @site.theme
end

def assert_file_with_relative_path(haystack, relative_path)
assert haystack.any? { |f|
f.relative_path == relative_path
}, "Site should read in the #{relative_path} file, but it was not found in #{haystack.inspect}"
assert haystack.any? { |f|
f.relative_path == relative_path
}, "Site should read in the #{relative_path} file, " \
"but it was not found in #{haystack.inspect}"
end

def refute_file_with_relative_path(haystack, relative_path)
refute haystack.any? { |f|
f.relative_path == relative_path
}, "Site should not have read in the #{relative_path} file, but it was found in #{haystack.inspect}"
refute haystack.any? { |f|
f.relative_path == relative_path
}, "Site should not have read in the #{relative_path} file, " \
"but it was found in #{haystack.inspect}"
end

context "with a valid theme" do
Expand Down Expand Up @@ -55,7 +57,5 @@ def refute_file_with_relative_path(haystack, relative_path)
refute_file_with_relative_path @site.static_files, "assets/img/logo.png"
refute_file_with_relative_path @site.pages, "assets/style.scss"
end

end

end

0 comments on commit 74baeb8

Please sign in to comment.