UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

154 lines (152 loc) • 5.73 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const gluegun_1 = require("gluegun"); const js_yaml_1 = __importDefault(require("js-yaml")); // This'd import the N-API bindings based on the OS and architecture const core_1 = require("@oclif/core"); class TestCommand extends core_1.Command { async run() { const { args: { datasource }, flags: { coverage, force, logs, recompile, version }, } = await this.parse(TestCommand); // Ensure PostgreSQL 16 is installed try { const versionOutput = (0, child_process_1.execSync)('psql --version').toString(); const versionMatch = versionOutput.match(/\b(14|15|16)\.\d+/); if (!versionMatch) { this.error(`PostgreSQL version >=14 is required. Detected version: ${versionOutput}`, { exit: 1, }); } } catch (error) { this.error(`PostgreSQL version >=14 is required. It seems PostgreSQL is not installed.\n To install PostgreSQL 16:\n On Linux (Ubuntu/Debian based): sudo apt update sudo apt install -y postgresql-16 On macOS: brew install postgresql@16`, { exit: 1, }); } let testsDir = './tests'; // Check if matchstick.yaml config exists if (gluegun_1.filesystem.exists('matchstick.yaml')) { try { // Load the config const config = await js_yaml_1.default.load(gluegun_1.filesystem.read('matchstick.yaml', 'utf8')); // Check if matchstick.yaml and testsFolder not null if (config?.testsFolder) { // assign test folder from matchstick.yaml if present testsDir = config.testsFolder; } } catch (error) { this.error(`A problem occurred while reading "matchstick.yaml":\n${error.message}`, { exit: 1, }); } } const cachePath = path_1.default.resolve(testsDir, '.latest.json'); const opts = { testsDir, cachePath, coverage, force, logs, recompile, version, latestVersion: getLatestVersionFromCache(cachePath), }; runNapi.bind(this)(datasource, opts); } } TestCommand.description = 'Runs rust binary for subgraph testing.'; TestCommand.args = { datasource: core_1.Args.string(), }; TestCommand.flags = { help: core_1.Flags.help({ char: 'h', }), coverage: core_1.Flags.boolean({ summary: 'Run the tests in coverage mode.', char: 'c', }), force: core_1.Flags.boolean({ summary: 'Binary - overwrites folder + file when downloading.', char: 'f', }), logs: core_1.Flags.boolean({ summary: 'Logs to the console information about the OS, CPU model and download url (debugging purposes).', char: 'l', }), recompile: core_1.Flags.boolean({ summary: 'Force-recompile tests.', char: 'r', }), version: core_1.Flags.string({ summary: 'Choose the version of the rust binary that you want to be downloaded/used.', char: 'v', }), }; exports.default = TestCommand; function getLatestVersionFromCache(cachePath) { if (gluegun_1.filesystem.exists(cachePath) == 'file') { const cached = gluegun_1.filesystem.read(cachePath, 'json'); // Get the cache age in days const cacheAge = (Date.now() - cached.timestamp) / (1000 * 60 * 60 * 24); // If cache age is less than 1 day, use the cached version if (cacheAge < 1) { return cached.version; } } return null; } async function runNapi(datasource, opts) { const args = []; if (opts.coverage) args.push('--coverage'); if (opts.recompile) args.push('--recompile'); if (datasource) args.push(datasource); try { // Dynamically import runTests only after PostgreSQL check const { runTests } = await Promise.resolve().then(() => __importStar(require('@graphprotocol/graph-tooling-napi-utils/mod/testing'))); // Call the N-API function runTests(args); } catch (error) { this.error(`Error running tests:\n${error.message}`, { exit: 1, }); } }