Skip to content

Commit 55f056e

Browse files
committed
U315-010 Add shell command to ALS tester driver
Use this comman to create symbolic link during test run. Fix regexp for `${NAME}` macro in tests. Add `DIR` into environment variables.
1 parent 762fc72 commit 55f056e

File tree

6 files changed

+113
-12
lines changed

6 files changed

+113
-12
lines changed

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,7 @@ vscode-test:
130130
# This contains no useful test, so deactivated for now.
131131
# cd integration/vscode/ada; npm run compile && node out/runTests.js
132132

133-
prepare:
134-
# Create a symbol link required by a test
135-
[ -e testsuite/ada_lsp/project_symlinks/link ] || \
136-
(cd testsuite/ada_lsp/project_symlinks; ln -s prj link)
137-
138-
check: all prepare
133+
check: all
139134
set -e; \
140135
if [ `$(PYTHON) -c "import sys;print('e3' in sys.modules)"` = "True" ]; then\
141136
(cd testsuite ; sh run.sh ) ; \

source/tester/tester-macros.adb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ package body Tester.Macros is
3838
-- Turn Path into URI with scheme 'file://'
3939

4040
Pattern : constant GNAT.Regpat.Pattern_Matcher :=
41-
GNAT.Regpat.Compile ("\${([\W]+)}|\$URI{([^}]*)}");
41+
GNAT.Regpat.Compile ("\${([\w]+)}|\$URI{([^}]*)}");
4242

4343
Replace_Slash : constant Ada.Strings.Maps.Character_Mapping :=
4444
Ada.Strings.Maps.To_Mapping
@@ -176,8 +176,10 @@ package body Tester.Macros is
176176
Full_Name : constant String := Ada.Directories.Full_Name (Path);
177177
Directory : constant String :=
178178
Ada.Directories.Containing_Directory (Full_Name);
179+
Env_With_Dir : Spawn.Environments.Process_Environment := Env;
179180
begin
180-
Test := Expand (Test, Env, Directory);
181+
Env_With_Dir.Insert ("DIR", Directory);
182+
Test := Expand (Test, Env_With_Dir, Directory);
181183
end Expand;
182184

183185
----------------

source/tester/tester-macros.ads

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package Tester.Macros is
2525
(Test : in out GNATCOLL.JSON.JSON_Value;
2626
Env : Spawn.Environments.Process_Environment;
2727
Path : String);
28-
-- Expand macros in given JSON test
28+
-- Expand macros in given JSON test. The Path is test's path.
2929
--
3030
-- Currently only one macro is supported:
3131
-- * ${NAME} - expands with environment variable NAME from Env

source/tester/tester-tests.adb

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package body Tester.Tests is
3131
Max_Wait : constant := 4_000;
3232
-- Max number of milliseconds to wait on a given snippet
3333

34-
type Command_Kind is (Start, Stop, Send, Comment);
34+
type Command_Kind is (Start, Stop, Send, Shell, Comment);
3535

