UNPKG

testtrack-cli

Version:

Submit your automated test runs to your Test Track account project

181 lines 9.37 kB
#!/usr/bin/env node "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 () { 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; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const FileNotFoundException_1 = require("./exceptions/FileNotFoundException"); const ResultFileEmptyException_1 = require("./exceptions/ResultFileEmptyException"); const axios_1 = __importDefault(require("axios")); const InvalidDataException_1 = require("./exceptions/InvalidDataException"); const path = __importStar(require("node:path")); const InvalidCommandException_1 = require("./exceptions/InvalidCommandException"); const yargs = require("yargs"); const fileToBase64 = (filePath) => { const fileBuffer = fs.readFileSync(filePath); const base64Data = fileBuffer.toString('base64'); return base64Data; }; const sendRequest = (data) => __awaiter(void 0, void 0, void 0, function* () { //const url = 'https://app.test-track.com/backend/api/automated-test-run'; const url = 'https://staging-app.test-track.com/api/automated-test-run'; const headers = { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', 'Accept': 'application/json' }; try { return yield axios_1.default.post(url, data, { headers: headers }); } catch (err) { throw err; } }); const options = yargs .usage("Usage: --api-key <API Key> --project-id <Project ID>") .option("version", { alias: "v", description: "Show version number", type: "boolean" }) .option("key", { alias: "api-key", describe: "Company API Key", type: "string", demandOption: true }) .option("id", { alias: "project-id", describe: "Project id you are submitting the automated test run against", type: "string", demandOption: true }) .option("file", { alias: "file", description: "JUnit XML File path", demandOption: false }) .option("directory", { alias: "directory", description: "Path to where are all JUnit XML files are stored. Any non XML files will be ignored", type: "string", demandOption: false }) .option("branch", { alias: "branch", description: "The branch name this test run is for, if not provided it will default to main", type: "string" }) .option("tags", { alias: "tags", description: "Tags to be added to the test run, comma separated no spaces", type: "string" }) .argv; const apiKey = options['api-key']; const projectId = options['project-id']; const file = options.file; const directory = options.directory; try { if (apiKey.length === 0) { throw new InvalidDataException_1.InvalidDataException("api-key"); } else if (projectId.length === 0) { throw new InvalidDataException_1.InvalidDataException("project-id"); } if (typeof file !== typeof undefined && typeof directory !== typeof undefined) { throw new InvalidCommandException_1.InvalidCommandException("You cannot use both --file and --directory at the same time"); } const data = { project_id: projectId }; if ((file === null || file === void 0 ? void 0 : file.length) > 0) { if (!fs.existsSync(file)) { throw new FileNotFoundException_1.FileNotFoundException(file); } data.file_name = path.basename(file); data.file_content = fileToBase64(file); } else if ((directory === null || directory === void 0 ? void 0 : directory.length) > 0) { if (!fs.existsSync(directory)) { throw new FileNotFoundException_1.FileNotFoundException(file); } const files = fs.readdirSync(directory); const processFiles = []; files.forEach(file => { if (path.extname(file) === '.xml') { processFiles.push({ file_name: path.basename(`${directory}/${file}`), file_content: fileToBase64(`${directory}/${file}`) }); } }); // @ts-ignore data.files = processFiles; if (data.files.length === 0) { throw new ResultFileEmptyException_1.ResultFileEmptyException(directory); } } else { throw new InvalidCommandException_1.InvalidCommandException("At least --file or --directory must be specified"); } data.branch = ((_a = options === null || options === void 0 ? void 0 : options.branch) === null || _a === void 0 ? void 0 : _a.length) > 0 ? options === null || options === void 0 ? void 0 : options.branch : "main"; data.tags = ((_b = options === null || options === void 0 ? void 0 : options.tags) === null || _b === void 0 ? void 0 : _b.length) > 0 ? options === null || options === void 0 ? void 0 : options.tags : ""; sendRequest(data).then(response => { //We should get no other response here other than 200 OK. Any 4xx or 5xx errors //are caught by the catch handler if (response.status === 200) { console.info("Successfully submitted automated test run"); console.info("Your test run is added to a queue and you should see the results shortly"); console.info("An email notification will be sent once your test run has been processed"); } else { console.log(`Received unexpected response code: ${response.status}: ${response.statusText}`); console.log("If you continue to see this, please raise a support ticket at https://support.devso.io and include the information below"); if (typeof (response === null || response === void 0 ? void 0 : response.status) !== typeof undefined && typeof (response === null || response === void 0 ? void 0 : response.statusText) !== typeof undefined) { console.log(`Status Code: ${response.status} - ${response.statusText}`); } if (typeof (response === null || response === void 0 ? void 0 : response.data) !== typeof undefined) { console.log(response.data); } } }).catch(err => { console.error("An unexpected error occurred submitting the automated test run to Test Track"); console.error("If you continue to see this problem, please raise a support ticket at https://support.devso.io with the details outputted below"); console.error("Status: " + err.response.status + ": " + err.response.statusText); console.error("Err", err.response.data); }); } catch (err) { if (err instanceof FileNotFoundException_1.FileNotFoundException) { console.error(err.message); } else if (err instanceof ResultFileEmptyException_1.ResultFileEmptyException) { console.error(err.message); } else if (err instanceof InvalidDataException_1.InvalidDataException) { console.error(err.message); } else if (err instanceof InvalidCommandException_1.InvalidCommandException) { console.error(err.message); } else { console.error("An unexpected error has occurred submitted your automated test run to Test Track"); console.error("If you continue to see this error, please raise a support ticket at https://support.devso.io with the output below"); console.error("An exception has occurred", err); } } //# sourceMappingURL=index.js.map