bbt_0.0.6_807c8d3a/src/bbt-tests-actions-file_operations.adb

 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
-- -----------------------------------------------------------------------------
-- bbt, the black box tester (https://github.com/LionelDraghi/bbt)
-- Author : Lionel Draghi
-- SPDX-License-Identifier: APSL-2.0
-- SPDX-FileCopyrightText: 2024, Lionel Draghi
-- -----------------------------------------------------------------------------

with BBT.Settings;
with Text_Utilities;

package body BBT.Tests.Actions.File_Operations is

   -- --------------------------------------------------------------------------
   function Confirm_Delete (Prompt : String) return Boolean is
      C : Character;
   begin
      if Settings.Yes then return True;
      end if;

      loop
         Ada.Text_IO.Put (Prompt);
         Ada.Text_IO.Put_Line ("   [Y]es/[N]o/[A]ll");
         Ada.Text_IO.Get_Immediate (C);
         case C is
            when 'N' | 'n' => return False;
            when 'Y' | 'y' => return True;
            when 'A' | 'a' =>
               Settings.Yes := True;
               return True;
            when others => null;
         end case;
      end loop;
   end Confirm_Delete;

   -- --------------------------------------------------------------------------
   function Exists (Name : String) return Boolean is
   begin
      return Ada.Directories.Exists (Name);
   exception
      when Ada.Directories.Name_Error =>
         Put_Warning ("Illegal file name : " & Name);
         raise;
         -- return False;
   end Exists;

   function Kind (Name : String) return File_Kind
                  renames Ada.Directories.Kind;
   procedure Create_Path (New_Directory : String;
                          Form          : String := "")
                          renames Ada.Directories.Create_Path;

   -- --------------------------------------------------------------------------
   procedure Delete_File (Name : String) is
   begin
      if Exists (Name)
        and then Confirm_Delete ("Delete file " & Name & "?")
      then
         Ada.Directories.Delete_File (Name);
      end if;
   end Delete_File;

   -- --------------------------------------------------------------------------
   procedure Delete_Tree (Dir_Name : String)  is
   begin
      if Exists (Dir_Name)
        and then Confirm_Delete ("Delete tree " & Dir_Name & "?")
      then
         Ada.Directories.Delete_Tree (Dir_Name);
      end if;
   end Delete_Tree;

   -- --------------------------------------------------------------------------
   procedure Create (File : in out File_Type;
                     Mode : File_Mode := Out_File;
                     Name : String := "";
                     Form : String := "") renames Ada.Text_IO.Create;
   procedure Close  (File : in out File_Type) renames Ada.Text_IO.Close;

   function Create_File (File_Name    : Ada.Strings.Unbounded.Unbounded_String;
                         With_Content : Text) return Boolean
                         renames Text_Utilities.Create_File;

end BBT.Tests.Actions.File_Operations;