File: test_path_utils.rb

package info (click to toggle)
ruby-sprockets 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ruby: 13,014; javascript: 157; makefile: 4
file content (308 lines) | stat: -rw-r--r-- 12,278 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# frozen_string_literal: true
require 'minitest/autorun'
require 'sprockets/path_utils'

class TestPathUtils < Minitest::Test
  include Sprockets::PathUtils

  DOSISH = File::ALT_SEPARATOR != nil
  DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
  DOSISH_UNC = File.dirname("//") == "//"

  def test_stat
    assert_kind_of File::Stat, stat(File.expand_path("../fixtures", __FILE__))
    refute stat("/tmp/sprockets/missingfile")
  end

  def test_file
    assert_equal true, file?(File.expand_path("../fixtures/default/hello.txt", __FILE__))
    assert_equal false, file?(File.expand_path("../fixtures", __FILE__))
  end

  def test_directory
    assert_equal true, directory?(File.expand_path("../fixtures", __FILE__))
    assert_equal false, directory?(File.expand_path("../fixtures/default/hello.txt", __FILE__))
  end

  def test_entries
    assert_equal [
      "asset",
      "compass",
      "context",
      "default",
      "directives",
      "double",
      "encoding",
      "errors",
      "index-assets",
      "manifest_utils",
      "octicons",
      "paths",
      "public",
      "resolve",
      "sass",
      "server",
      "source-maps",
      "symlink"
    ], entries(File.expand_path("../fixtures", __FILE__))

    [ ['a', 'b'], ['a', 'b', '.', '..'] ].each do |dir_contents|
      Dir.stub :entries, dir_contents do
        assert_equal ['a', 'b'], entries(Dir.tmpdir)
      end
    end

    assert_equal [], entries("/tmp/sprockets/missingdir")
  end

  def test_check_absolute_path
    assert absolute_path?(Dir.pwd)

    assert absolute_path?("/foo.rb")
    refute absolute_path?("foo.rb")
    refute absolute_path?("./foo.rb")
    refute absolute_path?("../foo.rb")

    if DOSISH_DRIVE_LETTER
      assert absolute_path?("A:foo.rb")
      assert absolute_path?("A:/foo.rb")
      assert absolute_path?("A:\\foo.rb")
    end

    if DOSISH
      assert absolute_path?("/foo.rb")
      assert absolute_path?("\\foo.rb")
    end
  end

  def test_check_relative_path
    assert relative_path?(".")
    assert relative_path?("..")
    assert relative_path?("./")
    assert relative_path?("../")
    assert relative_path?("./foo.rb")
    assert relative_path?("../foo.rb")

    if DOSISH
      assert relative_path?(".\\")
      assert relative_path?("..\\")
      assert relative_path?(".\\foo.rb")
      assert relative_path?("..\\foo.rb")
    end

    refute relative_path?(Dir.pwd)
    refute relative_path?("/foo.rb")
    refute relative_path?("foo.rb")
    refute relative_path?(".foo.rb")
    refute relative_path?("..foo.rb")
  end

  def test_split_subpath_from_root_path
    path = File.expand_path("../fixtures/default", __FILE__)

    subpath = File.expand_path("../fixtures/default/application.js", __FILE__)
    assert_equal "application.js", split_subpath(path, subpath)

    subpath = File.expand_path("../fixtures/default/application.js", __FILE__)
    assert_equal "application.js", split_subpath(path + "/", subpath)

    subpath = File.expand_path("../fixtures/default/app/application.js", __FILE__)
    assert_equal "app/application.js", split_subpath(path, subpath)

    assert_nil split_subpath(path, nil)

    subpath = File.expand_path("../fixtures/default", __FILE__)
    assert_equal "", split_subpath(path, subpath)

    subpath = File.expand_path("../fixtures/other/app/application.js", __FILE__)
    refute split_subpath(path, subpath)
  end

  def test_split_paths_root_from_base
    paths = [File.expand_path("../fixtures/default", __FILE__)]

    filename = File.expand_path("../fixtures/default/application.js", __FILE__)
    expected = [paths.first, "application.js"]
    assert_equal expected, paths_split(paths, filename)

    filename = File.expand_path("../fixtures/default/app/application.js", __FILE__)
    expected = [paths.first, "app/application.js"]
    assert_equal expected, paths_split(paths, filename)

    filename = File.expand_path("../fixtures/default", __FILE__)
    expected = [paths.first, ""]
    assert_equal expected, paths_split(paths, filename)

    filename = File.expand_path("../fixtures/other/app/application.js", __FILE__)
    refute paths_split(paths, filename)
  end

  def test_path_extensions
    assert_equal [".txt"], path_extnames("hello.txt")
    assert_equal [".txt"], path_extnames("sub/hello.txt")
    assert_equal [".txt"], path_extnames("sub.dir/hello.txt")
    assert_equal [".js"], path_extnames("jquery.js")
    assert_equal [".min", ".js"], path_extnames("jquery.min.js")
    assert_equal [".js", ".erb"], path_extnames("jquery.js.erb")
    assert_equal [".min", ".js", ".erb"], path_extnames("jquery.min.js.erb")
  end

  def test_match_path_extname
    extensions = { ".txt" => "text/plain" }
    assert_equal [".txt", "text/plain"], match_path_extname("hello.txt", extensions)
    assert_equal [".txt", "text/plain"], match_path_extname("sub/hello.txt", extensions)
    refute match_path_extname("hello.text", extensions)

    extensions = { ".js" => "application/javascript" }
    assert_equal [".js", "application/javascript"], match_path_extname("jquery.js", extensions)
    assert_equal [".js", "application/javascript"], match_path_extname("jquery.min.js", extensions)
    refute match_path_extname("jquery.js.erb", extensions)
    refute match_path_extname("jquery.min.js.erb", extensions)

    extensions = { ".js" => "application/javascript", ".js.erb" => "application/javascript+ruby" }
    assert_equal [".js", "application/javascript"], match_path_extname("jquery.js", extensions)
    assert_equal [".js", "application/javascript"], match_path_extname("jquery.min.js", extensions)
    assert_equal [".js.erb", "application/javascript+ruby"], match_path_extname("jquery.js.erb", extensions)
    assert_equal [".js.erb", "application/javascript+ruby"], match_path_extname("jquery.min.js.erb", extensions)
    refute match_path_extname("jquery.min.coffee.erb", extensions)

    extensions = { ".js.map" => "application/json", ".css.map" => "application/json" }
    assert_equal [".js.map", "application/json"], match_path_extname("jquery.js.map", extensions)
    assert_equal [".js.map", "application/json"], match_path_extname("jquery.min.js.map", extensions)
    assert_equal [".css.map", "application/json"], match_path_extname("jquery-ui.css.map", extensions)
    assert_equal [".css.map", "application/json"], match_path_extname("jquery-ui.min.css.map", extensions)
    refute match_path_extname("jquery.map", extensions)
    refute match_path_extname("jquery.map.js", extensions)
    refute match_path_extname("jquery.map.css", extensions)

    extensions = { ".coffee" => "application/coffeescript", ".js" => "application/javascript", ".js.jsx.coffee" => "application/jsx+coffee" }
    assert_equal [".js.jsx.coffee", "application/jsx+coffee"], match_path_extname("component.js.jsx.coffee", extensions)
  end

  def test_find_matching_path_for_extensions
    dirname = File.expand_path("../fixtures/default", __FILE__)

    extensions = { ".js" => "application/javascript", ".coffee" => "text/coffeescript" }
    assert_equal [
      ["#{dirname}/application.coffee", "text/coffeescript"]
    ], find_matching_path_for_extensions(dirname, "application", extensions)

    extensions = { ".txt" => "text/plain", ".jst.ejs" => "application/ejs" }
    assert_equal [
      ["#{dirname}/hello.jst.ejs", "application/ejs"],
      ["#{dirname}/hello.txt", "text/plain"]
    ], find_matching_path_for_extensions(dirname, "hello", extensions)

  end

  def test_path_parents
    root = File.expand_path("../..", __FILE__)

    assert_kind_of Array, path_parents(File.expand_path(__FILE__))

    assert_equal ["#{root}/test", root],
      path_parents(File.expand_path(__FILE__), root)
    assert_equal ["#{root}/test", root],
      path_parents("#{root}/test/fixtures/", root)
    assert_equal ["#{root}/test/fixtures", "#{root}/test", root],
      path_parents("#{root}/test/fixtures/default", root)
    assert_equal ["#{root}/test/fixtures/default", "#{root}/test/fixtures", "#{root}/test", root],
      path_parents("#{root}/test/fixtures/default/POW.png", root)

    assert_equal ["#{root}/test/fixtures/default", "#{root}/test/fixtures", "#{root}/test"],
      path_parents("#{root}/test/fixtures/default/POW.png", "#{root}/test")
    assert_equal ["#{root}/test/fixtures/default"],
      path_parents("#{root}/test/fixtures/default/POW.png", "#{root}/test/fixtures/default")
  end

  def test_find_upwards
    root = File.expand_path("../..", __FILE__)

    assert_equal "#{root}/Gemfile",
      find_upwards("Gemfile", File.expand_path(__FILE__))
    assert_equal "#{root}/Gemfile",
      find_upwards("Gemfile", "#{root}/test/fixtures/")
    assert_equal "#{root}/Gemfile",
      find_upwards("Gemfile", "#{root}/test/fixtures/default/POW.png")

    assert_equal "#{root}/test/sprockets_test.rb",
      find_upwards("sprockets_test.rb", "#{root}/test/fixtures/default/POW.png")
  end

  FILES_IN_SERVER = Dir["#{File.expand_path("../fixtures/server", __FILE__)}/*"]

  def test_stat_directory
    files = stat_directory(File.expand_path("../fixtures/server", __FILE__)).to_a
    assert_equal FILES_IN_SERVER.size, files.size
    path, stat = stat_directory(File.expand_path("../fixtures/server", __FILE__)).first
    assert_equal File.expand_path("../fixtures/server/app", __FILE__), path
    assert_kind_of File::Stat, stat

    assert_equal [], stat_directory(File.expand_path("../fixtures/missing", __FILE__)).to_a
  end

  def test_stat_tree
    files = stat_tree(File.expand_path("../fixtures/asset/tree/all", __FILE__)).to_a
    assert_equal 11, files.size

    path, stat = files.first
    assert_equal File.expand_path("../fixtures/asset/tree/all/README.md", __FILE__), path
    assert_kind_of File::Stat, stat

    assert_equal [
      File.expand_path("../fixtures/asset/tree/all/README.md", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c/d.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c/e.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b.css", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b.js.erb", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d/c.coffee", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d/e.js", __FILE__)
    ], files.map(&:first)

    assert_equal [], stat_tree("#{File.expand_path("../fixtures", __FILE__)}/missing").to_a
  end

  def test_stat_sorted_tree
    files = stat_sorted_tree(File.expand_path("../fixtures/asset/tree/all", __FILE__)).to_a
    assert_equal 11, files.size

    path, stat = files.first
    assert_equal File.expand_path("../fixtures/asset/tree/all/README.md", __FILE__), path
    assert_kind_of File::Stat, stat

    assert_equal [
      File.expand_path("../fixtures/asset/tree/all/README.md", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b.css", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b.js.erb", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c/d.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/b/c/e.js", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d/c.coffee", __FILE__),
      File.expand_path("../fixtures/asset/tree/all/d/e.js", __FILE__)
    ], files.map(&:first)

    assert_equal [], stat_tree(File.expand_path("../fixtures/missing", __FILE__)).to_a
  end

  def test_atomic_write_without_errors
    filename = "atomic.file"
    begin
      contents = "Atomic Text"
      atomic_write(filename) do |file|
        file.write(contents)
        assert !File.exist?(filename)
      end
      assert File.exist?(filename)
      assert_equal contents, File.read(filename)
    ensure
      File.unlink(filename) rescue nil
    end
  end
end