| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestSysUtils" -*- |
| 2 |
{ |
| 3 |
Copyright 2015-2021 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
unit TestSysUtils; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTester; |
| 23 |
|
| 24 |
type |
| 25 |
TTestSysUtils = class(TCastleTestCase) |
| 26 |
procedure TestDirectoryFileExists; |
| 27 |
end; |
| 28 |
|
| 29 |
implementation |
| 30 |
|
| 31 |
uses CastleFilesUtils, CastleUriUtils; |
| 32 |
|
| 33 |
procedure TTestSysUtils.TestDirectoryFileExists; |
| 34 |
begin |
| 35 |
{ Before FPC 3.2.0: |
| 36 |
|
| 37 |
FPC FileExists on Unix answers true for both regular files and directories. |
| 38 |
Unlike FPC FileExists on Windows, that answers true only for regular files. |
| 39 |
See http://www.freepascal.org/docs-html/rtl/sysutils/fileexists.html |
| 40 |
http://free-pascal-general.1045716.n5.nabble.com/FileExists-inconsistency-td2813433.html |
| 41 |
|
| 42 |
IMHO that's quite bad. |
| 43 |
- It's inconsistent across two platforms. |
| 44 |
- And Unix behavior is unexpected. |
| 45 |
|
| 46 |
FPC docs for FileExists say that this is deliberate on Unix, |
| 47 |
because "on Unix files are directories". |
| 48 |
But this seems like an after-thought explanation of a broken behavior |
| 49 |
in FPC docs. |
| 50 |
Indeed on Unix many things are "some kind of files" |
| 51 |
(including directories, sockets, pipes) when looking at C API and kernel API, |
| 52 |
but that's an internal detail for most people. |
| 53 |
In normal conversations, in normal API descriptions, |
| 54 |
when someone talks about "files", it's natural |
| 55 |
(also to Unix users and developers) to understand |
| 56 |
that you mean "regular files; not directories, network sockets, pipes...". |
| 57 |
|
| 58 |
It was fixed in |
| 59 |
https://github.com/graemeg/freepascal/commit/6fbfe3fc4c1dc23908b14a68fe54ae04d53ded73 , |
| 60 |
thankfully. |
| 61 |
} |
| 62 |
{$ifdef MSWINDOWS} |
| 63 |
AssertFalse(FileExists(UriToFilenameSafe('castle-data:/'))); |
| 64 |
AssertFalse(FileExists(UriToFilenameSafe('castle-data:/images/'))); |
| 65 |
{$else} |
| 66 |
{$if not defined(VER3_3)} // For FPC 3.3.1, the behavior depends on exact revision... |
| 67 |
{$if defined(VER3_0) or defined(VER3_1)} |
| 68 |
AssertTrue(FileExists(UriToFilenameSafe('castle-data:/'))); |
| 69 |
AssertTrue(FileExists(UriToFilenameSafe('castle-data:/images/'))); |
| 70 |
{$else} |
| 71 |
AssertFalse(FileExists(UriToFilenameSafe('castle-data:/'))); |
| 72 |
AssertFalse(FileExists(UriToFilenameSafe('castle-data:/images/'))); |
| 73 |
{$endif} |
| 74 |
{$endif} |
| 75 |
{$endif} |
| 76 |
AssertTrue(FileExists(UriToFilenameSafe('castle-data:/test.xml'))); |
| 77 |
AssertTrue(not FileExists(UriToFilenameSafe('castle-data:/test-not-existing.xml'))); |
| 78 |
|
| 79 |
{ Our RegularFileExists does not detect directory as "file". } |
| 80 |
AssertFalse(RegularFileExists(UriToFilenameSafe('castle-data:/'))); |
| 81 |
AssertFalse(RegularFileExists(UriToFilenameSafe('castle-data:/images/'))); |
| 82 |
AssertTrue(RegularFileExists(UriToFilenameSafe('castle-data:/test.xml'))); |
| 83 |
AssertTrue(not RegularFileExists(UriToFilenameSafe('castle-data:/test-not-existing.xml'))); |
| 84 |
|
| 85 |
{ DirectoryExists detects directories, not regular files. } |
| 86 |
AssertTrue(DirectoryExists(UriToFilenameSafe('castle-data:/'))); |
| 87 |
AssertTrue(DirectoryExists(UriToFilenameSafe('castle-data:/images/'))); |
| 88 |
AssertTrue(not DirectoryExists(UriToFilenameSafe('castle-data:/test.xml'))); |
| 89 |
AssertTrue(not DirectoryExists(UriToFilenameSafe('castle-data:/test-not-existing.xml'))); |
| 90 |
end; |
| 91 |
|
| 92 |
initialization |
| 93 |
RegisterTest(TTestSysUtils); |
| 94 |
end. |