-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_greetall.txt
59 lines (47 loc) · 1.54 KB
/
test_greetall.txt
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
Doctests for the greetall.py script
===================================
>>> import os
>>> import pathlib
>>> import subprocess
>>> def attempt(shell_command):
... proc = subprocess.run(shell_command, capture_output=True, shell=True)
... if proc.stdout:
... print('STDOUT:', proc.stdout.decode('utf-8'), sep='\n', end='')
... if proc.stderr:
... print('STDERR:', proc.stderr.decode('utf-8'), sep='\n', end='')
... return proc.returncode
With no arguments, it reads from stdin and writes to stdout:
>>> attempt('./greetall.py <names.txt')
STDOUT:
Hello, Bob Alistair!
Hello, Alice Bobson!
Hello, Eve "Eavesdropper" Ives!
0
With one argument, it reads from the named file and writes to stdout:
>>> attempt('./greetall.py names.txt')
STDOUT:
Hello, Bob Alistair!
Hello, Alice Bobson!
Hello, Eve "Eavesdropper" Ives!
0
With two arguments, it reads from the first file and writes to the second:
>>> path = pathlib.Path('greetings.txt')
>>> path.exists()
False
>>> attempt('./greetall.py names.txt greetings.txt')
0
>>> print(path.read_text(), end='')
Hello, Bob Alistair!
Hello, Alice Bobson!
Hello, Eve "Eavesdropper" Ives!
>>> os.remove(path)
With more arguments, it fails with an error:
>>> attempt('./greetall.py names.txt greetings.txt extra.txt') # doctest: +ELLIPSIS
STDERR:
...greetall.py: error: too many arguments
2
With a nonexistent input file, it fails with an error:
>>> attempt('./greetall.py nonexistent.txt') # doctest: +ELLIPSIS
STDERR:
...greetall.py: error: [Errno 2] No such file or directory: 'nonexistent.txt'
1