spanwright
Version:
CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation
204 lines • 10.9 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 cli_1 = require("../cli");
const constants_1 = require("../constants");
// Mock dependencies
vitest_1.vi.mock('fs');
vitest_1.vi.mock('path');
vitest_1.vi.mock('../errors');
vitest_1.vi.mock('../validation');
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const errors_1 = require("../errors");
const validation_1 = require("../validation");
const mockFs = fs;
const mockPath = path;
const mockSafeExit = errors_1.safeExit;
const mockIsFlag = validation_1.isFlag;
(0, vitest_1.describe)('CLI Module', () => {
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
console.log = vitest_1.vi.fn();
console.error = vitest_1.vi.fn();
// Default mocks
mockPath.join.mockImplementation((...args) => args.join('/'));
mockSafeExit.mockImplementation(() => {
throw new Error('safeExit called');
});
mockIsFlag.mockImplementation((arg) => arg.startsWith('-'));
});
(0, vitest_1.afterEach)(() => {
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.describe)('parseCommandLineArgs', () => {
const originalArgv = process.argv;
(0, vitest_1.afterEach)(() => {
process.argv = originalArgv;
});
(0, vitest_1.it)('should parse project name and flags correctly', () => {
process.argv = ['node', 'spanwright', 'my-project', '--help', '-v'];
mockIsFlag.mockImplementation((arg) => arg.startsWith('-'));
const result = (0, cli_1.parseCommandLineArgs)();
(0, vitest_1.expect)(result.projectName).toBe('my-project');
(0, vitest_1.expect)(result.flags).toEqual(['--help', '-v']);
});
(0, vitest_1.it)('should handle no project name', () => {
process.argv = ['node', 'spanwright', '--help'];
mockIsFlag.mockImplementation((arg) => arg.startsWith('-'));
const result = (0, cli_1.parseCommandLineArgs)();
(0, vitest_1.expect)(result.projectName).toBeUndefined();
(0, vitest_1.expect)(result.flags).toEqual(['--help']);
});
(0, vitest_1.it)('should handle no flags', () => {
process.argv = ['node', 'spanwright', 'my-project'];
mockIsFlag.mockImplementation((arg) => arg.startsWith('-'));
const result = (0, cli_1.parseCommandLineArgs)();
(0, vitest_1.expect)(result.projectName).toBe('my-project');
(0, vitest_1.expect)(result.flags).toEqual([]);
});
(0, vitest_1.it)('should handle multiple arguments and find first non-flag as project name', () => {
process.argv = ['node', 'spanwright', '--help', 'project-name', 'extra-arg'];
mockIsFlag.mockImplementation((arg) => arg.startsWith('-'));
const result = (0, cli_1.parseCommandLineArgs)();
(0, vitest_1.expect)(result.projectName).toBe('project-name');
(0, vitest_1.expect)(result.flags).toEqual(['--help']);
});
});
(0, vitest_1.describe)('checkForHelpAndVersion', () => {
(0, vitest_1.it)('should call showVersion when version flag is present', () => {
(0, vitest_1.expect)(() => (0, cli_1.checkForHelpAndVersion)(['--version'])).toThrow('safeExit called');
});
(0, vitest_1.it)('should call showHelp when help flag is present', () => {
(0, vitest_1.expect)(() => (0, cli_1.checkForHelpAndVersion)(['--help'])).toThrow('safeExit called');
});
(0, vitest_1.it)('should handle short version flag', () => {
(0, vitest_1.expect)(() => (0, cli_1.checkForHelpAndVersion)(['-v'])).toThrow('safeExit called');
});
(0, vitest_1.it)('should handle short help flag', () => {
(0, vitest_1.expect)(() => (0, cli_1.checkForHelpAndVersion)(['-h'])).toThrow('safeExit called');
});
(0, vitest_1.it)('should not call any function when no relevant flags are present', () => {
(0, vitest_1.expect)(() => (0, cli_1.checkForHelpAndVersion)(['--other-flag'])).not.toThrow();
});
});
(0, vitest_1.describe)('showVersion', () => {
(0, vitest_1.it)('should display version from package.json', () => {
const mockPackageJson = JSON.stringify({ version: '1.0.0' });
mockFs.readFileSync.mockReturnValue(mockPackageJson);
(0, vitest_1.expect)(() => (0, cli_1.showVersion)()).toThrow('safeExit called');
(0, vitest_1.expect)(mockPath.join).toHaveBeenCalledWith(vitest_1.expect.any(String), '..', 'package.json');
(0, vitest_1.expect)(mockFs.readFileSync).toHaveBeenCalledWith(vitest_1.expect.any(String), 'utf8');
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith('1.0.0');
(0, vitest_1.expect)(mockSafeExit).toHaveBeenCalledWith(0);
});
(0, vitest_1.it)('should handle error when reading package.json', () => {
mockFs.readFileSync.mockImplementation(() => {
throw new Error('File not found');
});
(0, vitest_1.expect)(() => (0, cli_1.showVersion)()).toThrow('safeExit called');
(0, vitest_1.expect)(console.error).toHaveBeenCalledWith('Error reading version from package.json');
(0, vitest_1.expect)(mockSafeExit).toHaveBeenCalledWith(0);
});
(0, vitest_1.it)('should handle invalid JSON in package.json', () => {
mockFs.readFileSync.mockReturnValue('invalid json');
(0, vitest_1.expect)(() => (0, cli_1.showVersion)()).toThrow('safeExit called');
(0, vitest_1.expect)(console.error).toHaveBeenCalledWith('Error reading version from package.json');
(0, vitest_1.expect)(mockSafeExit).toHaveBeenCalledWith(0);
});
});
(0, vitest_1.describe)('showHelp', () => {
(0, vitest_1.it)('should display help text and exit', () => {
(0, vitest_1.expect)(() => (0, cli_1.showHelp)()).toThrow('safeExit called');
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Usage: spanwright'));
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Options:'));
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Arguments:'));
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Examples:'));
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(vitest_1.expect.stringContaining('Non-interactive mode'));
(0, vitest_1.expect)(mockSafeExit).toHaveBeenCalledWith(0);
});
});
(0, vitest_1.describe)('showUsageError', () => {
(0, vitest_1.it)('should display usage error and exit with code 1', () => {
(0, vitest_1.expect)(() => (0, cli_1.showUsageError)()).toThrow('safeExit called');
(0, vitest_1.expect)(console.error).toHaveBeenCalledWith(constants_1.MESSAGES.ERRORS.NO_PROJECT_NAME);
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(constants_1.MESSAGES.USAGE.BASIC);
(0, vitest_1.expect)(console.log).toHaveBeenCalledWith(constants_1.MESSAGES.USAGE.HELP_SUGGESTION);
(0, vitest_1.expect)(mockSafeExit).toHaveBeenCalledWith(1);
});
});
(0, vitest_1.describe)('isNonInteractiveMode', () => {
const originalEnv = process.env;
(0, vitest_1.beforeEach)(() => {
process.env = { ...originalEnv };
});
(0, vitest_1.afterEach)(() => {
process.env = originalEnv;
});
(0, vitest_1.it)('should return true when CI environment variable is set', () => {
process.env.CI = 'true';
const result = (0, cli_1.isNonInteractiveMode)([]);
(0, vitest_1.expect)(result).toBe(true);
});
(0, vitest_1.it)('should return true when SPANWRIGHT_NON_INTERACTIVE is set', () => {
process.env.SPANWRIGHT_NON_INTERACTIVE = 'true';
const result = (0, cli_1.isNonInteractiveMode)([]);
(0, vitest_1.expect)(result).toBe(true);
});
(0, vitest_1.it)('should return true when non-interactive flag is present', () => {
const result = (0, cli_1.isNonInteractiveMode)([constants_1.CLI_FLAGS.NON_INTERACTIVE]);
(0, vitest_1.expect)(result).toBe(true);
});
(0, vitest_1.it)('should return false when no non-interactive indicators are present', () => {
process.env.CI = 'false';
process.env.SPANWRIGHT_NON_INTERACTIVE = 'false';
const result = (0, cli_1.isNonInteractiveMode)([]);
(0, vitest_1.expect)(result).toBe(false);
});
(0, vitest_1.it)('should return false when environment variables are not set to true', () => {
process.env.CI = 'false';
process.env.SPANWRIGHT_NON_INTERACTIVE = 'false';
const result = (0, cli_1.isNonInteractiveMode)(['--other-flag']);
(0, vitest_1.expect)(result).toBe(false);
});
(0, vitest_1.it)('should prioritize environment variables over flags', () => {
process.env.CI = 'true';
const result = (0, cli_1.isNonInteractiveMode)([]);
(0, vitest_1.expect)(result).toBe(true);
});
});
});
//# sourceMappingURL=cli.test.js.map