File: testfileutil.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (103 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download | duplicates (5)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
unit testfileutil;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpcunit, testglobals, FileUtil, LazFileUtils, LazUTF8;

type

  { TTestFileUtil }

  TTestFileUtil= class(TTestCase)
  published
    procedure TestFileIsExecutable;
    procedure TestExtractFileNameWithoutExt;
    procedure TestTrimFileName;
    procedure TestFileCopyPreserveTime;
  end;

implementation

{ TTestFileUtil }

procedure TTestFileUtil.TestFileIsExecutable;
  procedure DoTest(const AFileName: string; Expected: boolean);
  begin
    AssertEquals(AFileName, Expected, FileIsExecutable(AFileName));
  end;
begin
  DoTest(ParamStrUTF8(0),true);
  // a directory is not an executable file
  DoTest(ExtractFileDir(ParamStrUTF8(0)), false);
end;

procedure TTestFileUtil.TestExtractFileNameWithoutExt;
var
  DirName : string;
  procedure DoTest(const FileName, Expected: string);
  begin
    AssertEquals(FileName, Expected, ExtractFileNameWithoutExt(FileName));
  end;
begin
  DoTest('test.pas', 'test');
  DoTest('test.pas.bak', 'test.pas');
  DirName := AppendPathDelim('testdir');
  DoTest(DirName + 'test.pas', DirName + 'test');
  DoTest(DirName + 'test.pas.bak', DirName + 'test.pas');
end;

procedure TTestFileUtil.TestTrimFileName;
  procedure DoTest(AFileName, Expected: string);
  begin
    ForcePathDelims(AFileName);
    ForcePathDelims(Expected);
    AssertEquals(AFileName, Expected, TrimFilename(AFileName));
  end;
begin
{$ifdef windows}
  DoTest('c:\LazarusDir\..\dir\','c:\dir\');
{$endif}
  DoTest('$(LazarusDir)\..\dir\','$(LazarusDir)\..\dir\');
  // see also TestLazFileUtils.TestResolveDots
end;

procedure TTestFileUtil.TestFileCopyPreserveTime;
// test for issue 11912 and 12317
var
  File1, File2: string;
  Result: boolean;
  procedure CreateTestFile(FileName: string);
  var
    f: text;
  begin
    assign(f, Utf8ToAnsi(FileName));
    rewrite(f);
    writeln(f, 'TestFileCopyPreserveTime');
    close(f);
    FileSetDateUTF8(FileName, 960054793);
  end;

begin
  File1 := AnsiToUtf8(SysUtils.GetTempFileName);
  CreateTestFile(File1);
  File2 :=  AnsiToUtf8(SysUtils.GetTempFileName);
  try
    Result := CopyFile(File1,File2,true);
    AssertTrue('File copy failed', Result);
    AssertTrue('Copied file does not exist', FileExistsUTF8(File2));
    AssertEquals('Time not preserved', FileAgeUTF8(File1), FileAgeUTF8(File2));
  finally
    if FileExistsUTF8(File1) then
      DeleteFileUTF8(File1);
    if FileExistsUTF8(File2) then
      DeleteFileUTF8(File2);
  end;
end;

initialization
  AddToLazUtilsTestSuite(TTestFileUtil);
end.