UNPKG

9.86 kBJavaScriptView Raw
1var assert = require ('assert');
2var path = require ('path');
3var commandLogic = require ('../lib/cli/commands-logic');
4var rm = require ('rimraf');
5var fs = require ('fs');
6var async = require('async');
7
8function assertFileContains(path, text, cb){
9 fs.readFile(path, 'UTF-8', function read(err, data) {
10 assert.ifError(err);
11 assert.ok(data.indexOf(text)> -1, 'expected file to contain ' + text + '. Actual content: ' + data);
12 cb();
13 });
14}
15
16describe ('cli commands logic', function(){
17
18 require('./includes/startup');
19
20 var temp_folder = "test/tmp";
21 var packagesLocalFolder = path.join(process.cwd(), "/test/fixtures/packages");
22
23 function cleanup (cb){
24 rm(temp_folder, function(){
25 fs.mkdir(temp_folder, cb);
26 });
27 }
28
29 //make sure temp folder is deleted even if tests fail (before and after)
30 beforeEach(function(done){
31 cleanup(done);
32 });
33
34 afterEach(function(done){
35 cleanup(done);
36 });
37
38 describe ('new', function(){
39
40 it('should create a new project ok', function(done){
41 var projectPath = path.join(temp_folder, 'test');
42 commandLogic.newProject("samples/project", projectPath, function(err){
43 assert.ok(!err, err);
44 assert.ok(fs.existsSync(path.join(projectPath, "package.json")));
45 assert.ok(fs.existsSync(path.join(projectPath, "globalAuth.json")));
46 assert.ok(fs.existsSync(path.join(projectPath, "assets")));
47 assert.ok(fs.existsSync(path.join(projectPath, "packages")));
48 assert.ok(fs.existsSync(path.join(projectPath, "packages", "demo", "dashboards")));
49 assertFileContains(path.join(projectPath, "README.md"), "# test, my awesome", done); // templating works
50 });
51 });
52
53 it('should return error if file name is not considered safe or valid', function(done){
54
55 function test (name, cb){
56 rm(temp_folder, function(err){
57 fs.mkdir(temp_folder, function(err){
58 commandLogic.newProject('samples/project', path.join(temp_folder, '' + name), function(err){
59 cb(null, !err);
60 });
61 });
62 });
63 }
64
65 var invalidNames = ['sample=', 'sample(', 'sample&','s#ample', 'samp@le', 'sample!wer'];
66 var validNames = [33, '33', 'sample33', 'samp-le', 'samp_le'];
67
68 function isInvalid(valid){return !valid;}
69 function isValid(valid){return valid;}
70
71 async.mapSeries(invalidNames, test, function(err, results){
72 assert.ok(results.every(isInvalid));
73 async.mapSeries(validNames, test, function(err, results){
74 assert.ok(results.every(isValid));
75 done();
76 });
77 });
78 });
79
80 it('should return error if invalid path', function(done){
81 commandLogic.newProject("samples/project", '/etc/', function(err){
82 assert.ok(err);
83 done();
84 });
85 });
86
87 it('should not create a new project from a folder where a atlasboard project already exists', function(done){
88 commandLogic.newProject("samples/project", temp_folder, function(err){
89 assert.ok(err);
90 done();
91 });
92 });
93
94 it('should have a valid config file', function(done){ //avoid shiping an invalid config file
95 var projectPath = path.join(temp_folder, 'test');
96 commandLogic.newProject("samples/project", projectPath, function(err){
97 var config_path_contents = fs.readFileSync(path.join(projectPath, "config", "dashboard_common.json"));
98 var JSONconfig = JSON.parse(config_path_contents);
99 assert.ok(JSONconfig.config);
100 done();
101 });
102 });
103
104 });
105
106
107 describe ('generate', function(){
108 var projectPath = path.join(temp_folder, 'test');
109
110 beforeEach(function(done){
111 fs.mkdir(temp_folder, function(){
112 commandLogic.newProject("samples/project", projectPath, done);
113 });
114 });
115
116 it('should return error if bad resource type provided', function(done){
117 commandLogic.generate("/ba/ddir", "default", "widgettt", "mywidget", function(err){
118 assert.ok(err.indexOf("Invalid generator")>-1, err);
119 done();
120 });
121 });
122
123 it('should return error if no project exists in the provided path', function(done){
124 commandLogic.generate("/b/addir", "default", "widget", "mywidget", function(err){
125 assert.ok(err.indexOf("no project exists")>-1, err);
126 done();
127 });
128 });
129
130 it('should return error if unsafe item name is provided', function(done){
131 commandLogic.generate(projectPath, "default", "widget", "../mywidget", function(err){
132 assert.ok(err.indexOf("invalid")>-1, err);
133 done();
134 });
135 });
136
137 it('should return error if no item name is provided', function(done){
138 commandLogic.generate(projectPath, "default", "widget", "", function(err){
139 assert.ok(err.indexOf("invalid")>-1, err);
140 done();
141 });
142 });
143
144 describe ('widget', function(){
145 it('should create widget successfully', function(done){
146 commandLogic.generate(projectPath, "default", "widget", "newcalendar", function(err, data){
147 assert.ok(!err, err);
148 var htmlFileWidget = path.join(projectPath, "packages", "default", "widgets", "newcalendar", 'newcalendar.html');
149 assertFileContains(htmlFileWidget, '<h2>newcalendar</h2>', done);
150 });
151 });
152
153 it('should return error if widget already exists', function(done){
154 commandLogic.generate(projectPath, "default", "widget", "newcalendar", function(err, data){
155 commandLogic.generate(projectPath, "default", "widget", "newcalendar", function(err){
156 assert.ok(err, err);
157 done();
158 });
159 });
160 });
161 });
162
163
164 describe ('job', function(){
165 it('should create job successfully', function(done){
166 commandLogic.generate(projectPath, "default", "job", "newcalendar", function(err, data){
167 assert.ok(!err);
168 var jobPath = path.join(projectPath, "packages", "default", "jobs", "newcalendar", "newcalendar.js");
169 assertFileContains(jobPath, '* Job: newcalendar', done);
170 });
171 });
172
173 it('should return error if job already exists', function(done){
174 commandLogic.generate(projectPath, "default", "job", "newcalendar", function(err, data){
175 commandLogic.generate(projectPath, "default", "job", "newcalendar", function(err){
176 assert.ok(err, err);
177 done();
178 });
179 });
180 });
181
182 it('should create a unit test template', function(done){
183 commandLogic.generate(projectPath, "default", "job", "newcalendar", function(err, data){
184 assert.ok(!err);
185 var jobTestPath = path.join(projectPath, "packages", "default", "jobs", "newcalendar", "test", "test-newcalendar.js");
186 assertFileContains(jobTestPath, '* Test file for Job: newcalendar', done);
187 });
188 });
189
190 });
191
192 describe ('dashboard', function(){
193 it('should create dashboard successfully', function(done){
194 commandLogic.generate(projectPath, "default", "dashboard", "newdashboard", function(err, data){
195 assert.ok(!err, 'error generating dashboard: ' + err);
196 var dashboard_folder = path.join(projectPath, "packages", "default", "dashboards");
197 var dashboard_file = path.join(dashboard_folder, "newdashboard.json");
198 assertFileContains(dashboard_file, '"layout": {', done);
199 });
200 });
201
202 it('should return error if dashboard already exists', function(done){
203 commandLogic.generate(temp_folder, "default", "dashboard", "newdashboard", function(err){
204 assert.ok(err, err);
205 done();
206 });
207 });
208 });
209
210 });
211
212 describe ('list', function(){
213 it('should list available components', function(done){
214 commandLogic.list(packagesLocalFolder, function(err, lists){
215
216 assert.ok(Array.isArray(lists));
217 assert.equal(lists.length, 1);
218 assert.equal(lists[0].package, packagesLocalFolder);
219
220 assert.equal(lists[0].widgets.length, 2);
221
222 assert.ok(lists[0].widgets[0].dir.indexOf('/test/fixtures/packages/default')>-1);
223 assert.equal(lists[0].widgets[0].items.length, 0);
224
225 assert.ok(lists[0].widgets[1].dir.indexOf('/test/fixtures/packages/otherpackage1')>-1);
226 assert.equal(lists[0].widgets[1].items.length, 1);
227 assert.ok(lists[0].widgets[1].items[0].indexOf('test/fixtures/packages/otherpackage1/widgets/blockers/blockers.js')>-1);
228
229 assert.equal(lists[0].jobs.length, 2);
230
231 assert.ok(lists[0].jobs[0].dir.indexOf('/test/fixtures/packages/default')>-1);
232 assert.equal(lists[0].jobs[0].items.length, 3);
233 assert.ok(lists[0].jobs[0].items[0].indexOf('/test/fixtures/packages/default/jobs/job1/job1.js')>-1);
234
235 assert.ok(lists[0].jobs[1].dir.indexOf('/test/fixtures/packages/otherpackage1')>-1);
236 assert.equal(lists[0].jobs[1].items.length, 3);
237 assert.ok(lists[0].jobs[1].items[0].indexOf('/test/fixtures/packages/otherpackage1/jobs/job1/job1.js')>-1);
238
239 done();
240 });
241 });
242
243 it('should handle invalid package folders', function(done){
244 var emptyPackagesFolder = 'test/config';
245 commandLogic.list(emptyPackagesFolder, function(err, lists){
246 assert.ifError(err);
247 assert.equal(lists.length, 0);
248 done();
249 });
250 });
251
252 it('should not repeat results', function(done){
253 commandLogic.list([packagesLocalFolder, packagesLocalFolder, packagesLocalFolder], function(err, lists){
254 assert.ifError(err);
255 assert.equal(lists.length, 1);
256 done();
257 });
258 });
259
260 });
261});
\No newline at end of file