-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathcell_disabling.jl
390 lines (297 loc) Β· 11.4 KB
/
cell_disabling.jl
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using Test
using Pluto
using Pluto: update_run!, ServerSession, ClientSession, Cell, Notebook, set_disabled, is_disabled, WorkspaceManager
@testset "Cell Disabling" begin
π = ServerSession()
π.options.evaluation.workspace_use_distributed = false
notebook = Notebook([
Cell("const a = 1")
Cell("const b = 2")
Cell("const c = 3")
Cell("const d = 4")
Cell("const x = a") # 5
# these cells will be uncommented later
Cell("# const x = b") # 6
Cell("# const x = c") # 7
Cell("const z = x") # 8
Cell("# const z = d") # 9
Cell("const y = z") # 10
Cell("things = []") # 11
Cell("""begin
cool = 1
push!(things, 1)
end""") # 12
Cell("""begin
# cool = 2
# push!(things, 2)
end""") # 13
Cell("cool; length(things)") # 14
])
update_run!(π, notebook, notebook.cells)
# helper functions
id(i) = notebook.cells[i].cell_id
c(i) = notebook.cells[i]
get_indirectly_disabled_cells(notebook) = [i for (i, c) in pairs(notebook.cells) if c.depends_on_disabled_cells]
@test !any(is_disabled, notebook.cells)
@test get_indirectly_disabled_cells(notebook) == []
@test all(noerror, notebook.cells)
###
setcode!(c(6), "const x = b")
update_run!(π, notebook, c(6))
@test c(5).errored
@test c(6).errored
@test c(8).errored
@test c(10).errored
@test get_indirectly_disabled_cells(notebook) == []
###
set_disabled(c(1), true)
update_run!(π, notebook, c(1))
@test noerror(c(1))
@test noerror(c(6))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 5]
update_run!(π, notebook, c(5:6))
@test noerror(c(1))
@test noerror(c(6))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 5]
###
set_disabled(c(1), false)
update_run!(π, notebook, c(1))
@test noerror(c(1))
@test c(5).errored
@test c(6).errored
@test c(8).errored
@test c(10).errored
@test get_indirectly_disabled_cells(notebook) == []
###
set_disabled(c(5), true)
update_run!(π, notebook, c(5))
@test noerror(c(1))
@test noerror(c(6))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [5]
###
set_disabled(c(1), true)
update_run!(π, notebook, c(1))
@test noerror(c(1))
@test noerror(c(6))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 5]
###
set_disabled(c(5), false)
setcode!(c(7), "const x = c")
update_run!(π, notebook, c([5,7]))
@test c(5).errored
@test c(6).errored
@test c(7).errored
@test c(8).errored
@test c(10).errored
@test get_indirectly_disabled_cells(notebook) == [1, 5]
###
set_disabled(c(2), true)
update_run!(π, notebook, c(2))
@test noerror(c(3))
@test noerror(c(7))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 2, 5, 6]
###
setcode!(c(9), "const z = d")
update_run!(π, notebook, c([9]))
@test noerror(c(7))
@test c(8).errored
@test c(9).errored
@test c(10).errored
@test get_indirectly_disabled_cells(notebook) == [1, 2, 5, 6]
###
set_disabled(c(4), true)
update_run!(π, notebook, c(4))
@test noerror(c(3))
@test noerror(c(4))
@test noerror(c(7))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 2, 4, 5, 6, 9]
###
set_disabled(c(1), true)
set_disabled(c(2), false)
set_disabled(c(3), true)
set_disabled(c(4), false)
set_disabled(c(5), true)
set_disabled(c(6), true)
set_disabled(c(7), false)
set_disabled(c(8), false)
set_disabled(c(9), true)
setcode!(c(10), "const x = 123123")
set_disabled(c(10), false)
update_run!(π, notebook, c(1:10))
@test noerror(c(1))
@test noerror(c(2))
@test noerror(c(3))
@test noerror(c(4))
@test noerror(c(8))
@test noerror(c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 3, 5, 6, 7, 9]
###
set_disabled(c(3), false)
update_run!(π, notebook, c(3))
@test get_indirectly_disabled_cells(notebook) == [1, 5, 6, 9]
@test c(7).errored
@test c(8).errored
@test c(10).errored
###
set_disabled(c(10), true)
update_run!(π, notebook, c(10))
@test get_indirectly_disabled_cells(notebook) == [1, 5, 6, 9, 10]
@test noerror(c(7))
@test noerror(c(8))
###
set_disabled(c(7), true)
set_disabled(c(10), false)
update_run!(π, notebook, c([7,10]))
@test get_indirectly_disabled_cells(notebook) == [1, 5, 6, 7, 9]
@test noerror(c(7))
@test noerror(c(8))
@test noerror(c(10))
### check that they really don't run when disabled
@test c(14).output.body == "1"
setcode!(c(13), replace(c(13).code, "#" => ""))
update_run!(π, notebook, c([11,13]))
@test c(12).errored
@test c(13).errored
@test c(14).errored
set_disabled(c(13), true)
update_run!(π, notebook, c([13]))
@test noerror(c(12))
@test noerror(c(14))
@test c(14).output.body == "1"
update_run!(π, notebook, c([11]))
@test c(14).output.body == "1"
update_run!(π, notebook, c([12]))
update_run!(π, notebook, c([12]))
@test c(14).output.body == "3"
cleanup(π, notebook)
end
@testset "Cell Disabling 1" begin
π = ServerSession()
π.options.evaluation.workspace_use_distributed = false
notebook = Notebook([
Cell("""y = begin
1 + x
end"""),
Cell("x = 2"),
Cell("z = sqrt(y)"),
Cell("a = 4x"),
Cell("w = z^5"),
Cell(""),
])
update_run!(π, notebook, notebook.cells)
# helper functions
id(i) = notebook.cells[i].cell_id
get_disabled_cells(notebook) = [i for (i, c) in pairs(notebook.cells) if c.depends_on_disabled_cells]
@test !any(get(c.metadata, "disabled", false) for c in notebook.cells)
@test !any(c.depends_on_disabled_cells for c in notebook.cells)
# disable first cell
notebook.cells[1].metadata["disabled"] = true
update_run!(π, notebook, notebook.cells[1])
should_be_disabled = [1, 3, 5]
@test get_disabled_cells(notebook) == should_be_disabled
@test notebook.cells[1].metadata["disabled"] == true
# metadatum will exists in memory, but not in the serialized form
@test all(haskey(notebook.cells[i].metadata , "disabled") for i=1:5)
# change x, this change should not propagate through y
original_y_output = notebook.cells[1].output.body
original_z_output = notebook.cells[3].output.body
original_a_output = notebook.cells[4].output.body
original_w_output = notebook.cells[5].output.body
setcode!(notebook.cells[2], "x = 123123")
update_run!(π, notebook, notebook.cells[2])
@test notebook.cells[1].output.body == original_y_output
@test notebook.cells[3].output.body == original_z_output
@test notebook.cells[4].output.body != original_a_output
@test notebook.cells[5].output.body == original_w_output
setcode!(notebook.cells[2], "x = 2")
update_run!(π, notebook, notebook.cells[2])
@test notebook.cells[1].output.body == original_y_output
@test notebook.cells[3].output.body == original_z_output
@test notebook.cells[4].output.body == original_a_output
@test notebook.cells[5].output.body == original_w_output
# disable root cell
notebook.cells[2].metadata["disabled"] = true
update_run!(π, notebook, notebook.cells)
@test get_disabled_cells(notebook) == collect(1:5)
original_6_output = notebook.cells[6].output.body
setcode!(notebook.cells[6], "x + 6")
update_run!(π, notebook, notebook.cells[6])
@test notebook.cells[6].depends_on_disabled_cells
@test notebook.cells[6].errored === false
@test notebook.cells[6].output.body == original_6_output
# reactivate first cell - still all cells should be running_disabled
notebook.cells[1].metadata["disabled"] = false
update_run!(π, notebook, notebook.cells)
@test get_disabled_cells(notebook) == collect(1:6)
# the x cell is disabled, so changing it should have no effect
setcode!(notebook.cells[2], "x = 123123")
update_run!(π, notebook, notebook.cells[2])
@test notebook.cells[1].output.body == original_y_output
@test notebook.cells[3].output.body == original_z_output
@test notebook.cells[4].output.body == original_a_output
@test notebook.cells[5].output.body == original_w_output
# reactivate root cell
notebook.cells[2].metadata["disabled"] = false
update_run!(π, notebook, notebook.cells)
@test get_disabled_cells(notebook) == []
@test notebook.cells[1].output.body != original_y_output
@test notebook.cells[3].output.body != original_z_output
@test notebook.cells[4].output.body != original_a_output
@test notebook.cells[5].output.body != original_w_output
# disable first cell again
notebook.cells[1].metadata["disabled"] = true
update_run!(π, notebook, notebook.cells)
should_be_disabled = [1, 3, 5]
@test get_disabled_cells(notebook) == should_be_disabled
# and reactivate it
notebook.cells[1].metadata["disabled"] = false
update_run!(π, notebook, notebook.cells)
@test get_disabled_cells(notebook) == []
cleanup(π, notebook)
end
@testset "Disabled cells should stay in the topology (#2676)" begin
π = ServerSession()
notebook = Notebook(Cell.([
"using Dates",
"b = 2; December",
"b",
]))
disabled_cell = notebook.cells[end]
Pluto.set_disabled(disabled_cell, true)
@test is_disabled(disabled_cell)
old_topo = notebook.topology
@test count(Pluto.is_disabled, notebook.cells) == 1
order = update_run!(π, notebook, notebook.cells)
# Disabled
@test length(order.input_topology.disabled_cells) == 1
@test disabled_cell β order.input_topology.disabled_cells
runned_cells = collect(order)
@test length(runned_cells) == 2
@test disabled_cell β runned_cells
topo = notebook.topology
@test old_topo !== topo # topology was updated
order = Pluto.topological_order(notebook)
@test length(order.input_topology.disabled_cells) == 1
@test disabled_cell β order.input_topology.disabled_cells
saved_cells = collect(order)
@test length(saved_cells) == length(notebook.cells)
@test issetequal(saved_cells, notebook.cells)
io = IOBuffer()
Pluto.save_notebook(io, notebook)
seekstart(io)
notebook2 = Pluto.load_notebook_nobackup(io, "mynotebook.jl")
@test length(notebook2.cells) == length(notebook.cells)
cleanup(π, notebook)
end