-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathchapter-06.srt
570 lines (444 loc) · 11 KB
/
chapter-06.srt
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
1
00:00:00,000 --> 00:00:03,954
Chapter 06.
Raising an exception when a build step fails.
2
00:00:04,200 --> 00:00:08,262
JC: The thing that gets trickier about
this is when you have multiple instructions.
3
00:00:08,285 --> 00:00:10,251
You want them all to be executed
4
00:00:10,285 --> 00:00:13,222
but because they have side-effects you
want them done in a particular order.
5
00:00:13,230 --> 00:00:18,000
Specifying that with mocks
can be a bit tricky but...
6
00:00:18,217 --> 00:00:23,245
One thing that we can do, which is a use-case
I've not got to yet: what if any of them fails?
7
00:00:23,260 --> 00:00:25,577
So we can say something like: if the first one fails,
8
00:00:25,588 --> 00:00:30,102
the second one should not be executed
and that implies that there's an ordering.
9
00:00:31,897 --> 00:00:39,268
So let's do that context:
'when there are many instructions'
10
00:00:41,977 --> 00:00:54,274
let the instructions be
'first-instruction' and 'second-instruction'.
11
00:00:54,280 --> 00:00:59,234
Again, it doesn't matter what these are
because we're not going to execute them.
12
00:00:59,622 --> 00:01:04,240
'it prints all the instructions'.
13
00:01:10,285 --> 00:01:13,257
Oh no, expect output.
14
00:01:16,380 --> 00:01:22,960
With 'first-instruction' once.
15
00:01:26,720 --> 00:01:30,800
And also the second instruction.
16
00:01:31,302 --> 00:01:34,240
When we build the target.
17
00:01:37,554 --> 00:01:43,268
And we also want to execute all of the instructions.
18
00:01:46,290 --> 00:01:49,280
That should be the target in this case.
19
00:01:49,360 --> 00:02:01,451
And puts should be system when you build target.
20
00:02:01,790 --> 00:02:04,354
I believe these will work.
21
00:02:05,268 --> 00:02:08,388
They do.
TS: Great
22
00:02:08,510 --> 00:02:16,285
JC: Now we want to say
'when the first instruction fails'.
23
00:02:17,622 --> 00:02:23,150
TS: So you're saying here that it doesn't matter
what order it does it, that's not testing it?
24
00:02:23,150 --> 00:02:26,205
JC: No, RSpec will not check.
25
00:02:26,285 --> 00:02:30,514
RSpec doesn't expect things to be called
in the order in which you set up the mocks.
26
00:02:30,510 --> 00:02:32,514
You could switch these lines around.
27
00:02:32,640 --> 00:02:37,120
If I switch those this will still pass.
28
00:02:38,822 --> 00:02:42,205
Even though that isn't what I want.
29
00:02:42,514 --> 00:02:47,588
But specifying that you want
things called in a certain order is...
30
00:02:48,270 --> 00:02:53,405
The way that I program is an uncommon use
case and is awkward to specify using mocks.
31
00:02:53,420 --> 00:02:57,977
But if you have something where one thing
can prevent the next thing from happening,
32
00:02:58,030 --> 00:03:03,040
then that implies an ordering. It's enough to
say that one thing happens before the other.
33
00:03:03,108 --> 00:03:06,068
TS: And that's something else
that you want to test anyway.
34
00:03:06,060 --> 00:03:11,417
JC: Yes you want it to abort the
build if something bad happens.
35
00:03:12,582 --> 00:03:21,417
So when the first instruction fails, it
means that I'm going to allow the target
36
00:03:22,480 --> 00:03:34,285
to receive system with
'first-argument' and return nil.
37
00:03:34,280 --> 00:03:38,411
Which is what system returns
if the command doesn't work.
38
00:03:40,697 --> 00:03:50,834
So that scenario...(I'm not going to bother with
the printing stuff, we've done that to death).
39
00:03:50,982 --> 00:03:55,920
It does execute the first argument.
40
00:03:58,937 --> 00:04:04,400
But it does not execute the second.
41
00:04:07,405 --> 00:04:14,000
So that means we expect this... Once.
42
00:04:14,262 --> 00:04:18,491
[Silence]
43
00:04:18,617 --> 00:04:21,565
Once.
44
00:04:21,634 --> 00:04:29,382
Do we need to tell it that it will
return nil? Not sure that we do.
45
00:04:32,102 --> 00:04:34,960
We'll find out.
46
00:04:35,634 --> 00:04:41,634
I think I've done something weird
to Vim. Oh, I've got caps lock on, sorry!
47
00:04:43,131 --> 00:04:44,274
I'll copy that.
48
00:04:44,270 --> 00:04:55,897
So let me find the second-
they're not arguments, they're instructions.
49
00:04:59,300 --> 00:05:04,605
And it's not 'once', it's 'not'.
50
00:05:07,760 --> 00:05:09,291
So let me check this over.
51
00:05:09,290 --> 00:05:13,348
So if the target returns nil
for the first instruction,
52
00:05:13,360 --> 00:05:17,554
it should still execute it, but not the second one.
53
00:05:19,420 --> 00:05:22,914
This shouldn't work because I haven't
implemented anything like this yet.
54
00:05:23,050 --> 00:05:27,851
So expect it to use a target
(that I haven't actually told it to build).
55
00:05:27,897 --> 00:05:30,742
That's quite important. [laughter].
56
00:05:30,740 --> 00:05:33,085
target.build
57
00:05:33,222 --> 00:05:38,320
[Silence]
58
00:05:38,422 --> 00:05:42,948
Now it's executing the second
instruction when it shouldn't.
59
00:05:42,982 --> 00:05:48,411
So if we go back to our code we can
say it's going to print the instruction.
60
00:05:48,410 --> 00:05:59,062
And then we could raise failed command
unless system returns something truthy.
61
00:05:59,542 --> 00:06:03,005
TS: So what will system return if it's not an error?
62
00:06:03,165 --> 00:06:08,320
JC: True. We can see this in the shell for a second.
63
00:06:08,360 --> 00:06:15,965
So if you system, echo something,
it does the thing and returns true.
64
00:06:15,970 --> 00:06:19,337
And if you ask it to run a program
that doesn't exist it returns nil.
65
00:06:19,360 --> 00:06:21,428
TS: OK great.
66
00:06:23,760 --> 00:06:33,417
JC: So it's quite handy for having a high
level API onto what your system is doing.
67
00:06:36,628 --> 00:06:49,462
So make an exception and raise that
unless system returns something truthy.
68
00:06:49,782 --> 00:06:56,388
TS: So you've made a decision here that you don't want
it to skip them silently, you want them to actually...
69
00:06:56,617 --> 00:07:01,394
JC: No because a build is just a sequence.
70
00:07:01,700 --> 00:07:06,297
You turn a CoffeeScript file into a JavaScript file
and you need to put multiple files into a single one.
71
00:07:06,308 --> 00:07:09,634
If the first step fails you can't do the second step.
72
00:07:09,630 --> 00:07:15,074
And you shouldn't try
because if those files don't exist,
73
00:07:15,097 --> 00:07:19,668
like if your command uses a shell wildcard or
tries to interpolate something that doesn't exist,
74
00:07:19,660 --> 00:07:23,554
you might end up constructing a
command that will do something dangerous.
75
00:07:23,550 --> 00:07:29,440
So it really ought to check that everything
you've done so far did what you expected.
76
00:07:31,280 --> 00:07:39,085
So we shall run that. OK so loads of things are
now not working, which is common, and fine.
77
00:07:41,600 --> 00:07:44,114
So let's find out why that was.
78
00:07:45,074 --> 00:07:50,102
A lot of these raised, I was not
expecting them to be raised.
79
00:07:52,960 --> 00:07:59,131
For example, the first one, 33 lets run that.
80
00:08:01,420 --> 00:08:03,474
It's raising,
81
00:08:05,554 --> 00:08:09,371
And it's raising from build.
82
00:08:14,360 --> 00:08:18,605
We said we're going to raise unless
system returned something truthy.
83
00:08:18,617 --> 00:08:22,582
But we haven't made system
return anything, we stubbed it out.
84
00:08:22,720 --> 00:08:24,731
TS: So that will be returning nil by default.
85
00:08:24,730 --> 00:08:31,554
JC: Yes, so we should make that return true and
anywhere else we're saying it should be called.
86
00:08:31,565 --> 00:08:34,514
That's a not so it doesn't matter.
87
00:08:36,765 --> 00:08:43,380
TS: Earlier you said you were expecting these
to inherit the behaviour of the stub anyway.
88
00:08:45,000 --> 00:08:46,445
JC: That's a good point yes.
89
00:08:46,480 --> 00:08:51,302
I think if you stub something and
then put mock expectation on it,
90
00:08:51,622 --> 00:08:57,417
the thing at the top says if you call
it with anything it just returns true.
91
00:08:57,420 --> 00:08:59,097
The mocks tend to be more specific.
92
00:08:59,142 --> 00:09:03,725
Either they're saying not to but it doesn't
matter because it's not going to be called.
93
00:09:04,670 --> 00:09:09,348
But here we're saying it should be called
with something and that's a different use case.
94
00:09:09,370 --> 00:09:15,017
I believe in those situations you have
to tell it again what you want it to return.
95
00:09:15,050 --> 00:09:21,702
That's passing, a couple are
still not passing 64 and 69.
96
00:09:21,820 --> 00:09:29,965
[Silence]
97
00:09:30,057 --> 00:09:33,702
TS: Why were they failing?
98
00:09:34,708 --> 00:09:42,480
JC: I believe this will raise and it's supposed
to raise, but I'm not catching that it raises.
99
00:09:43,154 --> 00:09:50,365
I should have another test that
says it raises FailedCommand.
100
00:09:54,171 --> 00:10:06,400
Expect target.build to raise
error and FakeMake FailedCommand.
101
00:10:06,490 --> 00:10:10,502
TS: So the problem with these tests is that
the exception was bubbling out of the tests.
102
00:10:10,520 --> 00:10:13,165
JC: Right.
103
00:10:13,710 --> 00:10:17,725
And then I can just rescue in here.
104
00:10:17,850 --> 00:10:22,628
[Silence]
105
00:10:22,971 --> 00:10:24,365
OK so that's all green again.
106
00:10:24,400 --> 00:10:25,222
TS: Great.
107
00:10:27,074 --> 00:10:28,850
JC: It took a while to get there,
108
00:10:28,914 --> 00:10:34,788
But I think that's enough of a specification
of what the thing does about running commands.
109
00:10:34,800 --> 00:10:40,525
We've now got a thing that can run arbitrary commands
and can do something reasonably sensible with them.
110
00:10:40,520 --> 00:10:45,554
So I'm going to get out of here to make a commit.
111
00:10:45,550 --> 00:10:47,245
So what have we got?
112
00:10:50,365 --> 00:10:55,051
I might just move build.sh to my scratch
directory and keep that hanging around.
113
00:10:55,222 --> 00:11:03,714
So I ought to add the Gemfile things and the
example, although not everything in example.
114
00:11:04,960 --> 00:11:08,137
There are a few things we definitely don't want.
115
00:11:11,942 --> 00:11:17,040
I don't want an example bundle.js or example lib.
116
00:11:17,782 --> 00:11:22,914
Nor node modules, because they
are just for running the examples.
117
00:11:26,514 --> 00:11:28,742
I don't want scratch.
118
00:11:33,420 --> 00:11:38,205
I don't want package.json either, we're
building a Ruby project – although that is...
119
00:11:38,285 --> 00:11:42,340
No that's metadata about the examples
and you will use it to install your node modules.
120
00:11:42,354 --> 00:11:45,920
But you don't want node modules... that's fine.
121
00:11:46,034 --> 00:11:52,411
And we moved build.sh out of there. So if I
git add all of this. So what do we end up with?
122
00:11:53,570 --> 00:11:56,491
Git ignore and some gem files and some example code
123
00:11:56,491 --> 00:11:59,725
and our source code and package.json and specs.
124
00:12:00,251 --> 00:12:03,737
Which looks reasonable to me.
125
00:12:04,000 --> 00:12:16,331
[Silence]
126
00:12:16,434 --> 00:12:17,737
OK.