|
| 1 | +/** |
| 2 | + * Isolated test for chownRecursive that mocks fs to test the traversal logic. |
| 3 | + * This is in a separate file because the main ssl-bump.test.ts uses real fs operations, |
| 4 | + * and Node.js fs.chownSync is non-configurable (can't be spied on or redefined). |
| 5 | + */ |
| 6 | + |
| 7 | +import * as path from 'path'; |
| 8 | + |
| 9 | +// Mock fs before importing the module under test |
| 10 | +const mockChownSync = jest.fn(); |
| 11 | +const mockReaddirSync = jest.fn(); |
| 12 | +jest.mock('fs', () => { |
| 13 | + const actual = jest.requireActual('fs'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + chownSync: (...args: unknown[]) => mockChownSync(...args), |
| 17 | + readdirSync: (...args: unknown[]) => { |
| 18 | + // Only intercept calls with { withFileTypes: true } (from chownRecursive) |
| 19 | + if (args[1] && typeof args[1] === 'object' && 'withFileTypes' in args[1]) { |
| 20 | + return mockReaddirSync(...args); |
| 21 | + } |
| 22 | + return actual.readdirSync(...args); |
| 23 | + }, |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +// Mock execa (required by ssl-bump module) |
| 28 | +jest.mock('execa'); |
| 29 | + |
| 30 | +import { chownRecursive } from './ssl-bump'; |
| 31 | + |
| 32 | +describe('chownRecursive', () => { |
| 33 | + beforeEach(() => { |
| 34 | + mockChownSync.mockReset(); |
| 35 | + mockReaddirSync.mockReset(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should chown the directory itself', () => { |
| 39 | + mockReaddirSync.mockReturnValue([]); |
| 40 | + |
| 41 | + chownRecursive('/some/dir', 13, 13); |
| 42 | + |
| 43 | + expect(mockChownSync).toHaveBeenCalledWith('/some/dir', 13, 13); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should chown files in the directory', () => { |
| 47 | + mockReaddirSync.mockReturnValue([ |
| 48 | + { name: 'file1.txt', isDirectory: () => false }, |
| 49 | + { name: 'file2.txt', isDirectory: () => false }, |
| 50 | + ]); |
| 51 | + |
| 52 | + chownRecursive('/some/dir', 13, 13); |
| 53 | + |
| 54 | + expect(mockChownSync).toHaveBeenCalledTimes(3); // dir + 2 files |
| 55 | + expect(mockChownSync).toHaveBeenCalledWith('/some/dir', 13, 13); |
| 56 | + expect(mockChownSync).toHaveBeenCalledWith(path.join('/some/dir', 'file1.txt'), 13, 13); |
| 57 | + expect(mockChownSync).toHaveBeenCalledWith(path.join('/some/dir', 'file2.txt'), 13, 13); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should recursively chown subdirectories', () => { |
| 61 | + // Root dir has a subdir and a file |
| 62 | + mockReaddirSync |
| 63 | + .mockReturnValueOnce([ |
| 64 | + { name: 'subdir', isDirectory: () => true }, |
| 65 | + { name: 'root-file.txt', isDirectory: () => false }, |
| 66 | + ]) |
| 67 | + // Subdir has one file |
| 68 | + .mockReturnValueOnce([ |
| 69 | + { name: 'sub-file.txt', isDirectory: () => false }, |
| 70 | + ]); |
| 71 | + |
| 72 | + chownRecursive('/root', 13, 13); |
| 73 | + |
| 74 | + expect(mockChownSync).toHaveBeenCalledTimes(4); // root + subdir + root-file + sub-file |
| 75 | + expect(mockChownSync).toHaveBeenCalledWith('/root', 13, 13); |
| 76 | + expect(mockChownSync).toHaveBeenCalledWith(path.join('/root', 'subdir'), 13, 13); |
| 77 | + expect(mockChownSync).toHaveBeenCalledWith(path.join('/root', 'root-file.txt'), 13, 13); |
| 78 | + expect(mockChownSync).toHaveBeenCalledWith(path.join('/root', 'subdir', 'sub-file.txt'), 13, 13); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle empty directory', () => { |
| 82 | + mockReaddirSync.mockReturnValue([]); |
| 83 | + |
| 84 | + chownRecursive('/empty', 1000, 1000); |
| 85 | + |
| 86 | + expect(mockChownSync).toHaveBeenCalledTimes(1); // just the dir itself |
| 87 | + expect(mockChownSync).toHaveBeenCalledWith('/empty', 1000, 1000); |
| 88 | + }); |
| 89 | +}); |
0 commit comments