UNPKG

@cere/rob-cli

Version:

CLI tool for deploying and managing rafts and data sources

192 lines 7.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const yaml_loader_1 = require("../lib/yaml-loader"); // Mock fs module jest.mock('fs', () => ({ existsSync: jest.fn(), readFileSync: jest.fn(), writeFileSync: jest.fn(), })); // Mock path module jest.mock('path', () => ({ resolve: jest.fn((dir, file) => { // Handle paths that might start with / const normalizedFile = file.startsWith('/') ? file.substring(1) : file; return `${dir}/${normalizedFile}`; }), dirname: jest.fn(filePath => filePath.substring(0, filePath.lastIndexOf('/'))), })); // Mock dotenv jest.mock('dotenv', () => ({ config: jest.fn(), })); describe('YAML Loader', () => { beforeEach(() => { jest.clearAllMocks(); // Mock process.cwd() process.cwd = jest.fn().mockReturnValue('/test/dir'); // Mock path.resolve path_1.default.resolve.mockImplementation((dir, file) => { // Handle paths that might start with / const normalizedFile = file.startsWith('/') ? file.substring(1) : file; return `${dir}/${normalizedFile}`; }); }); describe('processEnvVars', () => { beforeEach(() => { process.env.TEST_VAR = 'test-value'; process.env.ANOTHER_VAR = '123'; process.env.COMPLEX_VAR = 'complex-"value"-with-{special}[chars]'; }); afterEach(() => { delete process.env.TEST_VAR; delete process.env.ANOTHER_VAR; delete process.env.COMPLEX_VAR; }); it('should replace environment variables in strings', () => { const input = 'This is a ${TEST_VAR} and ${ANOTHER_VAR}'; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toBe('This is a test-value and 123'); }); it('should replace environment variables in object properties', () => { const input = { name: 'Name: ${TEST_VAR}', value: '${ANOTHER_VAR}', nested: { prop: 'Nested ${TEST_VAR}', }, }; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toEqual({ name: 'Name: test-value', value: '123', nested: { prop: 'Nested test-value', }, }); }); it('should replace environment variables in arrays', () => { const input = ['${TEST_VAR}', 'static', '${ANOTHER_VAR}']; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toEqual(['test-value', 'static', '123']); }); it('should handle non-existing environment variables', () => { const input = 'This is ${NON_EXISTENT_VAR}'; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toBe('This is '); }); it('should handle environment variables with special characters', () => { const input = 'Special chars: ${COMPLEX_VAR}'; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toBe('Special chars: complex-"value"-with-{special}[chars]'); }); it('should handle multiple occurrences of the same environment variable', () => { const input = 'Double ${TEST_VAR} and again ${TEST_VAR}'; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toBe('Double test-value and again test-value'); }); it('should handle environment variables in complex nested structures', () => { const input = { config: { database: { username: '${TEST_VAR}', password: '${ANOTHER_VAR}', }, settings: [ '${TEST_VAR}', { nested: '${ANOTHER_VAR}', }, ], }, }; const result = (0, yaml_loader_1.processEnvVars)(input); expect(result).toEqual({ config: { database: { username: 'test-value', password: '123', }, settings: [ 'test-value', { nested: '123', }, ], }, }); }); }); describe('loadYamlFile', () => { beforeEach(() => { process.env.TEST_VAR = 'test-value'; process.env.DB_PASSWORD = 'secure-password'; }); afterEach(() => { delete process.env.TEST_VAR; delete process.env.DB_PASSWORD; }); it('should load and parse a YAML file', () => { const mockYamlContent = ` name: Test values: - 1 - 2 - 3 config: enabled: true `; fs_1.default.existsSync.mockReturnValue(true); fs_1.default.readFileSync.mockReturnValue(mockYamlContent); const result = (0, yaml_loader_1.loadYamlFile)('/test/file.yaml'); expect(fs_1.default.existsSync).toHaveBeenCalledWith('/test/dir/test/file.yaml'); expect(fs_1.default.readFileSync).toHaveBeenCalledWith('/test/dir/test/file.yaml', 'utf8'); expect(result).toEqual({ name: 'Test', values: [1, 2, 3], config: { enabled: true, }, }); }); it('should process environment variables in the loaded YAML file', () => { const mockYamlContent = ` name: \${TEST_VAR} database: username: admin password: \${DB_PASSWORD} `; fs_1.default.existsSync.mockReturnValue(true); fs_1.default.readFileSync.mockReturnValue(mockYamlContent); const result = (0, yaml_loader_1.loadYamlFile)('/test/file.yaml'); expect(result).toEqual({ name: 'test-value', database: { username: 'admin', password: 'secure-password', }, }); }); // it('should throw an error if the file does not exist', () => { // (fs.existsSync as jest.Mock).mockReturnValue(false); // // expect(() => loadYamlFile('/test/non-existent.yaml')).toThrow('File not found'); // }); }); describe('saveYamlFile', () => { it('should save data to a YAML file', () => { const data = { name: 'Test', values: [1, 2, 3], }; (0, yaml_loader_1.saveYamlFile)('/test/output.yaml', data); expect(fs_1.default.writeFileSync).toHaveBeenCalledWith('/test/dir/test/output.yaml', expect.any(String), 'utf8'); }); }); }); //# sourceMappingURL=yaml-loader.test.js.map