Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork938
Commit41fac85
committed
Avoid mktemp in tests, in straightforward cases
The tempfile.mktemp function is deprecated, because of a racecondition where the file may be concurrently created between whenits name is generated and when it is opened. Other faciliies in thetempfile module overcome this by generating a name, attempting tocreate the file or directory in a way that guarantees failure if italready existed, and, in the occasional case that it did alreadyexist, generating another name and trying again (stopping after apredefined limit). For further information on mktemp deprecation:-https://docs.python.org/3/library/tempfile.html#tempfile.mktemp-gitpython-developers/smmap#41The security risk of calls to mktemp in this project's test suiteis low. However, it is still best to avoid using it, because it isdeprecated, because it is (at least slightly) brittle, and becauseany use of mktemp looks like a potential security risk and therebyimposes a burden on working with the code (which could potentiallybe addressed with detailed comments analyzing why it is believedsafe in particular cases, but this would typically be more verbose,and at least as challenging to add, as replacing mktemp with abetter alternative).This commit replaces *some* uses of mktemp in the test suite: thosewhere it is readily clear how to do so in a way that preserves thecode's intent:- Where a name for a temporary directory is generated with mktemp and os.mkdir is called immediately, mkdtemp is now used.- Where a name for a temporary file that is not customized (such as with a prefix) is generated with mktemp, such that the code under test never uses the filename but only the already-open file-like object, TemporaryFile is now used. As the name isn't customized, the test code in these cases does not express an intent to allow the developer to inspect the file after a test failure, so even if the file wasn't guaranteed to be deleted with a finally block or context manager, it is fine to do so. TemporaryFile supports this use case well on all systems including Windows, and automatically deletes the file.- Where a name for a temporary file that *is* customized (such as with a prefix) to reflect the way the test uses it is generated with mktemp, and the test code does not attempt deterministic deletion of the file when an exception would make the test fail, NamedTemporaryFile with delete=False is now used. The original code to remove the file when the test succeeds is modified accordingly to do the same job, and also commented to explain that it is being handled this way to allow the file to be kept and examined when a test failure occurs.Other cases in the test suite should also be feasible to replace,but are left alone for now. Some of them are ambiguous in theirintent, with respect to whether the file should be retained after atest failure. Others appear deliberately to avoid creating a fileor directory where the code under test should do so, possibly tocheck that this is done properly. (One way to preserve that latterbehavior, while avoiding the weakness of using mktemp and alsoavoiding inadverently reproducing that weakness by other means,could be to use a path in a temporary directory made for the test.)This commit also doesn't address the one use of mktemp in the codeunder test (i.e., outside the test suite, inside the git module).1 parent3ac7e78 commit41fac85
File tree
6 files changed
+42
-45
lines changed- test
- lib
- performance
6 files changed
+42
-45
lines changedLines changed: 1 addition & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
89 | 89 |
| |
90 | 90 |
| |
91 | 91 |
| |
92 |
| - | |
93 |
| - | |
| 92 | + | |
94 | 93 |
| |
95 | 94 |
| |
96 | 95 |
| |
|
Lines changed: 1 addition & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
65 | 65 |
| |
66 | 66 |
| |
67 | 67 |
| |
68 |
| - | |
69 |
| - | |
| 68 | + | |
70 | 69 |
| |
71 | 70 |
| |
72 | 71 |
| |
|
Lines changed: 2 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
68 | 68 |
| |
69 | 69 |
| |
70 | 70 |
| |
71 |
| - | |
72 |
| - | |
| 71 | + | |
73 | 72 |
| |
74 | 73 |
| |
75 | 74 |
| |
76 |
| - | |
| 75 | + | |
77 | 76 |
| |
78 | 77 |
| |
79 | 78 |
| |
|
Lines changed: 2 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | 1 |
| |
2 | 2 |
| |
3 | 3 |
| |
4 |
| - | |
| 4 | + | |
5 | 5 |
| |
6 | 6 |
| |
7 | 7 |
| |
8 | 8 |
| |
9 | 9 |
| |
10 | 10 |
| |
11 | 11 |
| |
12 |
| - | |
13 |
| - | |
14 | 12 |
| |
15 | 13 |
| |
16 | 14 |
| |
| |||
35 | 33 |
| |
36 | 34 |
| |
37 | 35 |
| |
38 |
| - | |
39 |
| - | |
| 36 | + | |
40 | 37 |
| |
41 | 38 |
| |
42 | 39 |
| |
|
Lines changed: 2 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
667 | 667 |
| |
668 | 668 |
| |
669 | 669 |
| |
670 |
| - | |
671 |
| - | |
| 670 | + | |
672 | 671 |
| |
673 | 672 |
| |
674 |
| - | |
| 673 | + | |
675 | 674 |
| |
676 | 675 |
| |
677 | 676 |
| |
|
Lines changed: 34 additions & 30 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
359 | 359 |
| |
360 | 360 |
| |
361 | 361 |
| |
362 |
| - | |
363 |
| - | |
364 |
| - | |
365 |
| - | |
366 |
| - | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
367 | 368 |
| |
368 |
| - | |
369 |
| - | |
370 |
| - | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
371 | 372 |
| |
372 |
| - | |
373 |
| - | |
374 |
| - | |
375 |
| - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
376 | 377 |
| |
377 |
| - | |
378 |
| - | |
| 378 | + | |
| 379 | + | |
379 | 380 |
| |
380 |
| - | |
381 |
| - | |
| 381 | + | |
| 382 | + | |
382 | 383 |
| |
383 |
| - | |
384 |
| - | |
385 |
| - | |
386 |
| - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
387 | 388 |
| |
388 | 389 |
| |
389 |
| - | |
390 |
| - | |
391 |
| - | |
392 |
| - | |
393 |
| - | |
394 |
| - | |
395 |
| - | |
396 |
| - | |
397 |
| - | |
398 |
| - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
399 | 402 |
| |
400 | 403 |
| |
401 | 404 |
| |
402 | 405 |
| |
403 | 406 |
| |
| 407 | + | |
404 | 408 |
| |
405 | 409 |
| |
406 | 410 |
| |
|
0 commit comments
Comments
(0)