Skip to content

Commit

Permalink
meta-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Kuminov committed Mar 10, 2020
1 parent 8cc6ff0 commit d633712
Show file tree
Hide file tree
Showing 5 changed files with 531 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
env:
mocha: true # adds all of the Mocha testing global variables
protractor: true # Protractor global variables
globals:
app: false
assert: true
ionTestParam: true
page: true
browser: true
describe: true
it: true
before: true
after: true
rules:
no-console: off
no-sync: off # Разрешено использовать синхронные функции в тесте
no-invalid-this: off # Разрешаем использование this - нужно для mocha
125 changes: 125 additions & 0 deletions test/lib/get-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const fs = require('fs');
const path = require('path');


const ARR_NOTFOUND = -1;
const JSON_EXT = '.json';
/*
Нормализация имени класса, с учётом нейспейса
*/
function nz(className, namespace) {
if (className && className.indexOf('@') === ARR_NOTFOUND && namespace) {
return `${className}@${namespace}`;
}
return className;
}
module.exports.normilizeNamespase = nz;

function getNS(className) {
if (className) {
let symNameSpace = className.indexOf('@');
if (symNameSpace !== ARR_NOTFOUND) {
return className.substring(++symNameSpace);
}
}
return '';
}
module.exports.getNameSpace = getNS;

function getBaseName(className) {
if (className) {
const symNameSpace = className.indexOf('@');
if (symNameSpace !== ARR_NOTFOUND) {
return className.substring(0, symNameSpace);
}
}
return className;
}
module.exports.getBaseName = getBaseName;

function getDirList(sourcePath) {
const fileList = [];
const dirList = [];
try {
fs.accessSync(sourcePath, fs.constants.F_OK);
const files = fs.readdirSync(sourcePath);
for (let i = 0; i < files.length; i++) {
const stat = fs.lstatSync(path.join(sourcePath, files[i]));
if (stat.isDirectory()) {
dirList.push(files[i]);
} else {
fileList.push(files[i]);
}
}
} catch (err) {
throw err;
}
return {
dirList,
fileList
};
}
module.exports.getDirList = getDirList;

function getViewsList(sourcePath, ignore) {
const ns = path.basename(sourcePath) === 'workflows' ?
path.basename(path.join(sourcePath, '../..')) :
path.basename(path.join(sourcePath, '..'));
const viewsList = [];
getDirList(sourcePath).dirList.forEach((viewName) => {
if (!ignore || ignore.indexOf(viewName) === ARR_NOTFOUND) {
viewsList.push(nz(viewName, ns));
}
});
return viewsList;
}
module.exports.getViewsList = getViewsList;

function getMetaFiles(pathMeta, meta = {}) {
const metaType = path.basename(pathMeta);
const ns = path.basename(path.join(pathMeta, '..'));
processDir(pathMeta,
(nm) => {return nm.substr(-JSON_EXT.length) === JSON_EXT;},
(fn) => {
try {
let tempMetaClass = require(fn);
if (metaType === 'meta') {
meta[nz(tempMetaClass.name, tempMetaClass.namespace || ns)] = tempMetaClass;
} else if (metaType === 'navigation') {
meta[tempMetaClass.code] = tempMetaClass;
} else if (metaType === 'workflows') {
meta[nz(tempMetaClass.name, ns)] = tempMetaClass;
} else {
console.error('Необрабатываемый тип меты', metaType);
}
} catch (err) {
console.error('Ошибка в', err.message);
}
},
(err) => {console.error('Ошибка считывания файлов', err);});
return meta;
}

module.exports.getMetaFiles = getMetaFiles;

/*
* Основана на const processDir = require('core/util/read').processDir;
*/
function processDir(dir, filter, handler) {
try {
fs.accessSync(dir, fs.constants.F_OK);
let files = fs.readdirSync(dir);
for (let i = 0; i < files.length; i++) {
let fn = path.join(dir, files[i]);
let stat = fs.lstatSync(fn);
if (stat.isDirectory()) {
processDir(fn, filter, handler);
} else if (filter(files[i])) {
handler(fn);
}
}
} catch (err) {
throw err;
}
}

33 changes: 33 additions & 0 deletions test/quality/files-format.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Тестируем проверку соответствия файлов меты формату JSON
*/
const path = require('path');

const processDir = require('core/util/read').processDir;

describe('# Проверка соответствия файлов метаданных форматам', function () {
this.timeout(120000);
const pathApp = path.join(__dirname, '../../applications');
it('Проверка соответствия формату JSON в ' + pathApp, (done) => {
let filesList = [];
let errFiles = [];
processDir(pathApp,
(nm) => {return nm.substr(-5) === '.json';},
(fn) => {if (fn.indexOf('node_modules') === -1) {
filesList.push(fn);
try {
require(fn);
} catch (err) {
errFiles.push(fn);
console.error('Ошибка в', err.message);
}
}},
(err) => {console.error('Ошибка считывания файлов', err);});
if (errFiles.length) {
done(new Error ('В файлах метаданных и данных ошибка в формате JSON'));
} else {
console.info('Проверенно JSON файлов', filesList.length);
done();
}
});
});
Loading

0 comments on commit d633712

Please sign in to comment.