pathUtil.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var pathUtil = require('../../lib/util/pathUtil');
  2. var utils = require('../../lib/util/utils');
  3. var should = require('should');
  4. var fs = require('fs');
  5. var mockBase = process.cwd() + '/test/mock-base';
  6. describe('path util test', function() {
  7. describe('#getSysRemotePath', function() {
  8. it('should return the system remote service path for frontend server', function() {
  9. var role = 'frontend';
  10. var expectSuffix = '/common/remote/frontend';
  11. var p = pathUtil.getSysRemotePath(role);
  12. should.exist(p);
  13. fs.existsSync(p).should.be.true;
  14. utils.endsWith(p, expectSuffix).should.be.true;
  15. });
  16. it('should return the system remote service path for backend server', function() {
  17. var role = 'backend';
  18. var expectSuffix = '/common/remote/backend';
  19. var p = pathUtil.getSysRemotePath(role);
  20. should.exist(p);
  21. fs.existsSync(p).should.be.true;
  22. utils.endsWith(p, expectSuffix).should.be.true;
  23. });
  24. });
  25. describe('#getUserRemotePath', function() {
  26. it('should return userstate remote service path for the associated server type', function() {
  27. var serverType = 'connector';
  28. var expectSuffix = '/app/servers/connector/remote';
  29. var p = pathUtil.getUserRemotePath(mockBase, serverType);
  30. should.exist(p);
  31. fs.existsSync(p).should.be.true;
  32. utils.endsWith(p, expectSuffix).should.be.true;
  33. });
  34. it('should return null if the directory not exist', function() {
  35. var serverType = 'area';
  36. var p = pathUtil.getUserRemotePath(mockBase, serverType);
  37. should.not.exist(p);
  38. serverType = 'some-dir-not-exist';
  39. p = pathUtil.getUserRemotePath(mockBase, serverType);
  40. should.not.exist(p);
  41. });
  42. });
  43. describe('#listUserRemoteDir', function() {
  44. it('should return sub-direcotry name list of servers/ directory', function() {
  45. var expectNames = ['connector', 'area'];
  46. var p = pathUtil.listUserRemoteDir(mockBase);
  47. should.exist(p);
  48. expectNames.length.should.equal(p.length);
  49. for(var i=0, l=expectNames.length; i<l; i++) {
  50. p.should.include(expectNames[i]);
  51. }
  52. });
  53. it('should throw err if the servers/ illegal', function() {
  54. (function() {
  55. pathUtil.listUserRemoteDir('some illegal base');
  56. }).should.throw();
  57. });
  58. });
  59. describe('#remotePathRecord', function() {
  60. var namespace = 'user';
  61. var serverType = 'connector';
  62. var path = '/some/path/to/remote';
  63. var r = pathUtil.remotePathRecord(namespace, serverType, path);
  64. should.exist(r);
  65. namespace.should.equal(r.namespace);
  66. serverType.should.equal(r.serverType);
  67. path.should.equal(r.path);
  68. });
  69. describe('#getHandlerPath', function() {
  70. it('should return userstate handler path for the associated server type', function() {
  71. var serverType = 'connector';
  72. var expectSuffix = '/app/servers/connector/handler';
  73. var p = pathUtil.getHandlerPath(mockBase, serverType);
  74. should.exist(p);
  75. fs.existsSync(p).should.be.true;
  76. utils.endsWith(p, expectSuffix).should.be.true;
  77. });
  78. it('should return null if the directory not exist', function() {
  79. var serverType = 'area';
  80. var p = pathUtil.getHandlerPath(mockBase, serverType);
  81. should.not.exist(p);
  82. serverType = 'some-dir-not-exist';
  83. p = pathUtil.getHandlerPath(mockBase, serverType);
  84. should.not.exist(p);
  85. });
  86. });
  87. describe('#getScriptPath', function() {
  88. var p = pathUtil.getScriptPath(mockBase);
  89. var expectSuffix = '/scripts';
  90. should.exist(p);
  91. utils.endsWith(p, expectSuffix).should.be.true;
  92. });
  93. describe('#getLogPath', function() {
  94. var p = pathUtil.getLogPath(mockBase);
  95. var expectSuffix = '/logs';
  96. should.exist(p);
  97. utils.endsWith(p, expectSuffix).should.be.true;
  98. });
  99. });