• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revisionc036d958537eadd789cfc44e1071e92b848dcb5b (tree)
Time2016-01-20 23:06:59
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

Check Eos command execution parameters.

Change Summary

Incremental Difference

--- a/server/class/Eos.js
+++ b/server/class/Eos.js
@@ -12,9 +12,11 @@ function Eos() {
1212 */
1313 const commandReference = {
1414 mrcImageInfo: { },
15- dcdFilePring: { }
15+ dcdFilePrint: { }
1616 };
1717
18+ const workspace = ['file1.txt', 'file2.txt'];
19+
1820 return {
1921 /**
2022 * execute
@@ -33,13 +35,13 @@ function Eos() {
3335 * Check of command name
3436 */
3537 if(typeof command !== 'string') {
36- result.comment = 'command parameter need to be string';
38+ result.comment = 'Command parameter need to be string';
3739 return result;
3840 }
3941
40- const hasCommand = Object.keys(commandReference).indexOf(command) > 0;
42+ const hasCommand = Object.keys(commandReference).indexOf(command) > -1;
4143 if(!hasCommand) {
42- result.comment = 'command name is invalid';
44+ result.comment = 'Command name is invalid';
4345 return result;
4446 }
4547
@@ -47,9 +49,172 @@ function Eos() {
4749 * Check of options
4850 */
4951
52+ if(!(Array.isArray(options))) {
53+ result.comment = 'Options need to be Array';
54+ return result;
55+ }
56+
57+
58+ if(options.length === 0) {
59+ result.comment = 'At least one option is required.';
60+ return result;
61+ }
62+
63+ // translate options to key-value and check whether options include correct member
64+ const optionsObj = {};
65+ var hasCorrectMember = true;
66+ var isArgumentsArray = true;
67+
68+ options.forEach(function(o) {
69+ if(!(o.name) && !(o.arguments)) {
70+ hasCorrectMember = false;
71+ } else {
72+ if(Array.isArray(o.arguments)) {
73+ optionsObj[o.name] = o.arguments;
74+ } else {
75+ isArgumentsArray = false;
76+ }
77+ }
78+ });
79+
80+ // check each object has proberties "name" and "argumets"
81+ if(!hasCorrectMember) {
82+ result.comment = 'Options need to include Object which have properties "name" and "arguments"';
83+ return result;
84+ }
85+
86+ // check each "argumets" properties is Array
87+ if(!isArgumentsArray) {
88+ result.comment = 'Each "arguments" properties needs to be Array';
89+ return result;
90+ }
91+
92+ // Read OptionControlFile info of command
93+ const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
94+ var ocfObj = {};
95+
96+ // translate ocf info to key-value
97+ var notIncludingRequiredOptions = [];
98+ ocf.forEach(function(o) {
99+ if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
100+ notIncludingRequiredOptions.push(o.option);
101+ }
102+ ocfObj[o.option] = o;
103+ });
104+
105+ // check whether all required option exist
106+ if(notIncludingRequiredOptions.length > 0) {
107+ result.comment = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
108+ return result;
109+ }
50110
111+ var invalidArgumentsNumber= [];
112+ var invalidArgumentType = [];
113+ var invalidOutputFileName = [];
114+
115+ // output file Regexp
116+ const outRegExp = /out|append/;
117+
118+ options.forEach(function(o) {
119+ // option number
120+ const expectNum = ocfObj[o.name].optionNumber;
121+ const actualNum = o.arguments.length;
122+ if(expectNum !== actualNum) {
123+ invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
124+ }
125+
126+
127+ // argType and outFile name
128+ o.arguments.forEach(function(arg,i) {
129+ // argType
130+ var formType = ocfObj[o.name].arg[i].formType
131+ if(formType === 'select') { // This argument is filename
132+ var exist = workspace.indexOf(arg) > -1;
133+ if(!exist) {
134+ invalidArgumentType.push({name: o.name, file: arg});
135+ }
136+ } else {
137+ const expectType = formType === 'text' ? 'string' : 'number';
138+ const actualType = typeof arg;
139+ if(expectType !== actualType) {
140+ invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
141+ }
142+ }
143+
144+ // outFile name
145+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
146+ if(workspace.indexOf(o.arguments[i]) > -1) {
147+ invalidOutputFileName.push({name: o.name, file: arg});
148+ }
149+ }
150+ });
151+ });
152+
153+ // check arguments number value
154+ if(invalidArgumentsNumber.length > 0) {
155+ result.comment = '"arguments" properties is invalid number.\n';
156+ invalidArgumentsNumber.forEach(function(i) {
157+ result.comment += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
158+ });
159+ return result;
160+ }
161+
162+ // check arguments type
163+ if(invalidArgumentType.length > 0) {
164+ result.comment = '"arguments" type is invalid.\n';
165+ invalidArgumentType.forEach(function(i) {
166+ if(i.file) {
167+ result.comment += ' ' + i.name + ' ' + i.file + ' does not exist.\n';
168+ } else {
169+ result.comment += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
170+ }
171+ });
172+ return result;
173+ }
174+
175+ // check outFile name
176+ if(invalidOutputFileName.length > 0) {
177+ result.comment = 'output file name is invalid.\n';
178+ invalidOutputFileName.forEach(function(i) {
179+ result.comment += ' ' + i.name + ' ' + i.file + ' has already existed.\n';
180+ });
181+ return result;
182+ }
183+
184+ // set default parameters
185+ var finalOptions = {};
186+ ocf.forEach(function(o) {
187+ o.arg.forEach(function(arg) {
188+ if(!(arg.initialValue === "") && arg.initialValue) {
189+ if(!(finalOptions[o.option])) {
190+ finalOptions[o.option] = [];
191+ finalOptions[o.option].push(arg.initialValue);
192+ } else {
193+ finalOptions[o.option].push(arg.initialValue);
194+ }
195+ }
196+ });
197+ });
198+
199+ // set user setting parameters
200+ options.forEach(function(o) {
201+ finalOptions[o.name] = o.arguments;
202+ });
203+
204+ // set execution string
205+ var execStr = command + ' ';
206+ Object.keys(finalOptions).forEach(function(key) {
207+ execStr += key + ' ';
208+ finalOptions[key].forEach(function(arg) {
209+ execStr += arg + ' ';
210+ });
211+ });
212+
213+ // remove last blank
214+ execStr = execStr.slice(0,execStr.length-1);
215+
51216 /**
52- * End of Success
217+ * End in Success
53218 */
54219 result.hasDone = true;
55220 result.comment = command + ' has done.';
--- a/test/mocha/Eos.test.js
+++ b/test/mocha/Eos.test.js
@@ -2,30 +2,90 @@ var Eos = require('../../server/class/Eos');
22 var expect = require('chai').expect;
33
44 var test1 = `
5- /**
6- * Eosコマンドをエミュレートするクラス
7- *
8- * @param {string} Eosコマンドの名前
9- * @param {Array<Object>} コマンドに渡すオプション
10- * @returns
11- * function execute(command, params) {
12- */
13-
5+/**
6+* Eosコマンドをエミュレートするクラス
7+*
8+* @param {string} Eosコマンドの名前
9+* @param {Array<Object>} コマンドに渡すオプション
10+* @returns
11+* function execute(command, params) {
12+*/
13+
1414 `;
1515
1616 describe(/*execute()*/test1, function() {
1717 describe('#1: check whether command is valid or not', function() {
18+ var eos;
19+ before(function() {
20+ eos = new Eos();
21+ });
22+
1823 it('should return false when the command is typeof number.', function() {
19- const eos = new Eos();
20- const result = eos.execute(2);
24+ var result = eos.execute(2);
2125 expect(result.hasDone).to.equal(false);
2226 });
2327
2428 it('should return false when command name is invalid', function() {
25- const eos = new Eos();
26- const result = eos.execute('hoge');
29+ var result = eos.execute('hoge');
30+ expect(result.hasDone).to.equal(false);
31+ });
32+
33+ it('should return false when options is not Array', function() {
34+ var result = eos.execute('mrcImageInfo', {});
35+ expect(result.hasDone).to.equal(false);
36+ });
37+
38+ it('should return false when options is Array whose length is 0.', function() {
39+ var result = eos.execute('mrcImageInfo', []);
40+ expect(result.hasDone).to.equal(false);
41+ });
42+
43+ it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
44+ var result = eos.execute('mrcImageInfo', [{hoge: 'hoge'}]);
45+ expect(result.hasDone).to.equal(false);
46+ });
47+
48+ it('should return false when "argumetns" properties are not Array', function() {
49+ var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
50+ expect(result.hasDone).to.equal(false);
51+ });
52+
53+ it('should return false when "argumetns" properties are not Array', function() {
54+ var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
2755 expect(result.hasDone).to.equal(false);
2856 });
2957
58+ it('should return false when required options do not exist', function() {
59+ var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: []}]);
60+ expect(result.hasDone).to.equal(false);
61+ });
62+
63+ it('should return false when required options do not exist', function() {
64+ var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]);
65+ expect(result.hasDone).to.equal(false);
66+ });
67+
68+ it('should return false when input file does not exist', function() {
69+ var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]);
70+ expect(result.hasDone).to.equal(false);
71+ });
72+
73+ it('should return false when output file is not string', function() {
74+ var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]);
75+ expect(result.hasDone).to.equal(false);
76+ });
77+
78+ it('should return false when output file has already existed', function() {
79+ var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]);
80+ console.log(result.comment);
81+ expect(result.hasDone).to.equal(false);
82+ });
83+
84+ it('should return true when all options is proper.', function() {
85+ var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
86+ //console.log(result.comment);
87+ expect(result.hasDone).to.equal(true);
88+ });
89+
3090 });
3191 });
Show on old repository browser