forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_entry_filter.rb
170 lines (137 loc) · 5.59 KB
/
test_entry_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# frozen_string_literal: true
require "helper"
class TestEntryFilter < JekyllUnitTest
context "Filtering entries" do
setup do
@site = fixture_site
end
should "filter entries" do
ent1 = %w(foo.markdown bar.markdown baz.markdown #baz.markdown#
.baz.markdown foo.markdown~ .htaccess _posts _pages ~$benbalter.docx)
entries = EntryFilter.new(@site).filter(ent1)
assert_equal %w(foo.markdown bar.markdown baz.markdown .htaccess), entries
end
should "allow regexp filtering" do
files = %w(README.md)
@site.exclude = [
%r!README!,
]
assert_empty @site.reader.filter_entries(
files
)
end
should "filter entries with exclude" do
excludes = %w(README TODO vendor/bundle)
files = %w(index.html site.css .htaccess vendor)
@site.exclude = excludes + ["exclude*"]
assert_equal files, @site.reader.filter_entries(excludes + files + ["excludeA"])
end
should "filter entries with exclude relative to site source" do
excludes = %w(README TODO css)
files = %w(index.html vendor/css .htaccess)
@site.exclude = excludes
assert_equal files, @site.reader.filter_entries(excludes + files + ["css"])
end
should "filter excluded directory and contained files" do
excludes = %w(README TODO css)
files = %w(index.html .htaccess)
@site.exclude = excludes
assert_equal(
files,
@site.reader.filter_entries(
excludes + files + ["css", "css/main.css", "css/vendor.css"]
)
)
end
should "not filter entries within include" do
includes = %w(_index.html .htaccess include*)
files = %w(index.html _index.html .htaccess includeA)
@site.include = includes
assert_equal files, @site.reader.filter_entries(files)
end
should "not exclude explicitly included entry" do
entries = %w(README TODO css .htaccess _movies/.)
excludes = %w(README TODO css)
includes = %w(README .htaccess)
@site.exclude = excludes
@site.include = includes
filtered_entries = EntryFilter.new(@site).filter(entries)
assert_equal %w(README .htaccess), filtered_entries
end
should "keep safe symlink entries when safe mode enabled" do
allow(File).to receive(:symlink?).with("symlink.js").and_return(true)
files = %w(symlink.js)
assert_equal files, @site.reader.filter_entries(files)
end
should "not filter symlink entries when safe mode disabled" do
allow(File).to receive(:symlink?).with("symlink.js").and_return(true)
files = %w(symlink.js)
assert_equal files, @site.reader.filter_entries(files)
end
should "filter symlink pointing outside site source" do
ent1 = %w(_includes/tmp)
entries = EntryFilter.new(@site).filter(ent1)
assert_equal %w(), entries
end
should "include only safe symlinks in safe mode" do
# no support for symlinks on Windows
skip_if_windows "Jekyll does not currently support symlinks on Windows."
site = fixture_site("safe" => true)
site.reader.read_directories("symlink-test")
assert_equal %w(main.scss symlinked-file).length, site.pages.length
refute_equal [], site.static_files
end
should "include symlinks in unsafe mode" do
# no support for symlinks on Windows
skip_if_windows "Jekyll does not currently support symlinks on Windows."
@site.reader.read_directories("symlink-test")
refute_equal [], @site.pages
refute_equal [], @site.static_files
end
should "include only safe symlinks in safe mode even when included" do
# no support for symlinks on Windows
skip_if_windows "Jekyll does not currently support symlinks on Windows."
site = fixture_site("safe" => true, "include" => ["symlinked-file-outside-source"])
site.reader.read_directories("symlink-test")
assert_equal %w(main.scss symlinked-file).length, site.pages.length
refute_includes site.static_files.map(&:name), "symlinked-file-outside-source"
end
end
context "#glob_include?" do
setup do
@site = Site.new(site_configuration)
@filter = EntryFilter.new(@site)
end
should "return false with no glob patterns" do
refute @filter.glob_include?([], "a.txt")
end
should "return false with all not match path" do
data = ["a*", "b?"]
refute @filter.glob_include?(data, "ca.txt")
refute @filter.glob_include?(data, "ba.txt")
end
should "return true with match path" do
data = ["a*", "b?", "**/a*"]
assert @filter.glob_include?(data, "a.txt")
assert @filter.glob_include?(data, "ba")
assert @filter.glob_include?(data, "c/a/a.txt")
assert @filter.glob_include?(data, "c/a/b/a.txt")
end
should "match even if there is no leading slash" do
data = ["vendor/bundle"]
assert @filter.glob_include?(data, "/vendor/bundle")
assert @filter.glob_include?(data, "vendor/bundle")
end
should "match even if there is no trailing slash" do
data = ["/vendor/bundle/", "vendor/ruby"]
assert @filter.glob_include?(data, "vendor/bundle/jekyll/lib/page.rb")
assert @filter.glob_include?(data, "/vendor/ruby/lib/set.rb")
end
should "match directory only if there is trailing slash" do
data = ["_glob_include_test/_is_dir/", "_glob_include_test/_not_dir/"]
assert @filter.glob_include?(data, "_glob_include_test/_is_dir")
assert @filter.glob_include?(data, "_glob_include_test/_is_dir/include_me.txt")
refute @filter.glob_include?(data, "_glob_include_test/_not_dir")
end
end
end