Skip to content

Commit 364ded2

Browse files
committed
test: use createTemporaryFolder()
test symlink: more robust tempdir use
1 parent 756f758 commit 364ded2

File tree

7 files changed

+37
-52
lines changed

7 files changed

+37
-52
lines changed

test/TestHDF5.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
methods(TestClassSetup)
1515
function setup_file(tc)
16-
import matlab.unittest.fixtures.TemporaryFolderFixture
1716
import matlab.unittest.constraints.IsFile
18-
fixture = tc.applyFixture(TemporaryFolderFixture);
17+
18+
td = stdlib.posix(tc.createTemporaryFolder());
1919

2020
A0 = 42.;
2121
A1 = [42.; 43.];
@@ -34,7 +34,7 @@ function setup_file(tc)
3434
tc.TestData.utf = utf;
3535
tc.TestData.utf2 = utf2;
3636

37-
tc.TestData.basic = stdlib.posix(fixture.Folder) + "/basic.h5";
37+
tc.TestData.basic = td + "/basic.h5";
3838
bf = tc.TestData.basic;
3939

4040
% create test data first, so that parallel tests works

test/TestHash.m

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
classdef TestHash < matlab.unittest.TestCase
22

3-
properties
4-
Folder
5-
end
6-
73
properties (TestParameter)
84
type = {'sha256', 'md5'}
95
hash = {"36c1bbbdfd8d04ef546ffb15b9c0a65767fd1fe9a6135a257847e3a51fb1426c", "d58cfb32e075781ba59082a8b18287f9"}
@@ -16,12 +12,6 @@ function java_required(tc)
1612
end
1713
end
1814

19-
methods(TestMethodSetup)
20-
function setup_tempdir(tc)
21-
tc.Folder = stdlib.posix(tc.createTemporaryFolder());
22-
end
23-
end
24-
2515

2616
methods (Test, ParameterCombination = 'sequential')
2717

@@ -35,8 +25,10 @@ function test_extract(tc)
3525

3626
tc.assumeNotEmpty(stdlib.which("cmake"), "CMake not available")
3727

38-
stdlib.extract_zstd(fn, tc.Folder)
39-
tc.verifyThat( tc.Folder + "/test/hello.txt", IsFile)
28+
td = stdlib.posix(tc.createTemporaryFolder());
29+
30+
stdlib.extract_zstd(fn, td)
31+
tc.verifyThat(td + "/test/hello.txt", IsFile)
4032

4133
end
4234

@@ -59,7 +51,9 @@ function test_hash(tc, type, hash)
5951
function test_hash_text(tc)
6052
import matlab.unittest.constraints.IsFile
6153

62-
fn = tc.Folder + "/hello";
54+
td = stdlib.posix(tc.createTemporaryFolder());
55+
56+
fn = td + "/hello";
6357
fid = fopen(fn, "w");
6458
tc.addTeardown(@fclose, fid)
6559

test/TestJava.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,10 @@ function test_is_regular_file(tc)
127127

128128

129129
function test_touch_modtime(tc)
130-
import matlab.unittest.fixtures.TemporaryFolderFixture
131130

132-
fixture = tc.applyFixture(TemporaryFolderFixture);
131+
tf = stdlib.posix(tc.createTemporaryFolder());
133132

134-
fn = fullfile(fixture.Folder, "modtime.txt");
133+
fn = fullfile(tf, "modtime.txt");
135134

136135
tc.verifyTrue(stdlib.touch(fn, datetime("yesterday")))
137136
t0 = stdlib.get_modtime(fn);

test/TestNetCDF.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
function setup_file(tc)
1010
import matlab.unittest.constraints.IsFile
11-
import matlab.unittest.fixtures.TemporaryFolderFixture
1211

13-
fixture = tc.applyFixture(TemporaryFolderFixture);
12+
td = stdlib.posix(tc.createTemporaryFolder());
1413

1514
A0 = 42.;
1615
A1 = [42.; 43.];
@@ -31,7 +30,7 @@ function setup_file(tc)
3130
tc.TestData.utf1 = utf1;
3231
tc.TestData.utf2 = utf2;
3332

34-
basic = stdlib.posix(fixture.Folder) + "/basic.nc";
33+
basic = td + "/basic.nc";
3534
tc.TestData.basic = basic;
3635

3736
% create test data first, so that parallel tests works

test/TestPermissions.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ function test_get_permissions(tc, Ps)
2626
function test_set_permissions(tc)
2727

2828
import matlab.unittest.constraints.StartsWithSubstring
29-
import matlab.unittest.fixtures.TemporaryFolderFixture
3029
import matlab.unittest.fixtures.CurrentFolderFixture
3130

3231
tc.applyFixture(CurrentFolderFixture(".."))
3332
% matlab exist() doesn't work for MEX detection with ".." leading path
3433

3534
tc.assumeEqual(exist("+stdlib/set_permissions", "file"), 3)
3635

37-
fixture = tc.applyFixture(TemporaryFolderFixture);
36+
tf = stdlib.posix(tc.createTemporaryFolder());
3837

39-
nr = fullfile(fixture.Folder, "no-read");
38+
nr = fullfile(tf, "no-read");
4039

