Skip to content

Commit b940542

Browse files
committed
move tests
add is_exe, which() tests for Windows WSL
1 parent da680b0 commit b940542

File tree

13 files changed

+139
-90
lines changed

13 files changed

+139
-90
lines changed
File renamed without changes.

+stdlib/+test/TestFileImpure.m

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
classdef TestFileImpure < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_expanduser(tc)
6+
import stdlib.fileio.expanduser
7+
tc.verifyFalse(startsWith(expanduser('~/foo'), "~"))
8+
tc.verifyFalse(any(startsWith(expanduser(["~/abc", "~/123"]), "~")))
9+
10+
tc.verifyTrue(endsWith(expanduser('~/foo'), "foo"))
11+
tc.verifyTrue(all(endsWith(expanduser(["~/abc", "~/123"]), ["abc", "123"])))
12+
13+
tc.verifyEmpty(expanduser(string.empty))
14+
tc.verifyEqual(expanduser(""), "")
15+
end
16+
17+
function test_makedir(tc)
18+
d = tempname;
19+
stdlib.fileio.makedir(d)
20+
tc.assertTrue(isfolder(d))
21+
end
22+
23+
function test_which_name(tc)
24+
import stdlib.fileio.which
25+
26+
if ismac
27+
n = "ls";
28+
else
29+
n = "matlab";
30+
end
31+
%% which: Matlab in environment variable PATH
32+
% MacOS Matlab does not source .zshrc so Matlab is not on internal Matlab PATH
33+
tc.verifyNotEmpty(which(n))
34+
35+
end
36+
37+
function test_is_exe_which_wsl(tc)
38+
import stdlib.fileio.is_exe
39+
import stdlib.fileio.which
40+
import stdlib.sys.subprocess_run
41+
42+
tc.assumeTrue(ispc, "Windows only")
43+
44+
wsl = which("wsl");
45+
tc.assumeNotEmpty(wsl, "did not find Windows Subsystem for Linux")
46+
47+
[ret, cc] = system("wsl which cc");
48+
tc.assumeEqual(ret, 0, "could not find WSL C compiler")
49+
tc.assumeNotEmpty(cc, "did not find WSL C compiler")
50+
51+
cwd = fileparts(mfilename('fullpath'));
52+
src = "main.c";
53+
[~, out] = fileparts(tempname);
54+
55+
oldcwd = pwd;
56+
cd(cwd)
57+
ret = system("wsl cc " + src + " -o" + out);
58+
59+
tc.assumeEqual(ret, 0, "failed to compile " + src)
60+
tc.assumeTrue(isfile(out), "cc failed to produce output file " + out)
61+
62+
tc.verifyTrue(is_exe(out), "is_exe() failed to detect WSL executable " + out)
63+
64+
wsl_exe = which(out);
65+
tc.verifyNotEmpty(wsl_exe, "which() failed to detect WSL executable " + out)
66+
67+
delete(out)
68+
69+
cd(oldcwd)
70+
71+
end
72+
73+
function test_is_exe_which_fullpath(tc)
74+
import matlab.unittest.constraints.IsFile
75+
import stdlib.fileio.which
76+
import stdlib.fileio.is_exe
77+
78+
n = "matlab";
79+
%% is_exe test
80+
p = fullfile(matlabroot, "bin", n);
81+
if ispc
82+
fp = p + ".exe";
83+
else
84+
fp = p;
85+
end
86+
tc.verifyTrue(is_exe(fp))
87+
%% which: test absolute path
88+
exe = which(p);
89+
90+
if ispc
91+
tc.verifyTrue(endsWith(exe, ".exe"))
92+
else
93+
tc.verifyFalse(endsWith(exe, ".exe"))
94+
end
95+
tc.verifyThat(exe, IsFile)
96+
97+
end
98+
99+
function test_copyfile(tc)
100+
import matlab.unittest.constraints.IsFile
101+
102+
f1 = tempname;
103+
[~,name] = fileparts(f1);
104+
fclose(fopen(f1,'w'));
105+
stdlib.fileio.copyfile(f1, tempdir)
106+
107+
tc.verifyThat(fullfile(tempdir, name), IsFile)
108+
end
109+
110+
function test_hash(tc)
111+
import matlab.unittest.constraints.IsFile
112+
113+
fn = tempname;
114+
fid = fopen(fn, "w");
115+
tc.assumeGreaterThan(fid, 0);
116+
fprintf(fid, "hello");
117+
fclose(fid);
118+
tc.assumeThat(fn, IsFile)
119+
120+
tc.verifyEqual(stdlib.fileio.md5sum(fn), "5d41402abc4b2a76b9719d911017c592")
121+
tc.verifyEqual(stdlib.fileio.sha256sum(fn), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
122+
123+
delete(fn)
124+
end
125+
126+
end
127+
128+
end
Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
classdef TestFileio < matlab.unittest.TestCase
1+
classdef TestFilePure < matlab.unittest.TestCase
22

33
methods (Test)
44

5-
function test_expanduser(tc)
6-
import stdlib.fileio.expanduser
7-
tc.verifyFalse(startsWith(expanduser('~/foo'), "~"))
8-
tc.verifyFalse(any(startsWith(expanduser(["~/abc", "~/123"]), "~")))
9-
10-
tc.verifyTrue(endsWith(expanduser('~/foo'), "foo"))
11-
tc.verifyTrue(all(endsWith(expanduser(["~/abc", "~/123"]), ["abc", "123"])))
12-
13-
tc.verifyEmpty(expanduser(string.empty))
14-
tc.verifyEqual(expanduser(""), "")
15-
end
16-
175
function test_posix(tc)
186
import stdlib.fileio.posix
197

@@ -82,52 +70,6 @@ function test_absolute_path(tc)
8270
tc.verifyEqual(absolute_path(""), string(pwd))
8371
end
8472

85-
function test_makedir(tc)
86-
d = tempname;
87-
stdlib.fileio.makedir(d)
88-
tc.assertTrue(isfolder(d))
89-
end
90-
91-
function test_which_name(tc)
92-
import stdlib.fileio.which
93-
94-
if ismac
95-
n = "ls";
96-
else
97-
n = "matlab";
98-
end
99-
%% which: Matlab in environment variable PATH
100-
% MacOS Matlab does not source .zshrc so Matlab is not on internal Matlab PATH
101-
tc.verifyNotEmpty(which(n))
102-
103-
end
104-
105-
function test_is_exe_which_fullpath(tc)
106-
import matlab.unittest.constraints.IsFile
107-
import stdlib.fileio.which
108-
import stdlib.fileio.is_exe
109-
110-
n = "matlab";
111-
%% is_exe test
112-
p = fullfile(matlabroot, "bin", n);
113-
if ispc
114-
fp = p + ".exe";
115-
else
116-
fp = p;
117-
end
118-
tc.verifyTrue(is_exe(fp))
119-
%% which: test absolute path
120-
exe = which(p);
121-
122-
if ispc
123-
tc.verifyTrue(endsWith(exe, ".exe"))
124-
else
125-
tc.verifyFalse(endsWith(exe, ".exe"))
126-
end
127-
tc.verifyThat(exe, IsFile)
128-
129-
end
130-
13173
function test_with_suffix(tc)
13274
import stdlib.fileio.with_suffix
13375
tc.verifyEqual(with_suffix("foo.h5", ".nc"), "foo.nc")
@@ -143,32 +85,5 @@ function test_with_suffix(tc)
14385
end
14486
end
14587

146-
function test_copyfile(tc)
147-
import matlab.unittest.constraints.IsFile
148-
149-
f1 = tempname;
150-
[~,name] = fileparts(f1);
151-
fclose(fopen(f1,'w'));
152-
stdlib.fileio.copyfile(f1, tempdir)
153-
154-
tc.verifyThat(fullfile(tempdir, name), IsFile)
155-
end
156-
157-
function test_hash(tc)
158-
import matlab.unittest.constraints.IsFile
159-
160-
fn = tempname;
161-
fid = fopen(fn, "w");
162-
tc.assumeGreaterThan(fid, 0);
163-
fprintf(fid, "hello");
164-
fclose(fid);
165-
tc.assumeThat(fn, IsFile)
166-
167-
tc.verifyEqual(stdlib.fileio.md5sum(fn), "5d41402abc4b2a76b9719d911017c592")
168-
tc.verifyEqual(stdlib.fileio.sha256sum(fn), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
169-
170-
delete(fn)
171-
end
172-
17388
end
17489
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

+stdlib/+test/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
int main(void){
4+
printf("Hello\n");
5+
return 0;
6+
}

0 commit comments

Comments
 (0)