@sasjs/cli
Version:
Command line interface for SASjs
388 lines (387 loc) • 19.9 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 utils_1 = require("../utils");
var test_1 = require("../test");
var utils_2 = require("@sasjs/utils");
var node_1 = __importDefault(require("@sasjs/adapter/node"));
var path_1 = __importDefault(require("path"));
var __1 = require("..");
describe('utils', function () {
var folderPath = path_1.default.join('src', 'utils', 'spec');
describe('parseLogLines', function () {
test('should generate plain text log from json', function () {
var expectedLog = "line1\nline2\nline3\nline4\n";
var json = {
items: [
{
line: 'line1'
},
{
line: 'line2'
},
{
line: 'line3'
},
{
line: 'line4'
}
]
};
expect((0, utils_1.parseLogLines)(json)).toEqual(expectedLog);
});
});
describe('millisecondsToDdHhMmSs', function () {
it('should throw an error if not supported type was provided', function () {
expect(function () { return (0, utils_1.millisecondsToDdHhMmSs)(undefined); }).toThrow('Not supported attribute type.');
});
it('should process negative number', function () {
expect((0, utils_1.millisecondsToDdHhMmSs)(-1000)).toEqual('0 day(s); 0 hour(s); 0 minute(s); 1 second(s)');
});
it('should process 0', function () {
expect((0, utils_1.millisecondsToDdHhMmSs)(0)).toEqual('0 day(s); 0 hour(s); 0 minute(s); 0 second(s)');
});
it('should process number with floating point', function () {
expect((0, utils_1.millisecondsToDdHhMmSs)(8 * 60 * 60 * 1000 + 0.93326263)).toEqual('0 day(s); 8 hour(s); 0 minute(s); 0 second(s)');
});
it('should process number', function () {
expect((0, utils_1.millisecondsToDdHhMmSs)(24 * 60 * 60 * 1000)).toEqual('1 day(s); 0 hour(s); 0 minute(s); 0 second(s)');
});
});
describe('inExistingProject', function () {
it('should return true if package.json exists in folder', function () { return __awaiter(void 0, void 0, void 0, function () {
var packagePath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
process.projectDir = process.cwd();
return [4 /*yield*/, (0, __1.setConstants)()];
case 1:
_a.sent();
packagePath = path_1.default.join(process.projectDir, folderPath, 'package.json');
return [4 /*yield*/, (0, utils_2.createFile)(packagePath, '{}')];
case 2:
_a.sent();
return [4 /*yield*/, expect((0, utils_1.inExistingProject)(folderPath)).resolves.toEqual(true)];
case 3:
_a.sent();
return [4 /*yield*/, (0, utils_2.deleteFile)(packagePath)];
case 4:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should return false if package.json does not exist in folder', function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, expect((0, utils_1.inExistingProject)(folderPath)).resolves.toEqual(false)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
});
describe('diff', function () {
it('should return an array of differences', function () {
expect((0, utils_1.diff)([1, 2, 3], [2])).toEqual([1, 3]);
expect((0, utils_1.diff)(['b'], ['a', 'b', 'c'])).toEqual(['a', 'c']);
expect((0, utils_1.diff)([{}, { test: 1 }], [{}])).toEqual([{ test: 1 }]);
expect((0, utils_1.diff)([[1], []], [[]])).toEqual([[1]]);
expect((0, utils_1.diff)([null, NaN, undefined], [[]])).toEqual([
null,
NaN,
undefined,
[]
]);
expect((0, utils_1.diff)([true], [false])).toEqual([true, false]);
});
it('should return an empty array if provided two equal arrays', function () {
expect((0, utils_1.diff)([2], [2])).toEqual([]);
expect((0, utils_1.diff)(['b'], ['b'])).toEqual([]);
expect((0, utils_1.diff)([{}], [{}])).toEqual([]);
expect((0, utils_1.diff)([[]], [[]])).toEqual([]);
expect((0, utils_1.diff)([null, NaN, undefined], [null, NaN, undefined])).toEqual([]);
expect((0, utils_1.diff)([true], [true])).toEqual([]);
});
});
describe('setupGitIgnore', function () {
process.projectDir = process.cwd();
beforeAll(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, __1.setConstants)()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
var gitFilePath = path_1.default.join(process.projectDir, folderPath, '.gitignore');
it('should create .gitignore file', function () { return __awaiter(void 0, void 0, void 0, function () {
var gitIgnoreContent;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, expect((0, utils_1.setupGitIgnore)(folderPath)).toResolve()];
case 1:
_a.sent();
return [4 /*yield*/, expect((0, utils_2.fileExists)(gitFilePath)).resolves.toEqual(true)];
case 2:
_a.sent();
return [4 /*yield*/, (0, utils_2.readFile)(gitFilePath)];
case 3:
gitIgnoreContent = _a.sent();
expect(gitIgnoreContent.includes('sasjsbuild')).toEqual(true);
return [4 /*yield*/, (0, utils_2.deleteFile)(gitFilePath)];
case 4:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should add sasjsbuild folder to .gitignore file', function () { return __awaiter(void 0, void 0, void 0, function () {
var gitIgnoreContent, regExp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
gitIgnoreContent = 'node_modules/';
return [4 /*yield*/, (0, utils_2.createFile)(gitFilePath, gitIgnoreContent)];
case 1:
_a.sent();
return [4 /*yield*/, expect((0, utils_1.setupGitIgnore)(folderPath)).toResolve()];
case 2:
_a.sent();
regExp = new RegExp("^node_modules/\nsasjsbuild\n", 'gm');
return [4 /*yield*/, (0, utils_2.readFile)(gitFilePath)];
case 3:
gitIgnoreContent = _a.sent();
expect(gitIgnoreContent.match(regExp)).toBeTruthy();
return [2 /*return*/];
}
});
}); });
it('should not add sasjsbuild folder to .gitignore file if such rule already exists', function () { return __awaiter(void 0, void 0, void 0, function () {
var gitIgnoreContent, regExp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, utils_2.readFile)(gitFilePath)];
case 1:
gitIgnoreContent = _a.sent();
return [4 /*yield*/, expect((0, utils_1.setupGitIgnore)(folderPath)).toResolve()];
case 2:
_a.sent();
regExp = new RegExp("^sasjsbuild", 'gm');
expect(gitIgnoreContent.match(regExp)).toBeTruthy();
expect(gitIgnoreContent.match(regExp).length).toEqual(1);
return [4 /*yield*/, (0, utils_2.deleteFile)(gitFilePath)];
case 3:
_a.sent();
return [2 /*return*/];
}
});
}); });
});
describe('chunk', function () {
it('should return an array with only one element that is equal to text itself if text size is less than max length', function () {
var txt = '0B0pFTOzHXyRuikRydwnd8uQdwvITllwNOX20vQLsT0JgkXa1W';
expect((0, utils_2.chunk)(txt)[0]).toBe(txt);
});
it('should return an array with 3 elements', function () {
var txt = 'SGmqg3pnHCJn0iPQC4Rd9AnFS3k5V7dDc81GlF6lCCurmsEJq7VS\nig42yjxerV7OAjE1HIOgJZXqID30\nkaVJsCwWrtFEIL8mQepjp4zLGeCaWk7ymu9BqolSnqA3udaiWuHcm6m80E36mWYWU0lITAgdp1QzVwIczpopXRaUWtJamiPnHyflksdNEXkboKlAT5RuR795ShUOc6KajQZ1qH2ZGJPV2DxjEqtXA9uvNbpisOVY6MpRdEzWNgdDsXlfuXkrBk5RqA7PkdtjBtNHA7JtEklTnYKrOdge03qFXKjh';
expect((0, utils_2.chunk)(txt)).toBeArrayOfSize(3);
});
});
describe('arrToObj', function () {
it('should return an object', function () {
var arr = ['a', 'b', 'c'];
expect((0, utils_1.arrToObj)(arr)).toEqual({
a: 'a',
b: 'b',
c: 'c'
});
});
});
describe('checkNodeVersion', function () {
afterEach(function () {
jest.resetAllMocks();
});
it('should exit the process when node version is less than 12', function () {
var originalProcess = global.process;
global.process = __assign(__assign({}, originalProcess), { versions: __assign(__assign({}, originalProcess.versions), { node: '11.0.0' }) });
var mockExit = (0, test_1.mockProcessExit)();
(0, utils_1.checkNodeVersion)();
expect(mockExit).toHaveBeenCalledWith(1);
global.process = originalProcess;
});
it('should not exit the process when node version greater than 12', function () {
jest.spyOn(process, 'exit');
(0, utils_1.checkNodeVersion)();
expect(process.exit).toHaveBeenCalledTimes(0);
});
});
describe('getAdapterInstance', function () {
it('should throw an error when target is not valid', function () {
expect(function () { return (0, utils_1.getAdapterInstance)(null); }).toThrow('Unable to create SASjs adapter instance: Invalid target.');
});
it('should throw an error when target is missing serverUrl', function () {
expect(function () { return (0, utils_1.getAdapterInstance)({ name: 'test-target' }); }).toThrow("Unable to create SASjs adapter instance: Target test-target is missing a `serverUrl`.");
});
it('should throw an error when target is missing serverType', function () {
expect(function () {
return (0, utils_1.getAdapterInstance)({
name: 'test-target',
serverUrl: 'http://server.com'
});
}).toThrow("Unable to create SASjs adapter instance: Target test-target is missing a `serverType`.");
});
it('should return an instance of SASjs', function () {
expect((0, utils_1.getAdapterInstance)({
name: 'test-target',
serverUrl: 'http://server.com',
serverType: utils_2.ServerType.SasViya,
contextName: 'test-context'
})).toBeInstanceOf(node_1.default);
});
});
describe('displaySasjsRunnerError', function () {
it('should have called once', function () {
var mockFun = jest.fn(utils_1.displaySasjsRunnerError);
mockFun('testUser');
expect(mockFun).toHaveBeenCalledTimes(1);
});
});
describe('getAbsolutePath', function () {
var homedir = require('os').homedir();
it('should convert relative path to absolute', function () {
var relativePath = './my-path/xyz';
var pathRelativeTo = '/home/abc';
var expectedAbsolutePath = ['', 'home', 'abc', 'my-path', 'xyz'].join(path_1.default.sep);
expect((0, utils_2.getAbsolutePath)(relativePath, pathRelativeTo)).toEqual(expectedAbsolutePath);
});
it('should convert tilde from absolute path', function () {
var relativePath = '~/my-path/xyz';
var expectedAbsolutePath = homedir + ['', 'my-path', 'xyz'].join(path_1.default.sep);
expect((0, utils_2.getAbsolutePath)(relativePath, '')).toEqual(expectedAbsolutePath);
});
it('should return same path if path is absolute', function () {
var absolutePath = '/my-path/xyz';
var pathRelativeTo = '/home/abc';
var expectedAbsolutePath = ['', 'my-path', 'xyz'].join(path_1.default.sep);
expect((0, utils_2.getAbsolutePath)(absolutePath, pathRelativeTo)).toEqual(expectedAbsolutePath);
});
});
describe('loadEnvVariables', function () {
var fileName = ".env.".concat((0, utils_2.generateTimestamp)());
var filePath = path_1.default.join(process.projectDir, fileName);
beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
var fileContent;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
fileContent = 'TEST=this is test variable';
return [4 /*yield*/, (0, utils_2.createFile)(filePath, fileContent)];
case 1:
_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_2.deleteFile)(filePath)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('should load the environment variables from provided file', 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.loadEnvVariables)(fileName)];
case 1:
_a.sent();
expect(process.env.TEST).toBe('this is test variable');
return [2 /*return*/];
}
});
}); });
});
describe('createReactApp', function () {
it('should create React app', function () { return __awaiter(void 0, void 0, void 0, function () {
var reactFolder, reactFolderPath, sasjsFolderPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
reactFolder = 'react-seed-app';
reactFolderPath = path_1.default.join(__dirname, reactFolder);
sasjsFolderPath = path_1.default.join(reactFolderPath, 'sasjs');
return [4 /*yield*/, (0, utils_2.createFolder)(reactFolderPath)];
case 1:
_a.sent();
return [4 /*yield*/, expect((0, utils_1.createReactApp)(reactFolderPath)).toResolve()];
case 2:
_a.sent();
return [4 /*yield*/, expect((0, utils_2.folderExists)(sasjsFolderPath)).resolves.toEqual(true)];
case 3:
_a.sent();
return [4 /*yield*/, (0, utils_2.deleteFolder)(reactFolderPath)];
case 4:
_a.sent();
return [2 /*return*/];
}
});
}); }, 600000);
});
});