Skip to content

Commit 816ae50

Browse files
committed
test use working dir
1 parent 05ea7e2 commit 816ae50

File tree

9 files changed

+61
-26
lines changed

9 files changed

+61
-26
lines changed

+stdlib/is_char_device.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020

2121
function [i, b] = is_char_device(file, backend)
22-
arguments
23-
file
24-
backend (1,:) string = ["python", "sys"]
22+
if nargin < 2
23+
backend = ["python", "sys"];
24+
else
25+
backend = string(backend);
2526
end
2627

2728
i = logical.empty;

+stdlib/makedir.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function makedir(direc)
88
direc = stdlib.expanduser(direc);
99

1010
if ~stdlib.strempty(direc) && ~isfolder(direc)
11-
[~] = mkdir(direc);
11+
% char() is for Matlab < R2018a
12+
[~] = mkdir(char(direc));
1213
end
1314

1415
ok = isfolder(direc);

+stdlib/null_file.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
function nul = null_file()
44

55
if ispc
6-
nul = "NUL";
6+
nul = 'NUL';
77
else
8-
nul = "/dev/null";
8+
nul = '/dev/null';
99
end
1010

1111
end

AGENTS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ The namespace stdlib.native and stdlib.legacy use plain Matlab code, and allow s
1717
The self-test functions under "test/" directory can be used by Matlab >= R2019a as invoked by "test_main.m" at the top level of the project directory.
1818
Matlab >= R2022b can alternatively use "buildtool test" to run the self-tests.
1919

20-
The key limitations to minimum Matlab version are:
20+
Key limitations to minimum Matlab version include:
2121

22+
* R2017b: builtin isfolder(), isfile() available
2223
* R2017b: fileparts() supports string type. fileparts() is used in many places in the code as it's 5-10x faster than regexp() for filename parsing.
2324
* R2018a: fileattrib() supports string type
2425
* R2018a: mfilename('fullpath') tells the full path to the matlab .m file currently running (empty for older Matlab)

example/TestWindowsCOM.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ function test_not(tc, Pn)
1212
end
1313

1414
function test_short_folder(tc)
15-
import matlab.unittest.constraints.IsFolder
1615

1716
progdir = getenv("PROGRAMFILES");
1817
if ispc()
19-
tc.assertThat(progdir, IsFolder, "$Env:PROGRAMFILES is not a directory")
18+
tc.assertThat(progdir, matlab.unittest.constraints.IsFolder, "$Env:PROGRAMFILES is not a directory")
2019
end
2120

2221
short = windows_shortname(progdir);

test/TestBackend.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ function test_dirs(tc)
2222
methods (Test)
2323
function test_backend(tc)
2424
import matlab.unittest.constraints.IsSubsetOf
25-
import matlab.unittest.constraints.IsFile
25+
2626

2727
readme = tc.root + "/Readme.md";
28-
tc.assertThat(readme, IsFile)
28+
if stdlib.matlabOlderThan('R2018a')
29+
tc.assertTrue(isfile(readme))
30+
else
31+
tc.assertThat(readme, matlab.unittest.constraints.IsFile)
32+
end
2933

3034
[i, b] = stdlib.cpu_load();
3135
tc.assertThat(b, IsSubsetOf(stdlib.Backend('cpu_load').backends))
@@ -129,7 +133,11 @@ function test_backend(tc)
129133

130134
[i, b] = stdlib.read_symlink('Readme.lnk');
131135
tc.assertThat(b, IsSubsetOf(stdlib.Backend('read_symlink').backends))
132-
tc.verifyThat(i, IsFile)
136+
if stdlib.matlabOlderThan('R2018a')
137+
tc.assertTrue(isfile(i))
138+
else
139+
tc.assertThat(i, matlab.unittest.constraints.IsFile)
140+
end
133141

134142
[i, b] = stdlib.samepath(readme, readme);
135143
tc.assertThat(b, IsSubsetOf(stdlib.Backend('samepath').backends))

test/TestDisk.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,16 @@ function test_is_dev_drive(tc, B_ps)
134134

135135
function test_remove_file(tc)
136136

137-
f = tempname();
137+
f = "test_remove.tmp";
138138

139139
tc.verifyFalse(stdlib.remove(f), "should not succeed at removing non-existant path")
140140

141-
tc.assumeTrue(stdlib.touch(f), "failed to touch file " + f)
142-
tc.assumeThat(f, matlab.unittest.constraints.IsFile)
141+
tc.assertTrue(stdlib.touch(f), "failed to touch file " + f)
142+
if stdlib.matlabOlderThan('R2018a')
143+
tc.assertTrue(isfile(f))
144+
else
145+
tc.assertThat(f, matlab.unittest.constraints.IsFile)
146+
end
143147

144148
tc.verifyTrue(stdlib.remove(f), "failed to remove file " + f)
145149
end

test/TestFileImpure.m

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2019b', 'impure'}) ...
2+
TestTags = {'R2017b', 'impure'}) ...
33
TestFileImpure < matlab.unittest.TestCase
44

55
properties (TestParameter)
66
ph = {{0, '"stdin"'}, {1, '"stdout"'}, {2, '"stderr"'}, {fopen(tempname()), ''}}
7+
end
78

8-
p_file_size = {mfilename("fullpath") + ".m"}
9+
methods(TestClassSetup)
10+
function test_dirs(tc)
11+
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture())
12+
end
913
end
1014

1115

1216
methods (Test)
1317

14-
function test_file_size(tc, p_file_size)
15-
s = stdlib.file_size(p_file_size);
16-
tc.verifyGreaterThan(s, 0)
18+
function test_file_size(tc)
19+
n = "test_file_size.bin";
20+
fid = fopen(n, "wb");
21+
fwrite(fid, 0, 'uint8');
22+
fclose(fid);
23+
24+
s = stdlib.file_size(n);
25+
tc.verifyEqual(s, 1)
1726
end
1827

1928

2029
function test_null_file(tc)
30+
n = stdlib.null_file;
31+
2132
if ispc()
22-
tc.verifyEqual(stdlib.null_file, "NUL")
33+
tc.verifyEqual(n, "NUL")
34+
elseif stdlib.matlabOlderThan('R2019b')
35+
tc.verifyEqual(n, '/dev/null')
2336
else
24-
tc.verifyThat(stdlib.null_file, matlab.unittest.constraints.IsFile)
37+
tc.verifyThat(n, matlab.unittest.constraints.IsFile)
2538
end
2639
end
2740

2841

2942
function test_makedir(tc)
30-
d = tempname();
43+
d = "test_makedir.dir";
3144
stdlib.makedir(d)
32-
tc.verifyThat(d, matlab.unittest.constraints.IsFolder)
33-
rmdir(d)
45+
46+
if stdlib.matlabOlderThan('R2018a')
47+
tc.assertTrue(isfolder(d))
48+
else
49+
tc.verifyThat(d, matlab.unittest.constraints.IsFolder)
50+
end
3451
end
3552

3653

test/TestHash.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function create_file(tc)
2525
fprintf(fid, "hello");
2626
fclose(fid);
2727

28-
tc.assertThat(tc.file, matlab.unittest.constraints.IsFile)
28+
if stdlib.matlabOlderThan('R2018a')
29+
tc.assumeTrue(isfile(tc.file))
30+
else
31+
tc.assertThat(tc.file, matlab.unittest.constraints.IsFile)
32+
end
2933
end
3034
end
3135

0 commit comments

Comments
 (0)