Commit c66bf87
authored
fix: correct MockFileInfo and MockDirectoryInfo for non existing files/directories (#834)
Fixes #829
Fixes #830
BREAKING CHANGE: `MockFileData.NullObject` is now `internal`. Users need to create new `MockFileData` instances for representing arbitrary data.
Detailed changes:
* make MockFileData.NullObject internal
The `MockFileData.NullObject` was introduced in commit
0d85da3 as a way to represent the data
of a non-existing file. Nevertheless, the issue #830 shows that at least
one person used it as "some arbitrary MockFileData of an existing file
I don't care about".
Reason: As we are going to really make this the data representing a
non-existing file by fixing/changing the `Attribute` property in one of
the next commits, make this property internal and thereby breaking to
force all consumers to really think about their tests and if they
really ment a non-existing file or simply some arbirary data.
Otherwise, the upcoming change would have nasty side effects if the
NullObject is used for arbitrary data of an existing file.
An alternative considered would be to rename this static property (which
is also breaking) to any users, but decided against it as I couldn't
think of a usecase where explicitly creating a file or directory which
doesn't exist instead of relying on what the library will for you when
being asked for a non-existing file.
E.g. it is the difference of
var fs = new MockFileSystem();
var nonExistingPath = XFS.Path(@"c:\some\non\existing\file.txt");
fs.AddFile(nonExistingPath, MockFileData.NullObject);
var fi = fs.FileInfo.FromFilename(nonExistingPath);
vs simply relying on the library to return the correct data for the
non-existing file:
var fs = new MockFileSystem();
var nonExistingPath = XFS.Path(@"c:\some\non\existing\file.txt");
var fi = fs.FileInfo.FromFilename(nonExistingPath);
All tests are adapted to no longer use the MockFileData.NullObject.
* fix: return -1 in MockFileInfo.Attributes and MockDirectoryInfo
According to the Microsoft documentation[1] the `Attributes` property of
FileSystemInfo (base of FileInfo and DirectoryInfo) returns
(FileAttributes)(-1) for a file/directory which doesn't exist when the
object instance is created. The on disk content is not looked at when
calling the `Attributes` property, only a pre-cached value is returned.
Only calling `Refresh` will read from disk.
To be on the safe side, I checked the documented behaviour with the real
implemenation with the below code snipped and it indeed shows the
documented behaviour.
var fs = new FileSystem();
var expected = (FileAttributes)(-1);
var fi = fs.FileInfo.FromFileName(@"x:\not-existing\path");
Assert.That(fi.Attributes, Is.EqualTo(expected));
This lead to quite some needed code adaptions, especially in the area of
the `GetMockFileDataForRead` methods, as those now no longer throw an
exception. Furthermore, as cachedMockFileData is always a non-null instance,
get rid of the logic checking for null in `GetMockFileDataForRead` and
`MockFileInfo.CopyTo(string destFileName)` method.
Also ensure that the used MockFileData is always `Clone`ed (and therefore
even the MockFileData.NullObject) so that we operate on our own copy
and don't get any updates done in the FileSystem.
With this new semantics the `MockDiretory.Exists` method requires special
treatment, as we now must check for a valid MockFileData by checking
if Attributes != -1.
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.attributes?view=net-6.0#remarks
* MockFileInfo: add test setting Attributes on non-existing file
Ensure that when setting the Attributes property on a non existing file
the appropriate FileNotFoundException is thrown, as documented in [1].
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.attributes?view=net-6.0#remarks
* MockDirectory: add tests for Time property getters on non existing file
Add tests that when trying to retrieve the time properties of a non
existing file returns 12:00 midnight, January 1, 1601 A.D. (C.E.)
Coordinated Universal Time (UTC) as documented in [1]
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.lastaccesstime?view=net-6.0#remarks
* MockFileInfo: add tests for Time property getters on non existing file
Add tests that when trying to retrieve the time properties of a non
existing file returns 12:00 midnight, January 1, 1601 A.D. (C.E.)
Coordinated Universal Time (UTC) as documented in [1]
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.lastaccesstime?view=net-6.0#remarks
* Fix MockDirectoryInfo: throw correct exception on non-existing directory
When setting one of the Time properties or the `Attributes` property
on a non-existing directory, a DirectoryNotFoundException should be thrown.
Fix the code by throwing the correct exception (it did throw a
FileNotFoundExecption before) and add the missing tests.
* Increase major version to 17.0
MockFileData.NullObject was made internal, which is a breaking change,
therefore increase the major version.1 parent 94ba753 commit c66bf87
File tree
10 files changed
+249
-47
lines changed- src/System.IO.Abstractions.TestingHelpers
- tests/System.IO.Abstractions.TestingHelpers.Tests
10 files changed
+249
-47
lines changedLines changed: 8 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
78 | 82 | | |
79 | 83 | | |
80 | 84 | | |
| |||
396 | 400 | | |
397 | 401 | | |
398 | 402 | | |
399 | | - | |
| 403 | + | |
400 | 404 | | |
401 | 405 | | |
402 | 406 | | |
403 | 407 | | |
404 | 408 | | |
405 | 409 | | |
406 | | - | |
| 410 | + | |
407 | 411 | | |
408 | 412 | | |
409 | 413 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
Lines changed: 7 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | | - | |
92 | | - | |
| 92 | + | |
| 93 | + | |
93 | 94 | | |
94 | 95 | | |
95 | 96 | | |
| |||
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | 207 | | |
215 | 208 | | |
216 | 209 | | |
| |||
373 | 366 | | |
374 | 367 | | |
375 | 368 | | |
376 | | - | |
| 369 | + | |
377 | 370 | | |
378 | 371 | | |
379 | 372 | | |
| |||
388 | 381 | | |
389 | 382 | | |
390 | 383 | | |
391 | | - | |
| 384 | + | |
392 | 385 | | |
393 | 386 | | |
394 | 387 | | |
395 | 388 | | |
396 | 389 | | |
397 | 390 | | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
| 391 | + | |
411 | 392 | | |
412 | 393 | | |
413 | 394 | | |
| |||
Lines changed: 140 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
58 | 77 | | |
59 | 78 | | |
60 | 79 | | |
| |||
443 | 462 | | |
444 | 463 | | |
445 | 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 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
446 | 586 | | |
447 | 587 | | |
Lines changed: 9 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1812 | 1812 | | |
1813 | 1813 | | |
1814 | 1814 | | |
| 1815 | + | |
1815 | 1816 | | |
1816 | 1817 | | |
1817 | | - | |
1818 | | - | |
1819 | | - | |
1820 | | - | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
1821 | 1822 | | |
1822 | 1823 | | |
1823 | 1824 | | |
| |||
1838 | 1839 | | |
1839 | 1840 | | |
1840 | 1841 | | |
| 1842 | + | |
1841 | 1843 | | |
1842 | 1844 | | |
1843 | | - | |
1844 | | - | |
1845 | | - | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
1846 | 1848 | | |
1847 | 1849 | | |
1848 | 1850 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
| 113 | + | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| |||
0 commit comments