Skip to content

Commit 5dd2c70

Browse files
committed
Merge branch 'mr/jicquel/#53.rename_and_move' into 'master'
Preserve trailing directory separator with Rename_And_Move See merge request eng/toolchain/gnatcoll-core!99
2 parents ad7efa4 + d092047 commit 5dd2c70

File tree

7 files changed

+118
-3
lines changed

7 files changed

+118
-3
lines changed

src/projects/gnatcoll-projects-normalize.adb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,9 +2934,29 @@ package body GNATCOLL.Projects.Normalize is
29342934
Get_Host (New_Dir));
29352935
begin
29362936
if not Is_Absolute_Path (D) then
2937-
Set_String_Value_Of
2938-
(Node, Tree_Node,
2939-
Get_String (+Relative_Path (File, New_Dir)));
2937+
declare
2938+
Dir_String : constant String := String (D);
2939+
2940+
-- Get whether string ends with a directory separator
2941+
2942+
Ends_With_Separator : constant Boolean :=
2943+
Dir_String (Dir_String'Last) = '/' or else
2944+
Dir_String (Dir_String'Last) = '\';
2945+
2946+
-- If it does, make sure that it is kept, as it
2947+
-- would be removed by Normalize_Pathname.
2948+
2949+
Suffix : constant String :=
2950+
(if Ends_With_Separator then
2951+
"" & Dir_String (Dir_String'Last)
2952+
else
2953+
"");
2954+
begin
2955+
Set_String_Value_Of
2956+
(Node, Tree_Node,
2957+
Get_String
2958+
(+Relative_Path (File, New_Dir) & Suffix));
2959+
end;
29402960
end if;
29412961
end;
29422962

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project Foobar_P is
2+
3+
for Exec_Dir use ".";
4+
for Source_Dirs use ("src/" & "subsrc/");
5+
for Main use ("main.adb");
6+
7+
end Foobar_P;
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project P is
2+
for Exec_Dir use ".";
3+
for Source_Dirs use ("src/" & "subsrc/");
4+
for Main use ("main.adb");
5+
end P;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
with "gnatcoll";
2+
3+
project Prj is
4+
for Exec_Dir use ".";
5+
for Source_Dirs use (".", "src/" & "subsrc/");
6+
for Main use ("test.adb");
7+
end Prj;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function Main return Integer is
2+
begin
3+
return 0;
4+
end Main;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
with GNATCOLL.Projects; use GNATCOLL.Projects;
2+
with GNATCOLL.VFS; use GNATCOLL.VFS;
3+
with Test_Assert; use Test_Assert;
4+
with Ada.Directories;
5+
with Ada.Direct_IO;
6+
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
7+
8+
function Test return Integer is
9+
Tree : Project_Tree;
10+
Proj_File : constant String := "p.gpr";
11+
Proj : Project_Type;
12+
Dummy : Boolean;
13+
14+
function Read_File (File_Name : String) return String;
15+
-- Read a file and return its content as a String
16+
17+
function Read_File (File_Name : String) return String
18+
is
19+
File_Size : constant Natural :=
20+
Natural (Ada.Directories.Size (File_Name));
21+
22+
subtype File_String is String (1 .. File_Size);
23+
package File_String_IO is new Ada.Direct_IO (File_String);
24+
25+
File : File_String_IO.File_Type;
26+
Contents : File_String;
27+
28+
begin
29+
File_String_IO.Open (File, Mode => File_String_IO.In_File,
30+
Name => File_Name);
31+
File_String_IO.Read (File, Item => Contents);
32+
File_String_IO.Close (File);
33+
34+
return String (Contents);
35+
end Read_File;
36+
37+
begin
38+
Tree.Load (Create (Filesystem_String (Proj_File)));
39+
40+
Proj := Tree.Root_Project;
41+
Proj.Rename_And_Move
42+
("foobar_" & Proj.Name, Create
43+
(Filesystem_String
44+
(Ada.Directories.Containing_Directory
45+
(+Proj.Project_Path.Full_Name))));
46+
47+
Tree.Recompute_View;
48+
Dummy := Proj.Save;
49+
50+
declare
51+
Content : constant String := Read_File ("foobar_" & Proj_File);
52+
begin
53+
-- Check that the separator are not removed
54+
55+
Assert (Index (Content, """src/"" & ""subsrc/""") > 0);
56+
57+
-- Check that the project name has been updated
58+
59+
Assert (Index (Content, "Foobar_P") > 0);
60+
end;
61+
62+
return Report;
63+
end Test;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
description: Test Rename_And_Move and ensure that trailing directory separator are kept
2+
data:
3+
- "p.gpr"
4+
- "prj.gpr"
5+
- "test.adb"
6+
- "src"
7+
- "src/subsrc"
8+
- "src/subsrc/main.adb"

0 commit comments

Comments
 (0)