Skip to content

Commit 1f89e28

Browse files
committed
I've tried many, many ways of doing this without shelling out to powershell in some brittle way, and it just doesn't appear feasible for the unreadable directory case. The other unreadable file case is covered.
1 parent 8eb426d commit 1f89e28

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

packages/react-native-node-api-modules/src/node/path-utils.test.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,38 @@ import {
1515
import { setupTempDirectory } from "./test-utils.js";
1616

1717
function removeReadPermissions(p: string) {
18-
if (process.platform === "win32") {
19-
// Windows: simulate unreadable by setting file to offline
20-
const attributes = {
21-
IS_READ_ONLY: true,
22-
IS_OFFLINE: true,
23-
IS_TEMPORARY: true,
24-
};
25-
26-
const result = fswin.setAttributesSync(p, attributes);
27-
if (!result) console.error('!!!!! can not set attributes');
28-
} else {
18+
if (process.platform !== "win32") {
2919
// Unix-like: clear all perms
3020
fs.chmodSync(p, 0);
21+
return;
3122
}
23+
24+
// Windows: simulate unreadable by setting file to offline
25+
const attributes = {
26+
IS_READ_ONLY: true,
27+
IS_OFFLINE: true,
28+
IS_TEMPORARY: true,
29+
};
30+
31+
const result = fswin.setAttributesSync(p, attributes);
32+
if (!result) console.error('!!!!! can not set attributes to remove read permissions');
3233
}
3334

3435
function restoreReadPermissions(p: string) {
35-
if (process.platform === "win32") {
36-
// Windows: simulate unreadable by setting file to offline
37-
const attributes = {
38-
IS_READ_ONLY: false,
39-
IS_OFFLINE: false,
40-
IS_TEMPORARY: false,
41-
};
42-
43-
const result = fswin.setAttributesSync(p, attributes);
44-
if (!result) console.error('!!!!! can not set attributes');
45-
} else {
36+
if (process.platform !== "win32") {
4637
// Unix-like: clear all perms
4738
fs.chmodSync(p, 0o700);
39+
return;
4840
}
41+
42+
const attributes = {
43+
IS_READ_ONLY: false,
44+
IS_OFFLINE: false,
45+
IS_TEMPORARY: false,
46+
};
47+
48+
const result = fswin.setAttributesSync(p, attributes);
49+
if (!result) console.error('!!!!! can not set attributes to restore read permissions');
4950
}
5051

5152
describe("isNodeApiModule", () => {
@@ -58,11 +59,11 @@ describe("isNodeApiModule", () => {
5859
assert(isNodeApiModule(path.join(tempDirectoryPath, "addon.node")));
5960
});
6061

61-
it("returns false when directory cannot be read due to permissions", (context) => {
62+
// there is no way to set ACLs on directories in Node.js on Windows with brittle powershell commands
63+
(process.platform === "win32" ? it.skip : it)("returns false when directory cannot be read due to permissions", (context) => {
6264
const tempDirectoryPath = setupTempDirectory(context, {
6365
"addon.android.node": "",
6466
});
65-
// remove read permissions on directory
6667
removeReadPermissions(tempDirectoryPath);
6768
try {
6869
assert.equal(
@@ -79,7 +80,6 @@ describe("isNodeApiModule", () => {
7980
"addon.android.node": "",
8081
});
8182
const candidate = path.join(tempDirectoryPath, "addon.android.node");
82-
// remove read permission on file
8383
removeReadPermissions(candidate);
8484
try {
8585
assert.throws(

0 commit comments

Comments
 (0)