You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It may also be fun to use assert to check for the expected content of a file.
263
263
264
-
```bash
264
+
```test
265
265
code() {
266
266
echo 'not so cool' > /tmp/the_file
267
267
}
@@ -292,7 +292,7 @@ _assertion_ fails if its evaluation returns a status code different from 0.
292
292
293
293
If the evaluated expression does not fail, then *assert_fail* will fail and display the standard output and error of the evaluated _assertion_. The optional message is also displayed.
294
294
295
-
```bash
295
+
```test
296
296
code() {
297
297
echo 'not so cool' > /tmp/the_file
298
298
}
@@ -328,7 +328,7 @@ It may be useful if you want to distinguish between several error conditions in
328
328
329
329
In case of failure, the standard output and error of the evaluated _assertion_ is displayed. The optional message is also displayed.
In a previous exemple, we faked _ps_ by specifiyng code inline:
473
473
474
-
```bash
474
+
```test
475
475
fake ps echo hello world
476
476
ps
477
477
```
@@ -482,7 +482,7 @@ hello world
482
482
483
483
If you need to write more complex code to fake your command, you may abstract this code in a function:
484
484
485
-
```bash
485
+
```test
486
486
_ps() {
487
487
echo hello world
488
488
}
@@ -496,7 +496,7 @@ hello world
496
496
497
497
Be carefull however that your _ps function is not exported to sub-processes. It means that, depending on how your code under test works, _ps may not be defined in the context where ps will be called. For instance:
498
498
499
-
```bash
499
+
```test
500
500
_ps() {
501
501
echo hello world
502
502
}
@@ -511,7 +511,7 @@ bash: line 1: _ps: command not found
511
511
512
512
It depends on your code under test but it is safer to just export functions needed by your fake so that they are available in sub-processes:
513
513
514
-
```bash
514
+
```test
515
515
_ps() {
516
516
echo hello world
517
517
}
@@ -541,7 +541,7 @@ For instance, in our previous code that checks apache is running, we have an iss
541
541
542
542
To do that, the first naive approch would be:
543
543
544
-
```bash
544
+
```test
545
545
code() {
546
546
ps a | grep apache
547
547
}
@@ -581,7 +581,7 @@ With bash, the result code of a pipeline equals the result code of the last comm
581
581
582
582
An alternative may be to activate bash _pipefail_ option but this may introduce unwanted side effects. We can also simply not output anything in __ps_ so that _grep_ fails:
583
583
584
-
```bash
584
+
```test
585
585
code() {
586
586
ps a | grep apache
587
587
}
@@ -611,7 +611,7 @@ The only correct alternative is for the fake _ps_ to write _FAKE_PARAMS_ in a fi
611
611
so that your test can grab them after code execution and assert their value. For instance
0 commit comments