File: test-spawn.lua

package info (click to toggle)
awesome 4.3-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,468 kB
  • sloc: ansic: 14,508; sh: 526; makefile: 46
file content (343 lines) | stat: -rw-r--r-- 11,867 bytes parent folder | download
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
--- Tests for spawn

local runner = require("_runner")
local spawn = require("awful.spawn")

local spawns_done = 0
local async_spawns_done = 0
local exit_yay, exit_snd = nil, nil

-- * Using spawn with array is already covered by the test client.
-- * spawn with startup notification is covered by test-spawn-snid.lua

local tiny_client = function(class)
    return {"lua", "-e", [[
local lgi = require 'lgi'
local Gtk = lgi.require('Gtk')
local class = ']]..class..[['

Gtk.init()

window = Gtk.Window {
    default_width  = 100,
    default_height = 100,
    title          = 'title',
}

window:set_wmclass(class, class)

local app = Gtk.Application {}

function app:on_activate()
    window.application = self
    window:show_all()
end

app:run {''}
]]}
end

local matcher_called = false

local steps = {
    function()
        -- Test various error conditions. There are quite a number of them...
        local error_message

        error_message = spawn("this_does_not_exist_and_should_fail")
        assert(string.find(error_message, 'No such file or directory'), error_message)

        error_message = spawn({"this_does_not_exist_and_should_fail"})
        assert(string.find(error_message, 'No such file or directory'), error_message)

        error_message = spawn("foo '")
        assert(string.find(error_message, 'parse error: Text ended before matching quote was found'), error_message)

        error_message = spawn()
        assert(string.find(error_message, 'No command to execute'), error_message)

        error_message = spawn(" ")
        assert(string.find(error_message, 'Text was empty'), error_message)

        error_message = spawn("")
        assert(string.find(error_message, 'No command to execute'), error_message)

        error_message = spawn{}
        assert(string.find(error_message, 'There is nothing to execute'), error_message)

        return true
    end,

    function(count)
        if count == 1 then
            spawn.easy_async("echo yay", function(stdout)
                if stdout:match("yay") then
                    async_spawns_done = async_spawns_done + 1
                end
            end)
            spawn.easy_async_with_shell("true && echo yay", function(stdout)
                if stdout:match("yay") then
                    async_spawns_done = async_spawns_done + 1
                end
            end)
            local steps_yay = 0
            spawn.with_line_callback("echo yay", {
                                     stdout = function(line)
                                         assert(line == "yay", "line == '" .. tostring(line) .. "'")
                                         assert(steps_yay == 0)
                                         steps_yay = steps_yay + 1
                                     end,
                                     output_done = function()
                                         assert(steps_yay == 1)
                                         steps_yay = steps_yay + 1
                                         spawns_done = spawns_done + 1
                                     end,
                                     exit = function(reason, code)
                                         assert(reason == "exit")
                                         assert(exit_yay == nil)
                                         assert(code == 0)
                                         exit_yay = code
                                     end
                                 })

            -- Test that setting env vars works and that the env is cleared
            local read_line = false
            local pid, _, _, stdout = awesome.spawn({ "sh", "-c", "echo $AWESOME_SPAWN_TEST_VAR $HOME $USER" },
                    false, false, true, false, nil, { "AWESOME_SPAWN_TEST_VAR=42" })
            assert(type(pid) ~= "string", pid)
            spawn.read_lines(require("lgi").GioUnix.InputStream.new(stdout, true),
                    function(line)
                        assert(not read_line)
                        read_line = true
                        assert(line == "42", line)
                        spawns_done = spawns_done + 1
                    end, nil, true)

            -- Test error in parse_table_array.
            pid = awesome.spawn({"true"}, false, false, true, false, nil, { 0 })
            assert(pid == 'spawn: environment parse error: Non-string argument at table index 1', pid)

            -- Test error in parse_command.
            pid = awesome.spawn({0}, false, false, true, false, nil, {})
            assert(pid == 'spawn: parse error: Non-string argument at table index 1', pid)


            local steps_count = 0
            local err_count = 0
            spawn.with_line_callback({ "sh", "-c", "printf line1\\\\nline2\\\\nline3 ; echo err >&2 ; exit 42" }, {
                                     stdout = function(line)
                                         assert(steps_count < 3)
                                         steps_count = steps_count + 1
                                         assert(line == "line" .. steps_count, "line == '" .. tostring(line) .. "'")
                                     end,
                                     stderr = function(line)
                                         assert(err_count == 0)
                                         err_count = err_count + 1
                                         assert(line == "err", "line == '" .. tostring(line) .. "'")
                                     end,
                                     output_done = function()
                                         assert(steps_count == 3)
                                         assert(err_count == 1)
                                         steps_count = steps_count + 1
                                         spawns_done = spawns_done + 1
                                     end,
                                     exit = function(reason, code)
                                         assert(reason == "exit")
                                         assert(exit_snd == nil)
                                         assert(code == 42)
                                         exit_snd = code
                                     end
                                 })

            spawn.once(tiny_client("client1"), {tag=screen[1].tags[2]})
        end
        if spawns_done == 3 then
            assert(exit_yay == 0)
            assert(exit_snd == 42)
            assert(async_spawns_done == 2)
            return true
        end
    end,
    -- Test spawn_once
    function()
        if #client.get() ~= 1 then return end

        assert(client.get()[1].class == "client1")
        assert(client.get()[1]:tags()[1] == screen[1].tags[2])

        spawn.once(tiny_client("client1"), {tag=screen[1].tags[2]})
        spawn.once(tiny_client("client1"), {tag=screen[1].tags[2]})
        return true
    end,
    function(count)
        -- Limit the odds of a race condition
        if count ~= 3 then return end

        assert(#client.get() == 1)
        assert(client.get()[1].class == "client1")
        client.get()[1]:kill()
        return true
    end,
    -- Test single_instance
    function()
        if #client.get() ~= 0 then return end

        -- This should do nothing
        spawn.once(tiny_client("client1"), {tag=screen[1].tags[2]})

        spawn.single_instance(tiny_client("client2"), {tag=screen[1].tags[3]})

        return true
    end,
    -- Test that no extra clients are created
    function()
        if #client.get() ~= 1 then return end

        assert(client.get()[1].class == "client2")
        assert(client.get()[1]:tags()[1] == screen[1].tags[3])

        -- This should do nothing
        spawn.single_instance(tiny_client("client2"), {tag=screen[1].tags[3]})
        spawn.single_instance(tiny_client("client2"), {tag=screen[1].tags[3]})

        return true
    end,
    function()
        if #client.get() ~= 1 then return end

        assert(client.get()[1].class == "client2")
        assert(client.get()[1]:tags()[1] == screen[1].tags[3])

        client.get()[1]:kill()

        return true
    end,
    -- Test that new instances can be spawned
    function()
        if #client.get() ~= 0 then return end

        spawn.single_instance(tiny_client("client2"), {tag=screen[1].tags[3]})

        return true
    end,
    -- Test raise_or_spawn
    function()
        if #client.get() ~= 1 then return end

        assert(client.get()[1].class == "client2")
        assert(client.get()[1]:tags()[1] == screen[1].tags[3])
        client.get()[1]:kill()

        assert(not spawn.raise_or_spawn(tiny_client("client3"), {
            tag=screen[1].tags[3], foo = "baz"
        }))

        return true
    end,
    -- Add more clients to test the focus
    function()
        if #client.get() ~= 1 or client.get()[1].class ~= "client3" then return end

        -- In another iteration to make sure client4 has no focus
        spawn(tiny_client("client4"), {tag = screen[1].tags[4], foo = "bar"})
        spawn(tiny_client("client4"), {tag = screen[1].tags[4], foo = "bar"})
        spawn(tiny_client("client4"), {
            tag = screen[1].tags[4], switch_to_tags= true, focus = true, foo = "bar"
        })

        return true
    end,
    function()
        if #client.get() ~= 4 then return end

        local by_class = {}
        for _, c in ipairs(client.get()) do
            by_class[c.class] = by_class[c.class] and (by_class[c.class] + 1) or 1
            if c.class == "client4" then
                assert(#c:tags() == 1)
                assert(c.foo == "bar")
                assert(c:tags()[1] == screen[1].tags[4])
            elseif c.class == "client3" then
                assert(c.foo == "baz")
                assert(c:tags()[1] == screen[1].tags[3])
            end
        end

        assert(by_class.client3 == 1)
        assert(by_class.client4 == 3)
        assert(screen[1].tags[3].selected == false)
        assert(screen[1].tags[4].selected == true )

        assert(screen[1].tags[4].selected == true)

        assert(spawn.raise_or_spawn(tiny_client("client3"), {tag=screen[1].tags[3], foo = "baz"}))

        return true
    end,
    -- Test that the client can be raised
    function()
        if #client.get() ~= 4 then return false end

        assert(client.focus.class == "client3")
        assert(screen[1].tags[3].selected == true)
        assert(screen[1].tags[4].selected == false)


        for _, c in ipairs(client.get()) do
            if c.class == "client4" then
                c:tags()[1]:view_only()
                client.focus = c
                break
            end
        end

        assert(screen[1].tags[3].selected == false)
        assert(screen[1].tags[4].selected == true )

        for _, c in ipairs(client.get()) do
            if c.class == "client3" then
                c:kill()
            end
        end

        return true
    end,
    -- Test that a new instance can be spawned
    function()
        if #client.get() ~= 3 then return end

        spawn.raise_or_spawn(tiny_client("client3"), {tag=screen[1].tags[3]})

        return true
    end,
    function()
        if #client.get() ~= 4 then return end

        -- Cleanup
        for _, c in ipairs(client.get()) do
            c:kill()
        end

        return true
    end,
    -- Test the matcher
    function()
        if #client.get() ~= 0 then return end

        spawn.single_instance("xterm", {tag=screen[1].tags[5]}, function(c)
            matcher_called = true
            return c.class == "xterm"
        end)

        return true
    end,
    function()
        if #client.get() ~= 1 then return end
        assert(matcher_called)
        return true
    end,
}

runner.run_steps(steps)

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80