testeranto
Version:
the AI powered BDD test framework for typescript projects
158 lines (157 loc) • 7.17 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateGolingvuMetafile = generateGolingvuMetafile;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const helpers_1 = require("./golingvuMetafile/helpers");
const fileDiscovery_1 = require("./golingvuMetafile/fileDiscovery");
const importParser_1 = require("./golingvuMetafile/importParser");
let generationQueue = null;
async function generateGolingvuMetafile(testName, entryPoints) {
// If there's already a generation in progress, wait for it to complete
if (generationQueue) {
return generationQueue;
}
generationQueue = (async () => {
const inputs = {};
const outputs = {};
const signature = Date.now().toString(36);
// If entry points are provided, use them directly
// For test generation, we want to include test files
const filteredEntryPoints = [];
for (const entryPoint of entryPoints) {
// Skip if it's not a .go file
if (!entryPoint.endsWith(".go")) {
console.warn(`Skipping non-Go file: ${entryPoint}`);
continue;
}
// Check if the file exists and is a file
let resolvedPath = entryPoint;
if (!path_1.default.isAbsolute(entryPoint)) {
resolvedPath = path_1.default.join(process.cwd(), entryPoint);
}
if (!fs_1.default.existsSync(resolvedPath)) {
console.warn(`Entry point does not exist: ${resolvedPath}`);
continue;
}
if (!fs_1.default.statSync(resolvedPath).isFile()) {
console.warn(`Entry point is not a file: ${resolvedPath}`);
continue;
}
// Add to filtered entry points - don't skip test files for test generation
filteredEntryPoints.push(resolvedPath);
}
entryPoints = filteredEntryPoints;
// If no valid entry points remain, try to find Go files automatically
// For test generation, include all files including test files
// if (entryPoints.length === 0) {
// const allGoFiles = findGoFilesInProject();
// // Don't filter out test files
// entryPoints = allGoFiles.filter((file) => {
// const fileName = path.basename(file);
// // Only exclude our generated files
// return (
// !fileName.endsWith(".golingvu.test.go") &&
// !fileName.endsWith(".golingvu.go")
// );
// });
// if (entryPoints.length === 0) {
// console.warn("No Go files found in the project");
// } else {
// console.log(`Found ${entryPoints.length} Go files:`, entryPoints);
// }
// }
// Process all valid entry points to collect their dependencies
const allDependencies = new Set();
const validEntryPoints = [];
for (let i = 0; i < entryPoints.length; i++) {
let entryPoint = entryPoints[i];
// Resolve and validate the entry point path
let resolvedPath = entryPoint;
if (!path_1.default.isAbsolute(entryPoint)) {
resolvedPath = path_1.default.join(process.cwd(), entryPoint);
}
if (!fs_1.default.existsSync(resolvedPath)) {
console.warn(`Entry point ${entryPoint} does not exist at ${resolvedPath}`);
continue;
}
// Update the entry point to use the resolved path
entryPoints[i] = resolvedPath;
entryPoint = resolvedPath;
// Don't skip test files for test generation
validEntryPoints.push(entryPoint);
// Collect dependencies for this entry point
const entryPointDependencies = (0, fileDiscovery_1.collectGoDependencies)(entryPoint);
entryPointDependencies.forEach((dep) => allDependencies.add(dep));
}
// Process all dependencies to add to inputs
for (const dep of allDependencies) {
if (!inputs[dep]) {
const bytes = fs_1.default.statSync(dep).size;
const imports = (0, importParser_1.parseGoImports)(dep);
// Check if this is a test file
const isTestFile = path_1.default.basename(dep).includes("_test.go") ||
path_1.default.basename(dep).includes(".golingvu.test.go");
inputs[dep] = Object.assign({ bytes,
imports, format: "esm" }, (isTestFile ? { testeranto: { isTest: true } } : {}));
}
}
// Generate the output path based on the project structure
// Always use the project root as the base
const projectRoot = (0, helpers_1.findProjectRoot)();
let outputKey = "";
if (validEntryPoints.length > 0) {
const firstEntryPoint = validEntryPoints[0];
// Get the relative path from project root to the entry point
const relativePath = path_1.default.relative(projectRoot, path_1.default.dirname(firstEntryPoint));
// Get the base name without extension
const baseName = path_1.default.basename(firstEntryPoint, ".go");
// Construct the output key
// Ensure relativePath is not empty
const outputPath = relativePath === "" ? baseName : path_1.default.join(relativePath, baseName);
outputKey = `golang/${path_1.default.basename(projectRoot)}/${outputPath}.golingvu.go`;
}
else {
// Fallback if no valid entry points
outputKey = `golang/core/main.golingvu.go`;
}
// Calculate total bytes from all inputs
const inputBytes = {};
let totalBytes = 0;
for (const inputPath in inputs) {
const bytes = inputs[inputPath].bytes;
inputBytes[inputPath] = { bytesInOutput: bytes };
totalBytes += bytes;
}
// Store the first valid entry point for use in the wrapper generation
const firstEntryPoint = validEntryPoints.length > 0 ? validEntryPoints[0] : "";
outputs[outputKey] = {
imports: [],
exports: [],
entryPoint: firstEntryPoint,
inputs: inputBytes,
bytes: totalBytes,
signature,
};
// If no valid entry points were found, log a warning
if (validEntryPoints.length === 0) {
console.warn("No valid Go files found to process");
}
const result = {
errors: [],
warnings: [],
metafile: {
inputs,
outputs,
},
};
generationQueue = null;
return result;
})();
return generationQueue;
}