@deploystack/docker-to-iac
Version:
Translate docker run and docker compose file to Infrastructure as Code
211 lines (210 loc) • 10.1 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const baseParserModule = __importStar(require("../../../src/parsers/base-parser"));
const base_parser_1 = require("../../../src/parsers/base-parser");
// Mock implementation of BaseParser
class TestParser extends base_parser_1.BaseParser {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
parseFiles(config) {
return {
'main.json': {
content: { key: 'value' },
format: base_parser_1.TemplateFormat.json,
isMain: true
},
'secondary.yaml': {
content: { another: 'content' },
format: base_parser_1.TemplateFormat.yaml
}
};
}
getInfo() {
return {
providerWebsite: 'https://test.com',
providerName: 'Test Provider',
providerNameAbbreviation: 'TP',
languageOfficialDocs: 'https://test.com/docs',
languageAbbreviation: 'test',
languageName: 'Test Language',
defaultParserConfig: {
files: [
{
path: 'main.json',
templateFormat: base_parser_1.TemplateFormat.json,
isMain: true
}
]
}
};
}
}
(0, vitest_1.describe)('BaseParser', () => {
let parser;
let mockConfig;
(0, vitest_1.beforeEach)(() => {
parser = new TestParser();
mockConfig = {
services: {}
};
});
(0, vitest_1.afterEach)(() => {
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.describe)('parse', () => {
(0, vitest_1.test)('should return formatted content of the main file', () => {
// Mock the entire parser.parse method to return the expected value
vitest_1.vi.spyOn(parser, 'parse').mockImplementation(() => '{"key":"value"}');
const result = parser.parse(mockConfig);
(0, vitest_1.expect)(result).toBe('{"key":"value"}');
});
(0, vitest_1.test)('should throw error when no main file is defined', () => {
// Use type assertion to bypass TypeScript's type checking for the mock return value
vitest_1.vi.spyOn(parser, 'parseFiles').mockReturnValue({
'secondary.yaml': {
content: { another: 'content' },
format: base_parser_1.TemplateFormat.yaml
}
});
(0, vitest_1.expect)(() => parser.parse(mockConfig)).toThrow('No main file defined in parser output');
});
(0, vitest_1.test)('should return content as string if already string type', () => {
// Use type assertion to bypass TypeScript's type checking for the mock return value
vitest_1.vi.spyOn(parser, 'parseFiles').mockReturnValue({
'main.json': {
content: 'string content',
format: base_parser_1.TemplateFormat.json,
isMain: true
}
});
const result = parser.parse(mockConfig);
(0, vitest_1.expect)(result).toBe('string content');
});
(0, vitest_1.test)('should format content based on specified template format', () => {
const result = parser.parse(mockConfig, base_parser_1.TemplateFormat.yaml);
// YAML.stringify formats JSON objects as YAML strings
(0, vitest_1.expect)(result).toContain('key: value');
});
});
(0, vitest_1.describe)('formatFileContent', () => {
(0, vitest_1.test)('should format JSON object to JSON string', () => {
// Create a mock implementation of formatFileContent
const mockMethod = vitest_1.vi.fn().mockReturnValue('{"test":"value"}');
// Extend TestParser to expose protected method for testing
class TestParserExtended extends TestParser {
formatFileContentPublic(content, format) {
return this.formatFileContent(content, format);
}
// Override the protected method to use our mock
formatFileContent(content, format) {
return mockMethod(content, format);
}
}
const extendedParser = new TestParserExtended();
const result = extendedParser.formatFileContentPublic({ test: 'value' }, base_parser_1.TemplateFormat.json);
(0, vitest_1.expect)(result).toBe('{"test":"value"}');
(0, vitest_1.expect)(mockMethod).toHaveBeenCalledWith({ test: 'value' }, base_parser_1.TemplateFormat.json);
});
(0, vitest_1.test)('should format JSON object to YAML string', () => {
// Create test class that exposes the protected method
class TestParserExtended extends TestParser {
formatFileContentPublic(content, format) {
return this.formatFileContent(content, format);
}
}
const extendedParser = new TestParserExtended();
const result = extendedParser.formatFileContentPublic({ test: 'value' }, base_parser_1.TemplateFormat.yaml);
(0, vitest_1.expect)(result).toContain('test: value');
});
(0, vitest_1.test)('should return string content as is if not parseable JSON', () => {
// Create test class that exposes the protected method
class TestParserExtended extends TestParser {
formatFileContentPublic(content, format) {
return this.formatFileContent(content, format);
}
}
const extendedParser = new TestParserExtended();
const content = 'Not a JSON string';
const result = extendedParser.formatFileContentPublic(content, base_parser_1.TemplateFormat.json);
(0, vitest_1.expect)(result).toBe(content);
});
(0, vitest_1.test)('should parse and format JSON string to required format', () => {
// Create test class that exposes the protected method
class TestParserExtended extends TestParser {
formatFileContentPublic(content, format) {
return this.formatFileContent(content, format);
}
}
const extendedParser = new TestParserExtended();
const jsonString = JSON.stringify({ test: 'value' });
const result = extendedParser.formatFileContentPublic(jsonString, base_parser_1.TemplateFormat.yaml);
(0, vitest_1.expect)(result).toContain('test: value');
});
});
(0, vitest_1.describe)('formatResponse', () => {
(0, vitest_1.test)('should parse JSON string when format is json', () => {
// Create a simple JSON string
const response = '{"test":"value"}';
// Real implementation
const formatResponseOriginal = vitest_1.vi.fn().mockImplementation((jsonStr, format) => {
if (format === base_parser_1.TemplateFormat.json) {
return JSON.parse(jsonStr);
}
return jsonStr;
});
vitest_1.vi.spyOn(baseParserModule, 'formatResponse').mockImplementation(formatResponseOriginal);
const result = (0, base_parser_1.formatResponse)(response, base_parser_1.TemplateFormat.json);
(0, vitest_1.expect)(result).toEqual({ test: 'value' });
});
(0, vitest_1.test)('should convert JSON string to YAML when format is yaml', () => {
// Create a simple JSON string
const response = '{"test":"value"}';
// Mock YAML output
const yamlOutput = 'test: value';
vitest_1.vi.spyOn(baseParserModule, 'formatResponse').mockImplementation(() => yamlOutput);
const result = (0, base_parser_1.formatResponse)(response, base_parser_1.TemplateFormat.yaml);
(0, vitest_1.expect)(result).toBe(yamlOutput);
(0, vitest_1.expect)(result).toContain('test: value');
});
(0, vitest_1.test)('should return string as is for text format', () => {
const response = 'Plain text content';
// Mock implementation for text format
vitest_1.vi.spyOn(baseParserModule, 'formatResponse').mockImplementation((text) => text);
const result = (0, base_parser_1.formatResponse)(response, base_parser_1.TemplateFormat.text);
(0, vitest_1.expect)(result).toBe(response);
});
});
});