@sasjs/cli
Version:
Command line interface for SASjs
399 lines (398 loc) • 21.4 kB
JavaScript
"use strict";
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var dotenv_1 = __importDefault(require("dotenv"));
var path_1 = __importDefault(require("path"));
var __1 = require("../..");
var utils_1 = require("@sasjs/utils");
var test_1 = require("../../../utils/test");
var build_1 = require("../../build/build");
var utils_2 = require("../../../utils");
describe('sasjs run with Viya', function () {
var target;
describe('runSasCode within project', function () {
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
var appName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
appName = 'cli-tests-run-' + (0, utils_1.generateTimestamp)();
return [4 /*yield*/, (0, test_1.createTestApp)(__dirname, appName)];
case 1:
_a.sent();
target = (0, test_1.generateTestTarget)(appName, "/Public/app/cli-tests/".concat(appName), {
serviceFolders: [path_1.default.join('sasjs', 'testServices')],
initProgram: '',
termProgram: '',
macroVars: {}
}, utils_1.ServerType.SasViya);
return [4 /*yield*/, (0, utils_1.copy)(path_1.default.join(__dirname, 'testServices'), path_1.default.join(process.projectDir, 'sasjs', 'testServices'))];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, test_1.removeTestServerFolder)(target.appLoc, target)];
case 1:
_a.sent();
return [4 /*yield*/, (0, test_1.removeTestApp)(__dirname, target.name)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should throw an error if file type is not *.sas', function () { return __awaiter(void 0, void 0, void 0, function () {
var file, error;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
file = 'test.sas.txt';
error = new Error("'sasjs run' command supports only *.sas files.");
return [4 /*yield*/, expect((0, __1.runSasCode)(target, file)).rejects.toEqual(error)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should throw an error if url does not point to a webpage', function () { return __awaiter(void 0, void 0, void 0, function () {
var url;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = 'https://raw.githubusercontent.com/sasjs/cli/issues/808';
return [4 /*yield*/, expect((0, __1.runSasCode)(target, url)).rejects.toThrow()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should throw an error if url response starts with angular bracket(<)', function () { return __awaiter(void 0, void 0, void 0, function () {
var url, invalidSasError, error;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = 'https://github.com/sasjs/cli/issues/808';
invalidSasError = process.sasjsConstants.invalidSasError;
error = new Error("".concat(invalidSasError, "\nUrl: ").concat(url));
return [4 /*yield*/, expect((0, __1.runSasCode)(target, url)).rejects.toThrowError(error)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should throw an error when url response is not a string', function () { return __awaiter(void 0, void 0, void 0, function () {
var url, invalidSasError, error;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = 'https://api.agify.io/?name=sabir';
invalidSasError = process.sasjsConstants.invalidSasError;
error = new Error("".concat(invalidSasError, "\nUrl: ").concat(url));
return [4 /*yield*/, expect((0, __1.runSasCode)(target, url)).rejects.toThrowError(error)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having relative path', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPart, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPart = "1 data;\n2 do x=1 to 100;\n3 output;\n4 end;\n5 run;";
return [4 /*yield*/, (0, __1.runSasCode)(target, 'sasjs/testServices/logJob.sas')];
case 1:
result = _a.sent();
expect(result.log.includes(logPart)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having absolute path', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPart, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPart = "1 data;\n2 do x=1 to 100;\n3 output;\n4 end;\n5 run;";
return [4 /*yield*/, (0, __1.runSasCode)(target, "".concat(process.projectDir, "/sasjs/testServices/logJob.sas"))];
case 1:
result = _a.sent();
expect(result.log.includes(logPart)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having relative path but compile it first', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPartRegex, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPartRegex = /[0-9]* data;\n[0-9]* do x=1 to 100;\n[0-9]* output;\n[0-9]* end;\n[0-9]* run;\n/;
return [4 /*yield*/, (0, __1.runSasCode)(target, "sasjs/testServices/logJob.sas", true)];
case 1:
result = _a.sent();
expect(logPartRegex.test(result.log)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having absolute path but compile it first', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPartRegex, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPartRegex = /[0-9]* data;\n[0-9]* do x=1 to 100;\n[0-9]* output;\n[0-9]* end;\n[0-9]* run;\n/;
return [4 /*yield*/, (0, __1.runSasCode)(target, "".concat(process.projectDir, "/sasjs/testServices/logJob.sas"), true)];
case 1:
result = _a.sent();
expect(logPartRegex.test(result.log)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
});
describe('runSasCode outside project', function () {
var sharedAppName;
var homedir = require('os').homedir();
beforeAll(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
sharedAppName = "cli-tests-run-".concat((0, utils_1.generateTimestamp)());
return [4 /*yield*/, (0, test_1.createTestApp)(homedir, sharedAppName)];
case 1:
_a.sent();
return [4 /*yield*/, (0, test_1.updateConfig)({
macroFolders: [
"./".concat(sharedAppName, "/sasjs/macros"),
"./".concat(sharedAppName, "/sasjs/targets/viya/macros")
]
}, false)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
var appName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
appName = "cli-tests-run-".concat((0, utils_1.generateTimestamp)());
target = (0, test_1.generateTestTarget)(appName, "/Public/app/cli-tests/".concat(appName), {
serviceFolders: [path_1.default.join(__dirname, 'testServices')],
initProgram: '',
termProgram: '',
macroVars: {}
}, utils_1.ServerType.SasViya);
process.projectDir = '';
return [4 /*yield*/, (0, utils_2.setConstants)()];
case 1:
_a.sent();
process.currentDir = path_1.default.join(__dirname, appName);
return [4 /*yield*/, (0, utils_1.createFolder)(process.currentDir)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, utils_1.deleteFolder)(process.currentDir)];
case 1:
_a.sent();
return [4 /*yield*/, (0, test_1.removeTestServerFolder)(target.appLoc, target)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
afterAll(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, test_1.removeTestApp)(homedir, sharedAppName)];
case 1:
_a.sent();
return [4 /*yield*/, (0, utils_1.deleteFolder)(path_1.default.join(homedir, 'sasjsbuild'))];
case 2:
_a.sent();
return [4 /*yield*/, (0, utils_1.deleteFolder)(path_1.default.join(homedir, 'sasjsresults'))];
case 3:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having relative path', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPart, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPart = "1 data;\n2 do x=1 to 100;\n3 output;\n4 end;\n5 run;";
return [4 /*yield*/, (0, __1.runSasCode)(target, '../testServices/logJob.sas')];
case 1:
result = _a.sent();
expect(result.log.includes(logPart)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having absolute path', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPart, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPart = "1 data;\n2 do x=1 to 100;\n3 output;\n4 end;\n5 run;";
return [4 /*yield*/, (0, __1.runSasCode)(target, "".concat(__dirname, "/testServices/logJob.sas"))];
case 1:
result = _a.sent();
expect(result.log.includes(logPart)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having relative path but compile it first', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPartRegex, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPartRegex = /[0-9]* data;\n[0-9]* do x=1 to 100;\n[0-9]* output;\n[0-9]* end;\n[0-9]* run;\n/;
return [4 /*yield*/, (0, __1.runSasCode)(target, '../testServices/logJob.sas', true)];
case 1:
result = _a.sent();
expect(logPartRegex.test(result.log)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should get the log on successfull execution having absolute path but compile it first', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPartRegex, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPartRegex = /[0-9]* data;\n[0-9]* do x=1 to 100;\n[0-9]* output;\n[0-9]* end;\n[0-9]* run;\n/;
return [4 /*yield*/, (0, __1.runSasCode)(target, "".concat(__dirname, "/testServices/logJob.sas"), true)];
case 1:
result = _a.sent();
expect(logPartRegex.test(result.log)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
});
describe('runSasCode within vanilla js project', function () {
var appName;
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
dotenv_1.default.config();
appName = 'cli-tests-run-' + (0, utils_1.generateTimestamp)();
return [4 /*yield*/, (0, test_1.createTestMinimalApp)(__dirname, appName)];
case 1:
_a.sent();
return [4 /*yield*/, (0, test_1.updateTarget)({
appLoc: "/Public/app/cli-tests/".concat(appName),
streamConfig: {
assetPaths: [],
streamWeb: true,
streamWebFolder: 'webv',
webSourcePath: 'src',
streamServiceName: 'clickme'
},
authConfig: {
client: process.env.CLIENT,
secret: process.env.SECRET,
access_token: process.env.ACCESS_TOKEN,
refresh_token: process.env.REFRESH_TOKEN
}
}, 'viya')];
case 2:
target = _a.sent();
return [2 /*return*/];
}
});
}); });
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, test_1.removeTestServerFolder)(target.appLoc, target)];
case 1:
_a.sent();
return [4 /*yield*/, (0, test_1.removeTestApp)(__dirname, appName)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should get the log having launch code message', function () { return __awaiter(void 0, void 0, void 0, function () {
var logPart, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logPart = "SASjs Streaming App Created! Check it out here:\n \n \n \n \n your-sas-server.com/SASJobExecution?_FILE=".concat(target.appLoc, "/services/clickme.html\n");
return [4 /*yield*/, (0, build_1.build)(target)];
case 1:
_a.sent();
return [4 /*yield*/, (0, __1.runSasCode)(target, 'sasjsbuild/myviyadeploy.sas')];
case 2:
result = _a.sent();
expect(result.log.includes(logPart)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
});
});