aboutsummaryrefslogtreecommitdiff
path: root/.vim/test/autoload/lib_test.rb
blob: e39280c2b8c4fa14975c81c312eb5feb3c9bab89 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
require_relative "../helper"
require "json"

describe "Lib" do
  include WithTemporaryDirectory

  before do
    # Some tests may change the working directory, so reset it to a known good.
    $vim.command "cd #{Dir.getwd}"
    Dir.mkdir File.join(@dir, "node_modules")
  end

  def set_node_version(version)
    executable = File.join(@dir, "node")
    touch executable, <<-end.strip
      #!/bin/sh
      [ $# != 1 -o "$1" != --version  ] && exit 1
      echo "v#{version}"
    end
    File.chmod 0755, executable
    $vim.command(%(let $PATH = "#@dir:" . $PATH))
  end

  describe "node#lib#find" do
    def find(name)
      path = $vim.echo(%(node#lib#find("#{name}", expand("%"))))
      File.exists?(path) ? File.realpath(path) : path
    end

    it "must return ./README before ./README.js" do
      target = touch File.join(@dir, "README")
      touch File.join(@dir, "README.js")

      $vim.edit File.join(@dir, "index.js")
      find("./README").must_equal target
    end

    it "must return ./README.txt relative to file" do
      target = touch File.join(@dir, "lib", "README.txt")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("./README.txt").must_equal target
    end

    it "must return ../README.txt" do
      target = touch File.join(@dir, "README.txt")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("../README.txt").must_equal target
    end

    it "must return /.../README.txt" do
      target = touch File.join(@dir, "README.txt")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("#@dir/README.txt").must_equal target
    end

    it "must return ./other.js given ./other" do
      target = touch File.join(@dir, "other.js")
      $vim.edit File.join(@dir, "index.js")
      find("./other").must_equal target
    end

    it "must return ./other.jsx given ./other" do
      target = touch File.join(@dir, "other.jsx")
      $vim.edit File.join(@dir, "index.js")
      find("./other").must_equal target
    end

    it "must return ./other.es given ./other" do
      target = touch File.join(@dir, "other.es")
      $vim.edit File.join(@dir, "index.js")
      find("./other").must_equal target
    end

    it "must return ./other.js given ./other relative to file" do
      target = touch File.join(@dir, "lib", "other.js")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("./other").must_equal target
    end

    it "must return ./other.js before ./other/index.js given ./other" do
      target = touch File.join(@dir, "other.js")
      touch File.join(@dir, "other", "index.js")
      $vim.edit File.join(@dir, "index.js")
      find("./other").must_equal target
    end

    it "must return ../other.js given ../other" do
      target = touch File.join(@dir, "other.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("../other").must_equal target
    end

    it "must return /.../other.js given /.../other" do
      target = touch File.join(@dir, "other.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("#@dir/other").must_equal target
    end

    it "must return ./package.json given ./package" do
      target = touch File.join(@dir, "package.json")
      $vim.edit File.join(@dir, "index.js")
      find("./package").must_equal target
    end

    it "must return ./index.js given ." do
      target = touch File.join(@dir, "index.js")
      $vim.edit File.join(@dir, "other.js")
      find(".").must_equal target
    end

    it "must return ./index.js given ./" do
      target = touch File.join(@dir, "index.js")
      $vim.edit File.join(@dir, "other.js")
      find("./").must_equal target
    end

    it "must not find ./index/index.js given ./" do
      touch File.join(@dir, "index", "index.js")
      $vim.edit File.join(@dir, "other.js")
      $vim.echo(%(empty(node#lib#find("./", expand("%"))))).must_equal "1"
    end

    it "must not find ./.js given ./" do
      touch File.join(@dir, ".js")
      $vim.edit File.join(@dir, "other.js")
      $vim.echo(%(empty(node#lib#find("./", expand("%"))))).must_equal "1"
    end

    it "must return ../index.js given .." do
      target = touch File.join(@dir, "index.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "other.js")
      find("..").must_equal target
    end

    it "must return ../index.js given ../" do
      target = touch File.join(@dir, "index.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "other.js")
      find("../").must_equal target
    end

    it "must return /.../index.js given /..." do
      target = touch File.join(@dir, "index.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("#@dir").must_equal target
    end

    it "must return /.../index.js given /.../" do
      target = touch File.join(@dir, "index.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "index.js")
      find("#@dir/").must_equal target
    end

    it "must return ./lib/index.js given ./lib" do
      target = touch File.join(@dir, "lib", "index.js")
      $vim.edit File.join(@dir, "index.js")
      find("./lib").must_equal target
    end

    it "must return ./lib/other.js given ./lib with main" do
      target = touch File.join(@dir, "lib", "other.js")
      touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "other")
      $vim.edit File.join(@dir, "index.js")
      find("./lib").must_equal target
    end

    it "must return ./lib/index.js given ./lib with empty main" do
      target = touch File.join(@dir, "lib", "index.js")
      touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "")
      $vim.edit File.join(@dir, "index.js")
      find("./lib").must_equal target
    end

    it "must return ./lib/index.js given ./lib with non-existent main" do
      target = touch File.join(@dir, "lib", "index.js")
      touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "new")
      $vim.edit File.join(@dir, "index.js")
      find("./lib").must_equal target
    end

    it "must return ./other.js before ./other/index.js given . with main" do
      touch File.join(@dir, "package.json"), JSON.dump(:main => "other")
      target = touch File.join(@dir, "other.js")
      touch File.join(@dir, "other", "index.js")

      $vim.edit File.join(@dir, "index.js")
      find(".").must_equal target
    end

    it "must return node_modules/foo/index.js given foo" do
      index = File.join(@dir, "node_modules", "foo", "index.js")
      touch index
      $vim.edit File.join(@dir, "index.js")
      find("foo").must_equal index
    end

    it "must return node_modules/foo/other.js given foo/other" do
      other = File.join(@dir, "node_modules", "foo", "other.js")
      touch other
      $vim.edit File.join(@dir, "index.js")
      find("foo/other").must_equal other
    end

    it "must return node_modules/foo/other.js given foo/other.js" do
      other = File.join(@dir, "node_modules", "foo", "other.js")
      touch other
      $vim.edit File.join(@dir, "index.js")
      find("foo/other.js").must_equal other
    end

    # When package.json refers to a regular file.
    it "must return node_modules/foo/other.js given foo with main" do
      mod = File.join(@dir, "node_modules", "foo")
      touch File.join(mod, "package.json"), JSON.dump(:main => "./other.js")
      target = touch File.join(mod, "other.js")

      $vim.edit File.join(@dir, "index.js")
      find("foo").must_equal target
    end

    # When package.json refers to a directory.
    it "must return node_modules/foo/lib/index.js given foo with main as ./lib" do
      mod = File.join(@dir, "node_modules", "foo")
      touch File.join(mod, "package.json"), JSON.dump(:main => "./lib")
      target = touch File.join(mod, "lib/index.js")

      $vim.edit File.join(@dir, "index.js")
      find("foo").must_equal target
    end

    it "must return node_modules/foo/lib/index.js given foo with main as lib" do
      mod = File.join(@dir, "node_modules", "foo")
      touch File.join(mod, "package.json"), JSON.dump(:main => "lib")
      target = touch File.join(mod, "lib/index.js")

      $vim.edit File.join(@dir, "index.js")
      find("foo").must_equal target
    end

    it "must return node_modules/@scope/foo/index.js given @scope/foo" do
      target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
      touch target
      $vim.edit File.join(@dir, "index.js")
      find("@scope/foo").must_equal target
    end

    it "must return empty when looking for nothing" do
      $vim.edit File.join(@dir, "index.js")
      $vim.echo(%(empty(node#lib#find("", expand("%"))))).must_equal "1"
    end

    it "must return empty when nothing found" do
      $vim.edit File.join(@dir, "index.js")
      $vim.echo(%(empty(node#lib#find("new", expand("%"))))).must_equal "1"
    end

    it "must return URL for core module for current Node version" do
      set_node_version "0.13.37"
      $vim.edit File.join(@dir, "index.js")
      url = "https://rawgit.com/nodejs/node/v0.13.37/lib/assert.js"
      find("assert").must_equal url
    end

    it "must return URL for core module on master if no Node version" do
      touch File.join(@dir, "node"), "#!/bin/sh\nexit 1"
      File.chmod 0755, File.join(@dir, "node")
      $vim.edit File.join(@dir, "index.js")
      $vim.command(%(let $PATH = "#@dir:" . $PATH))
      url = "https://rawgit.com/nodejs/node/master/lib/assert.js"
      find("assert").must_equal url
    end

    it "must return URL for node.js for current Node version" do
      set_node_version "0.13.37"
      $vim.edit File.join(@dir, "index.js")
      url = "https://rawgit.com/nodejs/node/v0.13.37/src/node.js"
      find("node").must_equal url
    end

    it "must return URL for node.js on master if no Node version" do
      touch File.join(@dir, "node"), "#!/bin/sh\nexit 1"
      File.chmod 0755, File.join(@dir, "node")
      $vim.edit File.join(@dir, "index.js")
      $vim.command(%(let $PATH = "#@dir:" . $PATH))
      url = "https://rawgit.com/nodejs/node/master/src/node.js"
      find("node").must_equal url
    end
  end

  describe "node#lib#glob" do
    require "json"

    before do
      $vim.edit File.join(@dir, "index.js")
    end

    def glob(arg = "")
      # Because of possible locale and filesystem case-sensitiveness
      # differences, sort the output explicitly to be resistant.
      JSON.parse($vim.echo(%(node#lib#glob("#{arg}"))).gsub("'", '"')).sort
    end

    describe "given nothing" do
      it "must return files and directories" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README.txt")
        Dir.mkdir File.join(@dir, "test")

        files = %w[./README.txt ./index.js ./test/]
        glob.must_equal (CORE_MODULES + files).sort
      end

      it "must return modules and core modules" do
        FileUtils.mkpath File.join(@dir, "node_modules", "require-guard")
        FileUtils.mkpath File.join(@dir, "node_modules", "export")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul")
        glob.must_equal (CORE_MODULES + %w[export/ require-guard/ soul/]).sort
      end

      it "must return core modules without slashes" do
        glob.must_equal CORE_MODULES
        glob.wont_equal /\//
      end

      # Even though node.js is the bootstrapper file in src/.
      it "must return \"node\" as one of those core modules" do
        glob.must_include "node"
      end

      it "must return files, directories and modules" do
        FileUtils.mkpath File.join(@dir, "node_modules", "export")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul")
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README.txt")
        Dir.mkdir File.join(@dir, "test")

        files = %w[./README.txt ./index.js ./test/ export/ soul/]
        glob.must_equal (CORE_MODULES + files).sort
      end

      it "must not return the node_modules directory" do
        FileUtils.mkpath File.join(@dir, "node_modules")
        glob.must_equal CORE_MODULES
      end
    end

    describe "given absolute path" do
      it "must return files and directories given /" do
        files = Dir.entries("/")
        files.reject! {|f| f =~ /^\./ }
        files.sort!
        files.map! {|f| "/" + f }
        files.map! {|f| File.directory?(f) ? f + "/" : f }

        glob("/").must_equal files
      end

      it "must return files and directories given /.../" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README")
        Dir.mkdir File.join(@dir, "test")

        files = %W[#@dir/README #@dir/index.js #@dir/node_modules/ #@dir/test/]
        glob(@dir).must_equal files
      end

      it "must return files and directories given /.../test" do
        touch File.join(@dir, "test", "index_test.js")
        touch File.join(@dir, "test", "helpers.js")
        files = %W[#@dir/test/helpers.js #@dir/test/index_test.js]
        glob(File.join(@dir, "test")).must_equal files
      end

      it "must not return modules along with files" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README")
        Dir.mkdir File.join(@dir, "test")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul")

        files = %W[#@dir/README #@dir/index.js #@dir/node_modules/ #@dir/test/]
        glob(@dir).must_equal files
      end
    end

    describe "given relative path" do
      it "must return files and directories given ." do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README")
        Dir.mkdir File.join(@dir, "test")
        glob(".").must_equal %W[./README ./index.js ./test/]
      end

      it "must return files and directories given ./" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README")
        Dir.mkdir File.join(@dir, "test")
        glob("./").must_equal %W[./README ./index.js ./test/]
      end

      it "must return files and directories given .//" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README.txt")
        Dir.mkdir File.join(@dir, "test")
        glob(".//").must_equal %W[.//README.txt .//index.js .//test/]
      end

      it "must return files and directories given .///" do
        touch File.join(@dir, "index.js")
        touch File.join(@dir, "README.txt")
        Dir.mkdir File.join(@dir, "test")
        glob(".///").must_equal %W[.///README.txt .///index.js .///test/]
      end

      it "must return files and directories given ./test" do
        touch File.join(@dir, "test", "test.js")
        touch File.join(@dir, "test", "helpers.js")
        glob("./test/").must_equal %W[./test/helpers.js ./test/test.js]
      end
    end

    describe "given module name" do
      it "must return files and directories given soul" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        touch File.join(@dir, "node_modules", "soul", "README")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
        glob("soul").must_equal %w[soul/README soul/index.js soul/test/]
      end

      it "must return files and directories given soul/" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
        glob("soul/").must_equal %w[soul/index.js soul/test/]
      end

      it "must return files and directories given soul//" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
        glob("soul//").must_equal %w[soul//index.js soul//test/]
      end

      it "must return files and directories given soul///" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
        glob("soul///").must_equal %w[soul///index.js soul///test/]
      end

      it "must return files and directories given soul/test" do
        touch File.join(@dir, "node_modules", "soul", "test", "test.js")
        touch File.join(@dir, "node_modules", "soul", "test", "helpers.js")
        glob("soul/test").must_equal %w[soul/test/helpers.js soul/test/test.js]
      end
    end
  end

  describe "node#lib#version" do
    it "should return current Node's version" do
      set_node_version "0.13.37"
      $vim.echo("node#lib#version()").must_equal "0.13.37"
    end
  end
end