2 * Copyright 2013, 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
18 var fs = require('fs-extra');
19 var path = require('path');
21 var localfilesystem = require("../../../red/storage/localfilesystem");
23 describe('LocalFileSystem', function() {
24 var userDir = path.join(__dirname,".testUserHome");
25 var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
26 beforeEach(function(done) {
27 fs.remove(userDir,function(err) {
28 fs.mkdir(userDir,done);
31 afterEach(function(done) {
32 fs.remove(userDir,done);
35 it('should initialise the user directory',function(done) {
36 localfilesystem.init({userDir:userDir}).then(function() {
37 fs.existsSync(path.join(userDir,"lib")).should.be.true;
38 fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true;
40 }).otherwise(function(err) {
45 it('should handle missing flow file',function(done) {
46 localfilesystem.init({userDir:userDir}).then(function() {
47 var flowFile = 'flows_'+require('os').hostname()+'.json';
48 var flowFilePath = path.join(userDir,flowFile);
49 fs.existsSync(flowFilePath).should.be.false;
50 localfilesystem.getFlows().then(function(flows) {
53 }).otherwise(function(err) {
56 }).otherwise(function(err) {
61 it('should save flows to the default file',function(done) {
62 localfilesystem.init({userDir:userDir}).then(function() {
63 var flowFile = 'flows_'+require('os').hostname()+'.json';
64 var flowFilePath = path.join(userDir,flowFile);
65 var flowFileBackupPath = path.join(userDir,"flows.backup");
66 fs.existsSync(flowFilePath).should.be.false;
67 fs.existsSync(flowFileBackupPath).should.be.false;
68 localfilesystem.saveFlows(testFlow).then(function() {
69 fs.existsSync(flowFilePath).should.be.true;
70 fs.existsSync(flowFileBackupPath).should.be.false;
71 localfilesystem.getFlows().then(function(flows) {
72 flows.should.eql(testFlow);
74 }).otherwise(function(err) {
77 }).otherwise(function(err) {
80 }).otherwise(function(err) {
85 it('should save flows to the specified file',function(done) {
86 var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
87 var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
88 var flowFile = 'test.json';
89 var flowFilePath = path.join(userDir,flowFile);
91 localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
92 fs.existsSync(defaultFlowFilePath).should.be.false;
93 fs.existsSync(flowFilePath).should.be.false;
95 localfilesystem.saveFlows(testFlow).then(function() {
96 fs.existsSync(defaultFlowFilePath).should.be.false;
97 fs.existsSync(flowFilePath).should.be.true;
98 localfilesystem.getFlows().then(function(flows) {
99 flows.should.eql(testFlow);
101 }).otherwise(function(err) {
104 }).otherwise(function(err) {
107 }).otherwise(function(err) {
112 it('should backup the flows file', function(done) {
113 var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
114 var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
115 var flowFile = 'test.json';
116 var flowFilePath = path.join(userDir,flowFile);
117 var flowFileBackupPath = path.join(userDir,"flows.backup");
119 localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
120 fs.existsSync(defaultFlowFilePath).should.be.false;
121 fs.existsSync(flowFilePath).should.be.false;
122 fs.existsSync(flowFileBackupPath).should.be.false;
124 localfilesystem.saveFlows(testFlow).then(function() {
125 fs.existsSync(flowFileBackupPath).should.be.false;
126 fs.existsSync(defaultFlowFilePath).should.be.false;
127 fs.existsSync(flowFilePath).should.be.true;
128 var content = fs.readFileSync(flowFilePath,'utf8');
129 var testFlow2 = [{"type":"tab","id":"bc5672ad.2741d8","label":"Sheet 2"}];
131 localfilesystem.saveFlows(testFlow2).then(function() {
132 fs.existsSync(flowFileBackupPath).should.be.true;
133 fs.existsSync(defaultFlowFilePath).should.be.false;
134 fs.existsSync(flowFilePath).should.be.true;
135 var backupContent = fs.readFileSync(flowFileBackupPath,'utf8');
136 content.should.equal(backupContent);
137 var content2 = fs.readFileSync(flowFilePath,'utf8');
138 content2.should.not.equal(backupContent);
141 }).otherwise(function(err) {
145 }).otherwise(function(err) {
148 }).otherwise(function(err) {
155 it('should handle missing credentials', function(done) {
156 var flowFile = 'test.json';
157 var flowFilePath = path.join(userDir,flowFile);
158 var credFile = path.join(userDir,"test_cred.json");
159 localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
160 fs.existsSync(credFile).should.be.false;
162 localfilesystem.getCredentials().then(function(creds) {
163 creds.should.eql({});
165 }).otherwise(function(err) {
168 }).otherwise(function(err) {
173 it('should handle credentials', function(done) {
174 var flowFile = 'test.json';
175 var flowFilePath = path.join(userDir,flowFile);
176 var credFile = path.join(userDir,"test_cred.json");
178 localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
180 fs.existsSync(credFile).should.be.false;
182 var credentials = {"abc":{"type":"creds"}};
184 localfilesystem.saveCredentials(credentials).then(function() {
185 fs.existsSync(credFile).should.be.true;
186 localfilesystem.getCredentials().then(function(creds) {
187 creds.should.eql(credentials);
189 }).otherwise(function(err) {
192 }).otherwise(function(err) {
195 }).otherwise(function(err) {
200 it('should return an empty list of library flows',function(done) {
201 localfilesystem.init({userDir:userDir}).then(function() {
202 localfilesystem.getAllFlows().then(function(flows) {
203 flows.should.eql({});
205 }).otherwise(function(err) {
208 }).otherwise(function(err) {
213 it('should return a valid list of library flows',function(done) {
214 localfilesystem.init({userDir:userDir}).then(function() {
215 var flowLib = path.join(userDir,"lib","flows");
216 fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w"));
217 fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w"));
218 fs.mkdirSync(path.join(flowLib,"C"));
219 fs.closeSync(fs.openSync(path.join(flowLib,"C","D.json"),"w"));
220 var testFlowsList = {"d":{"C":{"f":["D"]}},"f":["A","B"]};
222 localfilesystem.getAllFlows().then(function(flows) {
223 flows.should.eql(testFlowsList);
225 }).otherwise(function(err) {
228 }).otherwise(function(err) {
233 it('should fail a non-existent flow', function(done) {
234 localfilesystem.init({userDir:userDir}).then(function() {
235 localfilesystem.getFlow("a/b/c.json").then(function(flow) {
236 should.fail(flow,"No flow","Flow found");
237 }).otherwise(function(err) {
238 // err should be null, so this will pass
241 }).otherwise(function(err) {
246 it('should return a flow',function(done) {
247 localfilesystem.init({userDir:userDir}).then(function() {
248 var testflowString = JSON.stringify(testFlow);
249 localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() {
250 localfilesystem.getFlow("a/b/c/d.json").then(function(flow) {
251 flow.should.eql(testflowString);
253 }).otherwise(function(err) {
256 }).otherwise(function(err) {
259 }).otherwise(function(err) {
264 it('should return an empty list of library objects',function(done) {
265 localfilesystem.init({userDir:userDir}).then(function() {
266 localfilesystem.getLibraryEntry('object','').then(function(flows) {
267 flows.should.eql({});
269 }).otherwise(function(err) {
272 }).otherwise(function(err) {
277 it('should return an error for a non-existent library object',function(done) {
278 localfilesystem.init({userDir:userDir}).then(function() {
279 localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
280 should.fail(null,null,"non-existent flow");
281 }).otherwise(function(err) {
285 }).otherwise(function(err) {
290 function createObjectLibrary() {
291 var objLib = path.join(userDir,"lib","object");
292 fs.mkdirSync(objLib);
293 fs.mkdirSync(path.join(objLib,"A"));
294 fs.mkdirSync(path.join(objLib,"B"));
295 fs.mkdirSync(path.join(objLib,"B","C"));
296 fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
297 fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
300 it('should return a directory listing of library objects',function(done) {
301 localfilesystem.init({userDir:userDir}).then(function() {
302 createObjectLibrary();
304 localfilesystem.getLibraryEntry('object','').then(function(flows) {
305 flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
306 localfilesystem.getLibraryEntry('object','B').then(function(flows) {
307 flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
308 localfilesystem.getLibraryEntry('object','B/C').then(function(flows) {
309 flows.should.eql([]);
311 }).otherwise(function(err) {
314 }).otherwise(function(err) {
317 }).otherwise(function(err) {
320 }).otherwise(function(err) {
325 it('should return a library object',function(done) {
326 localfilesystem.init({userDir:userDir}).then(function() {
327 createObjectLibrary();
328 localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) {
329 body.should.eql("// not a metaline \n\n Hi");
331 }).otherwise(function(err) {
334 }).otherwise(function(err) {
339 it('should return a newly saved library object',function(done) {
340 localfilesystem.init({userDir:userDir}).then(function() {
341 createObjectLibrary();
342 localfilesystem.getLibraryEntry('object','B').then(function(flows) {
343 flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
344 localfilesystem.saveLibraryEntry('object','B/D/file3.js',{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
345 localfilesystem.getLibraryEntry('object','B/D').then(function(flows) {
346 flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
347 localfilesystem.getLibraryEntry('object','B/D/file3.js').then(function(body) {
348 body.should.eql("// another non meta line\n\n Hi There");
350 }).otherwise(function(err) {
353 }).otherwise(function(err) {
356 }).otherwise(function(err) {
359 }).otherwise(function(err) {
362 }).otherwise(function(err) {