3636
procedure Do_Start
3737
(Self : in out Test'Class);
@@ -44,6 +44,10 @@ package body Tester.Tests is
4444
(Self : in out Test'Class;
4545
Command : GNATCOLL.JSON.JSON_Value);
4646

47+
procedure Do_Shell
48+
(Self : in out Test'Class;
49+
Command : GNATCOLL.JSON.JSON_Value);
50+
4751
function Wait_Factor return Integer;
4852
-- Return the factor to multiply the delays with - useful for valgrind
4953
-- runs. This is an integer read from the environment variable
@@ -155,6 +159,96 @@ package body Tester.Tests is
155159
end loop;
156160
end Do_Send;
157161

162+
procedure Do_Shell
163+
(Self : in out Test'Class;
164+
Command : GNATCOLL.JSON.JSON_Value)
165+
is
166+
pragma Unreferenced (Self);
167+
168+
function To_Program (Name : String) return String;
169+
-- Take base name of the command and find it on PATH
170+
171+
function To_Program (Name : String) return String is
172+
Found : GNAT.OS_Lib.String_Access :=
173+
GNAT.OS_Lib.Locate_Exec_On_Path (Name);
174+
begin
175+
return Result : constant String := Found.all do
176+
Free (Found);
177+
end return;
178+
end To_Program;
179+
180+
List : constant GNATCOLL.JSON.JSON_Array := Command.Get;
181+
Cmd : constant String := To_Program (GNATCOLL.JSON.Get (List, 1).Get);
182+
Args : Spawn.String_Vectors.UTF_8_String_Vector;
183+
184+
type Shell_Listener is new Spawn.Processes.Process_Listener with record
185+
Done : Boolean := False;
186+
end record;
187+
188+
overriding procedure Finished
189+
(Self : in out Shell_Listener;
190+
Exit_Code : Integer);
191+
192+
overriding procedure Error_Occurred
193+
(Self : in out Shell_Listener;
194+
Process_Error : Integer);
195+
196+
overriding procedure Error_Occurred
197+
(Self : in out Shell_Listener;
198+
Process_Error : Integer) is
199+
begin
200+
Ada.Text_IO.Put ("Fail to run '");
201+
Ada.Text_IO.Put (Cmd);
202+
203+
for X of Args loop
204+
Ada.Text_IO.Put (" ");
205+
Ada.Text_IO.Put (X);
206+
end loop;
207+
208+
Ada.Text_IO.Put ("' error ");
209+
Ada.Text_IO.Put_Line (Process_Error'Image);
210+
Self.Done := True;
211+
end Error_Occurred;
212+
213+
overriding procedure Finished
214+
(Self : in out Shell_Listener;
215+
Exit_Code : Integer) is
216+
begin
217+
if Exit_Code /= 0 then
218+
Ada.Text_IO.Put ("Process '");
219+
Ada.Text_IO.Put (Cmd);
220+
221+
for X of Args loop
222+
Ada.Text_IO.Put (" ");
223+
Ada.Text_IO.Put (X);
224+
end loop;
225+
226+
Ada.Text_IO.Put ("' finished with code ");
227+
Ada.Text_IO.Put_Line (Exit_Code'Image);
228+
end if;
229+
230+
Self.Done := True;
231+
end Finished;
232+
233+
Shell : Spawn.Processes.Process;
234+
235+
Listener : aliased Shell_Listener;
236+
begin
237+
for J in 2 .. GNATCOLL.JSON.Length (List) loop
238+
Args.Append (GNATCOLL.JSON.Get (List, J).Get);
239+
end loop;
240+
241+
Shell.Set_Listener (Listener'Unchecked_Access);
242+
Shell.Set_Program (Cmd);
243+
Shell.Set_Arguments (Args);
244+
Shell.Start;
245+
246+
loop
247+
Spawn.Processes.Monitor_Loop (Timeout => 10);
248+
exit when Listener.Done;
249+
end loop;
250+
end Do_Shell;
251+
158252
--------------
159253
-- Do_Start --
160254
--------------
@@ -591,6 +685,8 @@ package body Tester.Tests is
591685
Self.Do_Stop (Value);
592686
when Send =>
593687
Self.Do_Send (Value);
688+
when Shell =>
689+
Self.Do_Shell (Value);
594690
when Comment =>
595691
null; -- Do nothing on comments
596692
end case;

testsuite/ada_lsp/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ should be in server response, but some values have a special meaning:
9090
* array `['<HAS>', item1, item2, ...]` - ensures that all given items are
9191
included into the array, any other array items are considered irrelevant and ignored
9292

93+
### Command `shell`
94+
95+
Property value - array of strings.
96+
97+
Tester launches a shell command taking command and arguments from the array.
98+
9399
### Command `comment`
94100

95-
Property value - array of string or just string.
101+
Property value - array of strings or just string.
96102

97103
Tester just ignores this command. We use it to add test desription and other
98104
comments to JSON test script.
@@ -104,7 +110,7 @@ JSON file preprocessing
104110

105111
Before execution Tester does some text substitution in each string literal.
106112
* Each substring `${NAME}` is replaced by an environment variable with
107-
given NAME.
113+
given NAME. The `DIR` environment variable points to test's directory.
108114

109115
* Each substring `$URI{x}` is replaced by corresponding URI `file:///test_dir/x`.
110116
where `x` should be path relative to the directory where `.json` file is located.

testsuite/ada_lsp/project_symlinks/test.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"In this mode ALS shouldn't resolve symlink:",
66
"the 'link/aaa.ads' file shouldn't be referred as 'prj/aaa.ads'"
77
]
8+
}, {
9+
"shell": ["sh", "-c", "rm -rf ${DIR}/link; ln -s prj ${DIR}/link" ]
810
}, {
911
"start": {
1012
"cmd": ["${ALS}"]

0 commit comments

Comments
 (0)