-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate-template-utils.js
More file actions
332 lines (277 loc) · 10.4 KB
/
validate-template-utils.js
File metadata and controls
332 lines (277 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env node
/**
* TemplateUtils Validation Script
*
* Validates that the refactored TemplateUtils maintains 100% backward compatibility
* with the original API and functionality.
*/
const fs = require('fs');
const path = require('path');
// Import the refactored TemplateUtils
const TemplateUtils = require('./scripts/lib/template-utils');
// Test data
const TEST_TEMPLATE = 'Hello {{name}}! Welcome to {{project}} version {{version}}.';
const TEST_VARIABLES = { name: 'World', project: 'TestProject', version: '1.0.0' };
const TEST_REQUIRED = ['name', 'project'];
// Validation results
const validationResults = {
total: 0,
passed: 0,
failed: 0,
errors: [],
};
function logResult(testName, passed, error = null) {
validationResults.total++;
if (passed) {
validationResults.passed++;
console.log(`✅ ${testName}`);
} else {
validationResults.failed++;
validationResults.errors.push({ test: testName, error });
console.log(`❌ ${testName}: ${error}`);
}
}
async function validateTemplateUtils() {
console.log('🔍 Validating TemplateUtils Refactoring\n');
console.log('='.repeat(60));
// Test 1: Can access TemplateUtils class
try {
if (typeof TemplateUtils !== 'function') {
throw new Error('TemplateUtils is not a class/function');
}
logResult('TemplateUtils class is accessible', true);
} catch (error) {
logResult('TemplateUtils class is accessible', false, error.message);
}
// Test 2: Check all static methods exist
const requiredMethods = [
'renderTemplate',
'renderTemplateFile',
'generateFile',
'generateFromTemplateDir',
'_processTemplateDir',
'getLanguageTemplates',
'generateLanguageProject',
'generateLanguageConfig',
'generateReadme',
'generateGitignore',
'validateVariables',
'getTemplateCore',
'getDirectoryProcessor',
'getLanguageTemplatesModule',
'getProjectGenerator',
'getTemplateValidator',
];
try {
for (const method of requiredMethods) {
if (typeof TemplateUtils[method] !== 'function') {
throw new Error(`Missing static method: ${method}`);
}
}
logResult('All required static methods exist', true);
} catch (error) {
logResult('All required static methods exist', false, error.message);
}
// Test 3: Render template
try {
const result = TemplateUtils.renderTemplate(TEST_TEMPLATE, TEST_VARIABLES);
if (typeof result !== 'string') {
throw new Error('renderTemplate did not return string');
}
if (!result.includes('World') || !result.includes('TestProject') || !result.includes('1.0.0')) {
throw new Error('Template variables not properly rendered');
}
logResult('renderTemplate works correctly', true);
} catch (error) {
logResult('renderTemplate works correctly', false, error.message);
}
// Test 4: Validate variables
try {
const validation = TemplateUtils.validateVariables(TEST_VARIABLES, TEST_REQUIRED);
if (!validation || typeof validation !== 'object') {
throw new Error('validateVariables did not return object');
}
if (!validation.hasOwnProperty('isValid')) {
throw new Error('Validation result missing isValid property');
}
if (!validation.isValid) {
throw new Error(`Validation failed: ${validation.errors.join(', ')}`);
}
logResult('validateVariables works correctly', true);
} catch (error) {
logResult('validateVariables works correctly', false, error.message);
}
// Test 5: Get language templates
try {
const goTemplates = TemplateUtils.getLanguageTemplates('go');
const pythonTemplates = TemplateUtils.getLanguageTemplates('python');
const invalidTemplates = TemplateUtils.getLanguageTemplates('invalid');
if (!goTemplates || typeof goTemplates !== 'object') {
throw new Error('getLanguageTemplates for go did not return object');
}
if (!pythonTemplates || typeof pythonTemplates !== 'object') {
throw new Error('getLanguageTemplates for python did not return object');
}
if (!invalidTemplates || typeof invalidTemplates !== 'object') {
throw new Error('getLanguageTemplates for invalid language did not return object');
}
if (Object.keys(invalidTemplates).length !== 0) {
throw new Error('Invalid language should return empty object');
}
// Check for expected template files
if (!goTemplates['main.go'] || !goTemplates['go.mod']) {
throw new Error('Go templates missing expected files');
}
if (!pythonTemplates['main.py'] || !pythonTemplates['requirements.txt']) {
throw new Error('Python templates missing expected files');
}
logResult('getLanguageTemplates works correctly', true);
} catch (error) {
logResult('getLanguageTemplates works correctly', false, error.message);
}
// Test 6: Check module getters
try {
const templateCore = TemplateUtils.getTemplateCore();
const directoryProcessor = TemplateUtils.getDirectoryProcessor();
const languageTemplates = TemplateUtils.getLanguageTemplatesModule();
const projectGenerator = TemplateUtils.getProjectGenerator();
const templateValidator = TemplateUtils.getTemplateValidator();
if (
!templateCore ||
!directoryProcessor ||
!languageTemplates ||
!projectGenerator ||
!templateValidator
) {
throw new Error('Module getters returned null/undefined');
}
// Check that modules have expected methods
if (typeof templateCore.renderTemplate !== 'function') {
throw new Error('TemplateCore missing renderTemplate method');
}
if (typeof directoryProcessor.generateFromTemplateDir !== 'function') {
throw new Error('DirectoryProcessor missing generateFromTemplateDir method');
}
if (typeof languageTemplates.getLanguageTemplates !== 'function') {
throw new Error('LanguageTemplates missing getLanguageTemplates method');
}
if (typeof projectGenerator.generateReadme !== 'function') {
throw new Error('ProjectGenerator missing generateReadme method');
}
if (typeof templateValidator.validateVariables !== 'function') {
throw new Error('TemplateValidator missing validateVariables method');
}
logResult('Module getters work correctly', true);
} catch (error) {
logResult('Module getters work correctly', false, error.message);
}
// Test 7: Create test directory for file operations
const testDir = path.join(__dirname, 'test-template-output');
const testTemplateFile = path.join(testDir, 'test.template');
const testOutputFile = path.join(testDir, 'test.txt');
try {
// Clean up and create test directory
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true });
}
fs.mkdirSync(testDir, { recursive: true });
// Create test template file
fs.writeFileSync(testTemplateFile, TEST_TEMPLATE, 'utf8');
logResult('Test directory and files created', true);
} catch (error) {
logResult('Test directory and files created', false, error.message);
}
// Test 8: Render template file
try {
const result = TemplateUtils.renderTemplateFile(testTemplateFile, TEST_VARIABLES);
if (typeof result !== 'string') {
throw new Error('renderTemplateFile did not return string');
}
if (!result.includes('World') || !result.includes('TestProject')) {
throw new Error('Template file variables not properly rendered');
}
logResult('renderTemplateFile works correctly', true);
} catch (error) {
logResult('renderTemplateFile works correctly', false, error.message);
}
// Test 9: Generate file from template
try {
const result = TemplateUtils.generateFile(testTemplateFile, testOutputFile, TEST_VARIABLES, {
overwrite: true,
backup: false,
createDir: true,
});
if (!result || typeof result !== 'object') {
throw new Error('generateFile did not return object');
}
if (!result.success) {
throw new Error('generateFile was not successful');
}
// Verify file was created
if (!fs.existsSync(testOutputFile)) {
throw new Error('Output file was not created');
}
const fileContent = fs.readFileSync(testOutputFile, 'utf8');
if (!fileContent.includes('World') || !fileContent.includes('TestProject')) {
throw new Error('Generated file content incorrect');
}
logResult('generateFile works correctly', true);
} catch (error) {
logResult('generateFile works correctly', false, error.message);
}
// Test 10: Check module structure
try {
// Verify modules exist
const modules = [
'./scripts/lib/template-modules/template-core',
'./scripts/lib/template-modules/directory-processor',
'./scripts/lib/template-modules/language-templates',
'./scripts/lib/template-modules/project-generator',
'./scripts/lib/template-modules/template-validator',
];
for (const modulePath of modules) {
const fullPath = path.join(__dirname, modulePath);
if (!fs.existsSync(fullPath + '.js')) {
throw new Error(`Module not found: ${modulePath}`);
}
}
logResult('All modules exist and are properly structured', true);
} catch (error) {
logResult('All modules exist and are properly structured', false, error.message);
}
// Clean up test directory
try {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true });
}
} catch (error) {
console.log(`⚠️ Warning: Could not clean up test directory: ${error.message}`);
}
// Summary
console.log('\n' + '='.repeat(60));
console.log('📊 VALIDATION SUMMARY');
console.log('='.repeat(60));
console.log(`Total Tests: ${validationResults.total}`);
console.log(`✅ Passed: ${validationResults.passed}`);
console.log(`❌ Failed: ${validationResults.failed}`);
if (validationResults.failed > 0) {
console.log('\n❌ FAILED TESTS:');
validationResults.errors.forEach((error, index) => {
console.log(` ${index + 1}. ${error.test}`);
console.log(` Error: ${error.error}`);
});
process.exit(1);
} else {
console.log('\n🎉 All tests passed! TemplateUtils refactoring is validated.');
console.log('✅ 100% backward compatibility maintained');
console.log('✅ All modules properly structured');
console.log('✅ All methods available and functional');
console.log('✅ File operations work correctly');
process.exit(0);
}
}
// Run validation
validateTemplateUtils().catch((error) => {
console.error('❌ Validation script error:', error);
process.exit(1);
});