|
| 1 | +import { create } from '../../../__tests__/util'; |
| 2 | +import { AMODE } from '../../../consts/AMODE'; |
| 3 | + |
| 4 | +describe('chmod 0 permission issue reproduction', () => { |
| 5 | + describe('existsSync()', () => { |
| 6 | + test('chmod on file and existsSync', () => { |
| 7 | + // Arrange |
| 8 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 9 | + vol.chmodSync('/path/to/file.txt', 0o0000); |
| 10 | + |
| 11 | + // Act |
| 12 | + const exists = vol.existsSync('/path/to/file.txt'); |
| 13 | + const exists2 = vol.existsSync('/path/to/file2.txt'); |
| 14 | + const exists3 = vol.existsSync('/path/to3/file3.txt'); |
| 15 | + |
| 16 | + // Assert |
| 17 | + expect(exists).toBe(true); |
| 18 | + expect(exists2).toBe(false); |
| 19 | + expect(exists3).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + test('chmod on directory and existsSync', () => { |
| 23 | + // Arrange |
| 24 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 25 | + vol.chmodSync('/path/to/', 0o0000); |
| 26 | + |
| 27 | + // Act |
| 28 | + const exists = vol.existsSync('/path/to/file.txt'); |
| 29 | + |
| 30 | + // Assert |
| 31 | + expect(exists).toBe(false); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + test.each([AMODE.R_OK, AMODE.W_OK])('chmod on file and access mode %d', mode => { |
| 36 | + // Arrange |
| 37 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 38 | + vol.accessSync('/path/to/file.txt', mode); |
| 39 | + vol.chmodSync('/path/to/file.txt', 0o0000); |
| 40 | + |
| 41 | + // Act & Assert |
| 42 | + expect(() => { |
| 43 | + vol.accessSync('/path/to/file.txt', mode); |
| 44 | + }).toThrow(); |
| 45 | + }); |
| 46 | + |
| 47 | + test.each([AMODE.R_OK, AMODE.W_OK])('chmod on directory and access mode %d', mode => { |
| 48 | + // Arrange |
| 49 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 50 | + vol.accessSync('/path/to/file.txt', mode); |
| 51 | + vol.chmodSync('/path/to/', 0o0000); |
| 52 | + |
| 53 | + // Act & Assert |
| 54 | + expect(() => { |
| 55 | + vol.accessSync('/path/to/file.txt', mode); |
| 56 | + }).toThrow(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('chmod on file and access F_OK should not throw', () => { |
| 60 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 61 | + vol.accessSync('/path/to/file.txt', AMODE.F_OK); |
| 62 | + vol.chmodSync('/path/to/file.txt', 0o0000); |
| 63 | + vol.accessSync('/path/to/file.txt', AMODE.F_OK); |
| 64 | + }); |
| 65 | + |
| 66 | + // Test the exact scenario from the issue (https://github.com/streamich/memfs/issues/1172) |
| 67 | + test('chmod on file and promises access with fs.constants', async () => { |
| 68 | + const vol = create({ '/path/to/file.txt': 'some text' }); |
| 69 | + |
| 70 | + await vol.promises.chmod('/path/to/file.txt', 0o0000); |
| 71 | + |
| 72 | + // Should throw for R_OK and W_OK |
| 73 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.R_OK)).rejects.toThrow(); |
| 74 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.R_OK | AMODE.F_OK)).rejects.toThrow(); |
| 75 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.W_OK)).rejects.toThrow(); |
| 76 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.W_OK | AMODE.F_OK)).rejects.toThrow(); |
| 77 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.W_OK | AMODE.R_OK)).rejects.toThrow(); |
| 78 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.W_OK | AMODE.R_OK | AMODE.F_OK)).rejects.toThrow(); |
| 79 | + |
| 80 | + // F_OK should NOT throw - it just checks existence, which it does exist |
| 81 | + await expect(vol.promises.access('/path/to/file.txt', AMODE.F_OK)).resolves.toBeUndefined(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments