File: insulate-expose_spec.lua

package info (click to toggle)
lua-busted 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 820 kB
  • sloc: sh: 198; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (4)
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
assert.is_nil(package.loaded.pl)
assert.is_nil(package.loaded['pl.file'])

describe('Tests insulation', function()
  insulate('environment inside insulate', function()
    pl = require 'pl'
    _G.insuated_global = true

    it('updates insuated global table _G', function()
      assert.is_not_nil(insuated_global)
      assert.is_not_nil(_G.insuated_global)
    end)

    it('updates package.loaded', function()
      assert.is_not_nil(pl)
      assert.is_not_nil(Date)
      assert.is_not_nil(package.loaded.pl)
      assert.is_not_nil(package.loaded['pl.Date'])
    end)
  end)

  describe('environment after insulate', function()
    it('restores insuated global table _G', function()
      assert.is_nil(insuated_global)
      assert.is_nil(_G.insuated_global)
    end)

    it('restores package.loaded', function()
      assert.is_nil(pl)
      assert.is_nil(Date)
      assert.is_nil(package.loaded.pl)
      assert.is_nil(package.loaded['pl.Date'])
    end)
  end)
end)

insulate('Tests insulation with values set to nil', function()
  -- The tests in this block need to be run in order, as we're testing state recovery between tests.

  _G.some_global = true

  describe('environment before insulate', function()
    it('has global', function()
      assert.is_not_nil(some_global)
      assert.is_not_nil(_G.some_global)
    end)
  end)

  insulate('environment inside insulate', function()
    it('sets global to nil', function()
      _G.some_global = nil
      assert.is_nil(some_global)
      assert.is_nil(_G.some_global)
    end)
  end)

  describe('environment after insulate', function()
    it('has restored the global', function()
      assert.is_not_nil(some_global)
      assert.is_not_nil(_G.some_global)
    end)
  end)
end)

insulate('', function()
  describe('Tests expose', function()
    insulate('inside insulate block', function()
      expose('tests environment inside expose block', function()
        pl = require 'pl'
        exposed_global = true
        _G.global = true

        it('creates exposed global', function()
          assert.is_not_nil(exposed_global)
          assert.is_nil(_G.exposed_global)
        end)

        it('updates global table _G', function()
          assert.is_not_nil(global)
          assert.is_not_nil(_G.global)
        end)

        it('updates package.loaded', function()
          assert.is_not_nil(pl)
          assert.is_not_nil(Date)
          assert.is_not_nil(package.loaded.pl)
          assert.is_not_nil(package.loaded['pl.Date'])
        end)
      end)
    end)

    describe('neutralizes insulation', function()
      it('creates exposed global in outer block', function()
        assert.is_not_nil(exposed_global)
        assert.is_nil(_G.exposed_global)
      end)

      it('does not restore global table _G', function()
        assert.is_not_nil(global)
        assert.is_not_nil(_G.global)
      end)

      it('does not restore package.loaded', function()
        assert.is_not_nil(pl)
        assert.is_not_nil(Date)
        assert.is_not_nil(package.loaded.pl)
        assert.is_not_nil(package.loaded['pl.Date'])
      end)
    end)
  end)

  it('Tests exposed globals does not exist in outer most block', function()
    assert.is_nil(pl)
    assert.is_nil(exposed_global)
    assert.is_nil(_G.exposed_global)
  end)

  it('Tests global table _G persists without insulate', function()
    assert.is_not_nil(global)
    assert.is_not_nil(_G.global)
  end)

  it('Tests package.loaded persists without insulate', function()
    assert.is_not_nil(Date)
    assert.is_not_nil(package.loaded.pl)
    assert.is_not_nil(package.loaded['pl.Date'])
  end)
end)

describe('Tests after insulating an expose block', function()
  it('restores global table _G', function()
    assert.is_nil(global)
    assert.is_nil(_G.global)
  end)

  it('restores package.loaded', function()
    assert.is_nil(pl)
    assert.is_nil(Date)
    assert.is_nil(package.loaded.pl)
    assert.is_nil(package.loaded['pl.Date'])
  end)
end)

describe('Tests insulate/expose', function()
  local path = require 'pl.path'
  local utils = require 'pl.utils'
  local busted_cmd = path.is_windows and 'lua bin/busted' or 'bin/busted'

  local executeBusted = function(args)
    local success, exitcode, out, err = utils.executeex(busted_cmd .. ' ' .. args)
    if exitcode > 255 then
      exitcode = math.floor(exitcode/256), exitcode - math.floor(exitcode/256)*256
    end
    return not not success, exitcode, out, err
  end

  describe('file insulation', function()
    it('works between files', function()
      local success, exitcode = executeBusted('spec/insulate_file1.lua spec/insulate_file2.lua')
      assert.is_true(success)
      assert.is_equal(0, exitcode)
    end)

    it('works between files independent of order', function()
      local success, exitcode = executeBusted('spec/insulate_file2.lua spec/insulate_file1.lua')
      assert.is_true(success)
      assert.is_equal(0, exitcode)
    end)
  end)

  describe('expose from file context', function()
    it('works between files', function()
      local success, exitcode = executeBusted('spec/expose_file1.lua spec/expose_file2.lua')
      assert.is_true(success)
      assert.is_equal(0, exitcode)
    end)
  end)
end)