Skip to content

Commit b52f2b1

Browse files
committed
subprocess_run: test with actual executables built on system
1 parent c3434a1 commit b52f2b1

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ docs/
99

1010
*.mex*
1111
*.oct
12+
13+
test/printer_c.exe
14+
test/printer_fortran.exe

buildfile.m

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
plan("clean") = matlab.buildtool.tasks.CleanTask;
2020
end
2121

22+
plan("build_c") = matlab.buildtool.Task(Actions=@subprocess_build_c);
23+
plan("build_fortran") = matlab.buildtool.Task(Actions=@subprocess_build_fortran);
24+
plan("test").Dependencies = ["build_c", "build_fortran"];
25+
2226
if ~isMATLABReleaseOlderThan("R2024a")
2327
plan("check") = matlab.buildtool.tasks.CodeIssuesTask(pkg_name, IncludeSubfolders=true, ...
2428
WarningThreshold=0, Results="CodeIssues.sarif");
@@ -68,7 +72,7 @@ function legacy_mex(context, compiler_opt, linker_opt)
6872

6973

7074
function legacy_test(context)
71-
r = runtests(fullfile(context.Plan.RootFolder, "test"), Strict=false);
75+
r = runtests(context.Plan.RootFolder + "/test", Strict=false);
7276
% Parallel Computing Toolbox takes more time to startup than is worth it for this task
7377

7478
assert(~isempty(r), "No tests were run")
@@ -78,7 +82,7 @@ function legacy_test(context)
7882

7983
function publishTask(context)
8084
% publish HTML inline documentation strings to individual HTML files
81-
outdir = fullfile(context.Plan.RootFolder, "docs");
85+
outdir = context.Plan.RootFolder + "/docs";
8286

8387
publish_gen_index_html("stdlib", ...
8488
"A standard library of functions for Matlab.", ...
@@ -87,6 +91,64 @@ function publishTask(context)
8791
end
8892

8993

94+
function subprocess_build_c(context)
95+
96+
td = context.Plan.RootFolder + "/test";
97+
src_c = td + "/main.c";
98+
exe = td + "/printer_c.exe";
99+
100+
ccObj = mex.getCompilerConfigurations('c');
101+
102+
cc = ccObj.Details.CompilerExecutable;
103+
104+
outFlag = "-o";
105+
shell = "";
106+
shell_arg = "";
107+
msvcLike = ispc && endsWith(cc, "cl");
108+
if msvcLike
109+
shell = strtrim(c.Details.CommandLineShell);
110+
shell_arg = c.Details.CommandLineShellArg;
111+
outFlag = "/link /out:";
112+
end
113+
114+
cmd = join([cc, src_c, outFlag, exe]);
115+
if shell ~= ""
116+
cmd = join([shell, shell_arg, cmd]);
117+
end
118+
119+
[r, m] = system(cmd);
120+
if r ~= 0
121+
warning("failed to build TestSubprocess printer_c.exe " + m)
122+
end
123+
124+
end
125+
126+
127+
function subprocess_build_fortran(context)
128+
129+
td = context.Plan.RootFolder + "/test";
130+
src = td + "/main.f90";
131+
exe = td + "/printer_fortran.exe";
132+
133+
fcObj = mex.getCompilerConfigurations('Fortran');
134+
if isempty(fcObj)
135+
fc = "gfortran";
136+
else
137+
fc = fcObj.Details.CompilerExecutable;
138+
end
139+
140+
outFlag = "-o";
141+
142+
cmd = join([fc, src, outFlag, exe]);
143+
144+
[r, m] = system(cmd);
145+
if r ~= 0
146+
warning("failed to build TestSubprocess printer_fortran.exe " + m)
147+
end
148+
149+
end
150+
151+
90152
function srcs = get_mex_sources(build_all)
91153
arguments
92154
build_all (1,1) logical = false

test/TestSubprocess.m

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ function java_required(tc)
99

1010
methods (Test)
1111

12-
function test_simple_run(tc)
12+
function test_exe_c(tc)
13+
import matlab.unittest.constraints.IsFile
14+
15+
cwd = fileparts(mfilename('fullpath'));
16+
exe = cwd + "/printer_c.exe";
17+
tc.assumeThat(exe, IsFile, exe + " not found")
18+
19+
[status, msg, err] = stdlib.subprocess_run(exe);
20+
tc.assertEqual(status, 0, err)
21+
tc.verifyEqual(msg, "stdout")
22+
tc.verifyEqual(err, "stderr")
1323

14-
if ispc
15-
c = ["cmd", "/c", "dir"];
16-
else
17-
c = 'ls';
1824
end
1925

20-
[status, msg, err] = stdlib.subprocess_run(c);
26+
27+
function test_exe_fortran(tc)
28+
import matlab.unittest.constraints.IsFile
29+
30+
cwd = fileparts(mfilename('fullpath'));
31+
exe = cwd + "/printer_fortran.exe";
32+
tc.assumeThat(exe, IsFile, exe + " not found")
33+
34+
[status, msg, err] = stdlib.subprocess_run(exe);
2135
tc.assertEqual(status, 0, err)
22-
tc.verifyGreaterThan(strlength(msg), 0)
23-
tc.verifyEqual(strlength(err), 0)
36+
tc.verifyEqual(msg, "stdout")
37+
tc.verifyEqual(err, "stderr")
2438

2539
end
2640

@@ -41,7 +55,7 @@ function test_cwd(tc)
4155

4256
td = tc.createTemporaryFolder();
4357

44-
[s, mc, e] = stdlib.subprocess_run(c, "cwd", td);
58+
[s, mc, e] = stdlib.subprocess_run(c, cwd=td);
4559
tc.assertEqual(s, 0, "status non-zero")
4660
tc.verifyNotEqual(m, mc, "expected different directory to have different contents")
4761
tc.verifyEqual(strlength(e), 0, e)

test/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22

33
int main(void){
4-
printf("Hello\n");
4+
printf("stdout\n");
5+
fprintf(stderr, "stderr\n");
56
return 0;
67
}

test/main.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
program main
2+
3+
use, intrinsic :: iso_fortran_env
4+
5+
implicit none
6+
7+
write(output_unit, '(a)') "stdout"
8+
write(error_unit, '(a)') "stderr"
9+
10+
end program

0 commit comments

Comments
 (0)