4140
tc.verifyTrue(stdlib.touch(nr))
4241
tc.verifyTrue(stdlib.set_permissions(nr, -1, 0, 0))
@@ -46,7 +45,7 @@ function test_set_permissions(tc)
4645
tc.verifyThat(p, StartsWithSubstring("-"), "no-read permission failed to set")
4746
end
4847

49-
nw = fullfile(fixture.Folder, "no-write");
48+
nw = fullfile(tf, "no-write");
5049

5150
tc.verifyTrue(stdlib.touch(nw))
5251
tc.verifyTrue(stdlib.set_permissions(nw, 0, -1, 0))

test/TestSubprocess.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@ function test_simple_run(tc)
2626

2727

2828
function test_cwd(tc)
29-
import matlab.unittest.fixtures.TemporaryFolderFixture
3029

3130
if ispc
3231
c = ["cmd", "/c", "dir"];
3332
else
3433
c = ["ls", "-l"];
3534
end
3635

37-
fixture = tc.applyFixture(TemporaryFolderFixture);
38-
td = fixture.Folder;
39-
4036
[s, m, e] = stdlib.subprocess_run(c);
4137
tc.assertEqual(s, 0, "status non-zero")
4238
tc.verifyGreaterThan(strlength(m), 0, "empty directory not expected")
4339
tc.verifyEqual(strlength(e), 0, e)
4440

41+
td = stdlib.posix(tc.createTemporaryFolder());
42+
4543
[s, mc, e] = stdlib.subprocess_run(c, "cwd", td);
4644
tc.assertEqual(s, 0, "status non-zero")
4745
tc.verifyNotEqual(m, mc, "expected different directory to have different contents")

test/TestSymlink.m

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
classdef TestSymlink < matlab.unittest.TestCase
22

33
properties
4-
d
4+
target
5+
link
6+
tempDir
57
end
68

79
properties (TestParameter)
@@ -10,30 +12,24 @@
1012

1113

1214
methods(TestClassSetup)
13-
1415
function setup_symlink(tc)
15-
import matlab.unittest.fixtures.CurrentFolderFixture
16-
import matlab.unittest.fixtures.TemporaryFolderFixture
17-
18-
fixture = tc.applyFixture(TemporaryFolderFixture);
19-
td = stdlib.posix(fixture.Folder);
20-
tc.applyFixture(CurrentFolderFixture(td))
16+
tc.tempDir = stdlib.posix(tc.createTemporaryFolder());
2117

22-
tc.d.link = td + "/my.lnk";
18+
tc.link = tc.tempDir + "/my.lnk";
2319

24-
tc.d.this = stdlib.posix(mfilename("fullpath") + ".m");
25-
26-
tc.assumeTrue(stdlib.create_symlink(tc.d.this, tc.d.link), ...
27-
"failed to create test link " + tc.d.link)
20+
tc.target = stdlib.posix(mfilename("fullpath") + ".m");
2821

22+
tc.assumeTrue(stdlib.create_symlink(tc.target, tc.link), ...
23+
"failed to create test link " + tc.link)
2924
end
3025
end
3126

3227

3328
methods (Test)
3429

3530
function test_is_symlink(tc, p)
36-
tc.verifyEqual(stdlib.is_symlink(p{1}), p{2})
31+
tc.verifyTrue(stdlib.is_symlink(tc.link), "failed to detect own link")
32+
tc.verifyEqual(stdlib.is_symlink(p{1}), p{2}, p{1})
3733
end
3834

3935

@@ -43,32 +39,32 @@ function test_read_symlink(tc)
4339
tc.verifyEqual(stdlib.read_symlink(""), "")
4440
tc.verifyEqual(stdlib.read_symlink("file:///"), "")
4541
tc.verifyEqual(stdlib.read_symlink("not-exist"), "")
46-
tc.verifyEqual(stdlib.read_symlink(tc.d.this), "")
42+
tc.verifyEqual(stdlib.read_symlink(tc.target), "")
4743

48-
t = stdlib.read_symlink(tc.d.link);
44+
t = stdlib.read_symlink(tc.link);
4945
tc.verifyNotEmpty(t)
5046
tc.verifyThat(t, IsOfClass('string'))
51-
tc.verifyEqual(tc.d.this, t)
47+
tc.verifyEqual(tc.target, t)
5248

5349
end
5450

5551

5652
function test_create_symlink(tc)
57-
5853
tc.verifyFalse(stdlib.create_symlink("", tempname))
5954
tc.verifyFalse(stdlib.create_symlink("file:///", tempname))
60-
tc.verifyFalse(stdlib.create_symlink(tc.d.this, tc.d.link))
61-
tc.verifyTrue(stdlib.create_symlink(tc.d.this, "another.lnk"))
62-
tc.verifyTrue(stdlib.is_symlink("another.lnk"))
55+
tc.verifyFalse(stdlib.create_symlink(tc.target, tc.link), "should fail for existing symlink")
6356

57+
ano = tc.tempDir + "/another.lnk";
58+
tc.verifyTrue(stdlib.create_symlink(tc.target, ano))
59+
tc.verifyTrue(stdlib.is_symlink(ano))
6460
end
6561

6662
end
6763
end
6864

6965

7066
function p = init_symlink()
71-
p = {{"not-exist", false}, {"my.lnk", true}, ...
67+
p = {{"not-exist", false}, ...
7268
{mfilename("fullpath") + ".m", false}, ...
7369
{"", false}, ...
7470
{"file:///", false}};

0 commit comments

Comments
 (0)