aboutsummaryrefslogtreecommitdiffstats
path: root/spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb
blob: fd0a3efe14053778f53ccf63882239ce20b35882 (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
class ConstantsLockFile
  LOCK_FILE_NAME = '.mspec.constants'

  def self.load
    if File.exist?(LOCK_FILE_NAME)
      File.readlines(LOCK_FILE_NAME).map(&:chomp)
    else
      []
    end
  end

  def self.dump(ary)
    contents = ary.map(&:to_s).uniq.sort.join("\n") + "\n"
    File.write(LOCK_FILE_NAME, contents)
  end
end

class ConstantLeakError < StandardError
end

class ConstantsLeakCheckerAction
  def initialize(save)
    @save = save
    @check = !save
    @constants_locked = ConstantsLockFile.load
    @exclude_patterns = MSpecScript.get(:toplevel_constants_excludes) || []
  end

  def register
    MSpec.register :start, self
    MSpec.register :before, self
    MSpec.register :after, self
    MSpec.register :finish, self
  end

  def start
    @constants_start = constants_now
  end

  def before(state)
    @constants_before = constants_now
  end

  def after(state)
    constants = remove_excludes(constants_now - @constants_before - @constants_locked)

    if @check && !constants.empty?
      MSpec.protect 'Constants leak check' do
        raise ConstantLeakError, "Top level constants leaked: #{constants.join(', ')}"
      end
    end
  end

  def finish
    constants = remove_excludes(constants_now - @constants_start - @constants_locked)

    if @save
      ConstantsLockFile.dump(@constants_locked + constants)
    end

    if @check && !constants.empty?
      MSpec.protect 'Global constants leak check' do
        raise ConstantLeakError, "Top level constants leaked in the whole test suite: #{constants.join(', ')}"
      end
    end
  end

  private

  def constants_now
    Object.constants.map(&:to_s)
  end

  def remove_excludes(constants)
    constants.reject { |name|
      @exclude_patterns.any? { |pattern| pattern === name }
    }
  end
end