Skip to content

Commit 36a142a

Browse files
jhughes-mwclaude
andcommitted
Make read-only permission tests work on Windows
The read-only-folder approach does not fail on Windows: the directory read-only attribute is ignored by the OS for file creation, so the .zgroup / .zattrs write succeeded and no error was raised. (The original noWritePermissions test passed on Windows only incidentally, because it marked a pre-existing .zattrs *file* read-only via recursion.) Make the specific target file read-only instead of its folder. A read-only file is honored on both Windows and Unix, so the open-for- write failure fires on all platforms. Write permission is restored on teardown so the fixture can clean up. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5a8c3bb commit 36a142a

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

test/tZarr.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,24 @@ function verifyCreateGroupMakesFolder(testcase)
9999

100100
function verifyCreateGroupOpenFailure(testcase)
101101
% Verify error when the .zgroup file cannot be opened for
102-
% writing (e.g. the target folder is read-only). The read-only
103-
% folder lives inside an isolated temporary folder fixture, so
104-
% no real data is modified and cleanup is automatic (removal
105-
% succeeds via the writable parent, so no permission restore is
106-
% needed).
102+
% writing. Everything lives inside an isolated temporary folder
103+
% fixture, so no real data is modified.
104+
%
105+
% We make the existing .zgroup *file* read-only rather than its
106+
% folder: a read-only folder does not prevent file creation on
107+
% Windows (the directory read-only attribute is ignored there),
108+
% whereas a read-only file is honored on both Windows and Unix.
107109
import matlab.unittest.fixtures.TemporaryFolderFixture
108110
tempFixture = testcase.applyFixture(TemporaryFolderFixture);
109111

110112
groupPath = fullfile(tempFixture.Folder, "readOnlyGroup");
111-
mkdir(groupPath);
112-
fileattrib(groupPath, '-w', '', 's');
113+
Zarr.createGroup(groupPath); % writes .zgroup
114+
zgroupFile = fullfile(groupPath, ".zgroup");
115+
116+
fileattrib(zgroupFile, '-w');
117+
% Restore write permission before the fixture is torn down so its
118+
% contents can be removed (runs before the fixture's rmdir).
119+
testcase.addTeardown(@()fileattrib(zgroupFile, '+w'));
113120

114121
testcase.verifyError(@()Zarr.createGroup(groupPath),...
115122
"MATLAB:Zarr:fileOpenFailure");

test/tZarrAttributes.m

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,28 @@ function notZarrObject(testcase)
110110
end
111111

112112
function noWritePermissions(testcase)
113-
% Verify error if there are no write permissions to the Zarr array.
114-
% Create the array inside an isolated temporary folder fixture so
115-
% no shared fixture data is modified and cleanup is automatic
116-
% (removal succeeds via the writable parent, so no permission
117-
% restore is needed).
113+
% Verify error if the .zattrs file cannot be opened for writing.
114+
% Everything lives inside an isolated temporary folder fixture so
115+
% no shared fixture data is modified.
116+
%
117+
% We make the existing .zattrs *file* read-only rather than its
118+
% folder: a read-only folder does not prevent file creation on
119+
% Windows (the directory read-only attribute is ignored there),
120+
% whereas a read-only file is honored on both Windows and Unix.
118121
import matlab.unittest.fixtures.TemporaryFolderFixture
119122
tempFixture = testcase.applyFixture(TemporaryFolderFixture);
120123

121124
arrPath = fullfile(tempFixture.Folder, "roArr");
122125
zarrcreate(arrPath, testcase.ArrSize);
123-
124-
% Make the array folder read-only.
125-
fileattrib(arrPath,'-w','','s');
126+
% Write one attribute so the .zattrs file exists, then make it
127+
% read-only so the next write cannot open it.
128+
zarrwriteatt(arrPath, 'existingAttr', 1);
129+
zattrsFile = fullfile(arrPath, '.zattrs');
130+
131+
fileattrib(zattrsFile, '-w');
132+
% Restore write permission before the fixture is torn down so its
133+
% contents can be removed (runs before the fixture's rmdir).
134+
testcase.addTeardown(@()fileattrib(zattrsFile, '+w'));
126135

127136
errID = 'MATLAB:zarrwriteatt:fileOpenFailure';
128137
testcase.verifyError(@()zarrwriteatt(arrPath,'myAttr','attrVal'), ...

0 commit comments

Comments
 (0)