File: globbing_spec.lua

package info (click to toggle)
luacheck 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,704 kB
  • sloc: sh: 106; makefile: 96; python: 35
file content (80 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (5)
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
local globbing = require "luacheck.globbing"
local fs = require "luacheck.fs"

local cur_dir = fs.get_current_dir()

local function check_match(expected_result, glob, path)
   glob = fs.normalize(fs.join(cur_dir, glob))
   path = fs.normalize(fs.join(cur_dir, path))
   assert.equal(expected_result, globbing.match(glob, path))
end

describe("globbing", function()
   describe("match", function()
      it("returns true on literal match", function()
         check_match(true, "foo/bar", "foo/bar")
      end)

      it("returns true on literal match after normalization", function()
         check_match(true, "foo//bar/baz/..", "./foo/bar/")
      end)

      it("returns false for on literal mismatch", function()
         check_match(false, "foo/bar", "foo/baz")
      end)

      it("accepts subdirectory matches", function()
         check_match(true, "foo/bar", "foo/bar/baz")
      end)

      it("understands wildcards", function()
         check_match(true, "*", "foo")
         check_match(true, "foo/*r", "foo/bar")
         check_match(true, "foo/*r", "foo/bar/baz")
         check_match(false, "foo/*r", "foo/baz")
      end)

      it("understands optional characters", function()
         check_match(false, "?", "foo")
         check_match(true, "???", "foo")
         check_match(true, "????", "foo")
         check_match(true, "f?o/?a?", "foo/bar")
         check_match(false, "f?o/?a?", "foo/abc")
      end)

      it("understands ranges and classes", function()
         check_match(true, "[d-h]o[something]", "foo")
         check_match(false, "[d-h]o[somewhere]", "bar")
         check_match(false, "[.-h]o[i-z]", "bar")
      end)

      it("accepts closing bracket as first class character", function()
         check_match(true, "[]]", "]")
         check_match(false, "[]]", "[")
         check_match(true, "[]foo][]foo][]foo]", "foo")
      end)

      it("accepts dash as first or last class character", function()
         check_match(true, "[-]", "-")
         check_match(false, "[-]", "+")
         check_match(true, "[---]", "-")
      end)

      it("understands negation", function()
         check_match(true, "[!foo][!bar][!baz]", "boo")
         check_match(false, "[!foo][!bar][!baz]", "far")
         check_match(false, "[!a-z]", "g")
      end)

      it("understands recursive globbing using **", function()
         check_match(true, "**/*.lua", "foo.lua")
         check_match(true, "**/*.lua", "foo/bar.lua")
         check_match(false, "foo/**/*.lua", "bar.lua")
         check_match(false, "foo/**/*.lua", "foo.lua")
         check_match(true, "foo/**/bar/*.lua", "foo/bar/baz.lua")
         check_match(true, "foo/**/bar/*.lua", "foo/foo2/foo3/bar/baz.lua")
         check_match(false, "foo/**/bar/*.lua", "foo/baz.lua")
         check_match(false, "foo/**/bar/*.lua", "bar/baz.lua")
      end)
   end)
end)