@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
22import { describe , it } from "node:test" ;
33import path from "node:path" ;
44import fs from "node:fs" ;
5+ import fswin from "fswin" ;
56
67import {
78 determineModuleContext ,
@@ -13,6 +14,40 @@ import {
1314} from "./path-utils.js" ;
1415import { setupTempDirectory } from "./test-utils.js" ;
1516
17+ 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 {
29+ // Unix-like: clear all perms
30+ fs . chmodSync ( p , 0 ) ;
31+ }
32+ }
33+
34+ 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 {
46+ // Unix-like: clear all perms
47+ fs . chmodSync ( p , 0o700 ) ;
48+ }
49+ }
50+
1651describe ( "isNodeApiModule" , ( ) => {
1752 it ( "returns true for .node" , ( context ) => {
1853 const tempDirectoryPath = setupTempDirectory ( context , {
@@ -28,14 +63,14 @@ describe("isNodeApiModule", () => {
2863 "addon.android.node" : "" ,
2964 } ) ;
3065 // remove read permissions on directory
31- fs . chmodSync ( tempDirectoryPath , 0 ) ;
66+ removeReadPermissions ( tempDirectoryPath ) ;
3267 try {
3368 assert . equal (
3469 isNodeApiModule ( path . join ( tempDirectoryPath , "addon" ) ) ,
3570 false
3671 ) ;
3772 } finally {
38- fs . chmodSync ( tempDirectoryPath , 0o700 ) ;
73+ restoreReadPermissions ( tempDirectoryPath ) ;
3974 }
4075 } ) ;
4176
@@ -45,14 +80,14 @@ describe("isNodeApiModule", () => {
4580 } ) ;
4681 const candidate = path . join ( tempDirectoryPath , "addon.android.node" ) ;
4782 // remove read permission on file
48- fs . chmodSync ( candidate , 0 ) ;
83+ removeReadPermissions ( candidate ) ;
4984 try {
5085 assert . throws (
5186 ( ) => isNodeApiModule ( path . join ( tempDirectoryPath , "addon" ) ) ,
5287 / F o u n d a n u n r e a d a b l e m o d u l e a d d o n \. a n d r o i d \. n o d e /
5388 ) ;
5489 } finally {
55- fs . chmodSync ( candidate , 0o600 ) ;
90+ restoreReadPermissions ( candidate ) ;
5691 }
5792 } ) ;
5893
@@ -81,12 +116,12 @@ describe("isNodeApiModule", () => {
81116 } ) ;
82117 const unreadable = path . join ( tempDirectoryPath , "addon.android.node" ) ;
83118 // only android module is unreadable
84- fs . chmodSync ( unreadable , 0 ) ;
119+ removeReadPermissions ( unreadable ) ;
85120 assert . throws (
86121 ( ) => isNodeApiModule ( path . join ( tempDirectoryPath , "addon" ) ) ,
87122 / F o u n d a n u n r e a d a b l e m o d u l e a d d o n \. a n d r o i d \. n o d e /
88123 ) ;
89- fs . chmodSync ( unreadable , 0o600 ) ;
124+ restoreReadPermissions ( unreadable ) ;
90125 } ) ;
91126} ) ;
92127
0 commit comments