File: filter_loader.lua

package info (click to toggle)
lua-busted 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: sh: 198; makefile: 2
file content (159 lines) | stat: -rw-r--r-- 5,472 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
return function()
  local function filter(busted, options)
    local getFullName = function(name)
      local parent = busted.context.get()
      local names = { name }

      while parent and (parent.name or parent.descriptor) and
            parent.descriptor ~= 'file' do
        table.insert(names, 1, parent.name or parent.descriptor)
        parent = busted.context.parent(parent)
      end

      return table.concat(names, ' ')
    end

    local hasTag = function(name, tag)
      local found = name:find('#' .. tag)
      return (found ~= nil)
    end

    local filterExcludeTags = function(name)
      for i, tag in pairs(options.excludeTags) do
        if hasTag(name, tag) then
          return nil, false
        end
      end
      return nil, true
    end

    local filterTags = function(name)
      local fullname = getFullName(name)
      for i, tag in pairs(options.tags) do
        if hasTag(fullname, tag) then
          return nil, true
        end
      end
      return nil, (#options.tags == 0)
    end

    local filterOutNames = function(name)
      for _, filter in pairs(options.filterOut) do
        if getFullName(name):find(filter) ~= nil then
          return nil, false
        end
      end
      return nil, true
    end

    local excludeNames = {}
    if options.excludeNamesFile then
      for name in io.lines(options.excludeNamesFile) do
        table.insert(excludeNames, name)
      end
    end

    local excludeNamesFile = function(name)
      for _, filter in ipairs(excludeNames) do
        if getFullName(name) == filter then
          return nil, false
        end
      end
      return nil, true
    end

    local name = function(name)
      for _, candidate in pairs(options.name) do
        if string.find(candidate, getFullName(name), 1, true) then
          return nil, true
        end
      end
      return nil, (#options.name == 0)
    end

    local filterNames = function(name)
      for _, filter in pairs(options.filter) do
        if getFullName(name):find(filter) ~= nil then
          return nil, true
        end
      end
      return nil, (#options.filter == 0)
    end

    local printTestName = function(element, parent, status)
      if not (options.suppressPending and status == 'pending') then
        local fullname = getFullName()
        local trace = element.trace
        if trace and trace.what == 'Lua' then
          fullname = trace.short_src .. ':' .. trace.currentline .. ': ' .. fullname
        end
        print(fullname)
      end
      return nil, false
    end

    local ignoreAll = function()
      return nil, false
    end

    local noop = function() end
    local stubOut = function(descriptor, name, fn, ...)
      if fn == noop then
        return nil, true
      end
      busted.publish({ 'register', descriptor }, name, noop, ...)
      return nil, false
    end

    local skipOnError = function()
      return nil, not busted.skipAll
    end

    local applyFilter = function(descriptors, name, fn)
      if options[name] and options[name] ~= '' then
        for _, descriptor in ipairs(descriptors) do
          busted.subscribe({ 'register', descriptor }, fn, { priority = 1 })
        end
      end
    end

    local applyDescFilter = function(descriptors, name, fn)
      if options[name] and options[name] ~= '' then
        for _, descriptor in ipairs(descriptors) do
          local f = function(...) return fn(descriptor, ...) end
          busted.subscribe({ 'register', descriptor }, f, { priority = 1 })
        end
      end
    end

    if options.list then
      busted.subscribe({ 'suite', 'start' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'suite', 'end' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'file', 'start' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'file', 'end' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'describe', 'start' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'describe', 'end' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'test', 'start' }, ignoreAll, { priority = 1 })
      busted.subscribe({ 'test', 'end' }, printTestName, { priority = 1 })
      applyDescFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'list', stubOut)
      applyDescFilter({ 'lazy_setup', 'lazy_teardown' }, 'list', stubOut)
      applyDescFilter({ 'strict_setup', 'strict_teardown' }, 'list', stubOut)
      applyDescFilter({ 'it', 'pending' }, 'list', stubOut)
    end

    applyFilter({ 'lazy_setup', 'lazy_teardown' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'strict_setup', 'strict_teardown' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'setup', 'teardown', 'before_each', 'after_each' }, 'nokeepgoing', skipOnError)
    applyFilter({ 'file', 'describe', 'it', 'pending' }, 'nokeepgoing', skipOnError)

    -- The following filters are applied in reverse order
    applyFilter({ 'it', 'pending' }            , 'filter'          , filterNames           )
    applyFilter({ 'describe', 'it', 'pending' }, 'name'            , name                  )
    applyFilter({ 'describe', 'it', 'pending' }, 'filterOut'       , filterOutNames        )
    applyFilter({ 'describe', 'it', 'pending' }, 'excludeNamesFile', excludeNamesFile      )
    applyFilter({ 'it', 'pending' }            , 'tags'            , filterTags            )
    applyFilter({ 'describe', 'it', 'pending' }, 'excludeTags'     , filterExcludeTags     )
  end

  return filter
end