Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

common.spec.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. *
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an
  12. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. * KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations
  15. * under the License.
  16. *
  17. */
  18. var rewire = require('rewire');
  19. var common = rewire('../../../bin/templates/cordova/lib/pluginHandlers');
  20. var path = require('path');
  21. var fs = require('fs');
  22. var osenv = require('os');
  23. var shell = require('shelljs');
  24. var test_dir = path.join(osenv.tmpdir(), 'test_plugman');
  25. var project_dir = path.join(test_dir, 'project');
  26. var src = path.join(project_dir, 'src');
  27. var dest = path.join(project_dir, 'dest');
  28. var java_dir = path.join(src, 'one', 'two', 'three');
  29. var java_file = path.join(java_dir, 'test.java');
  30. var symlink_file = path.join(java_dir, 'symlink');
  31. var non_plugin_file = path.join(osenv.tmpdir(), 'non_plugin_file');
  32. var copyFile = common.__get__('copyFile');
  33. var deleteJava = common.__get__('deleteJava');
  34. var copyNewFile = common.__get__('copyNewFile');
  35. describe('common platform handler', function () {
  36. describe('copyFile', function () {
  37. it('Test#001 : should throw if source path not found', function () {
  38. shell.rm('-rf', src);
  39. expect(function () { copyFile(test_dir, src, project_dir, dest); })
  40. .toThrow(new Error('"' + src + '" not found!'));
  41. });
  42. it('Test#002 : should throw if src not in plugin directory', function () {
  43. shell.mkdir('-p', project_dir);
  44. fs.writeFileSync(non_plugin_file, 'contents', 'utf-8');
  45. var outside_file = '../non_plugin_file';
  46. expect(function () { copyFile(test_dir, outside_file, project_dir, dest); })
  47. .toThrow(new Error('File "' + path.resolve(test_dir, outside_file) + '" is located outside the plugin directory "' + test_dir + '"'));
  48. shell.rm('-rf', test_dir);
  49. });
  50. it('Test#003 : should allow symlink src, if inside plugin', function () {
  51. shell.mkdir('-p', java_dir);
  52. fs.writeFileSync(java_file, 'contents', 'utf-8');
  53. // This will fail on windows if not admin - ignore the error in that case.
  54. if (ignoreEPERMonWin32(java_file, symlink_file)) {
  55. return;
  56. }
  57. copyFile(test_dir, symlink_file, project_dir, dest);
  58. shell.rm('-rf', project_dir);
  59. });
  60. it('Test#004 : should throw if symlink is linked to a file outside the plugin', function () {
  61. shell.mkdir('-p', java_dir);
  62. fs.writeFileSync(non_plugin_file, 'contents', 'utf-8');
  63. // This will fail on windows if not admin - ignore the error in that case.
  64. if (ignoreEPERMonWin32(non_plugin_file, symlink_file)) {
  65. return;
  66. }
  67. expect(function () { copyFile(test_dir, symlink_file, project_dir, dest); })
  68. .toThrow(new Error('File "' + path.resolve(test_dir, symlink_file) + '" is located outside the plugin directory "' + test_dir + '"'));
  69. shell.rm('-rf', project_dir);
  70. });
  71. it('Test#005 : should throw if dest is outside the project directory', function () {
  72. shell.mkdir('-p', java_dir);
  73. fs.writeFileSync(java_file, 'contents', 'utf-8');
  74. expect(function () { copyFile(test_dir, java_file, project_dir, non_plugin_file); })
  75. .toThrow(new Error('Destination "' + path.resolve(project_dir, non_plugin_file) + '" for source file "' + path.resolve(test_dir, java_file) + '" is located outside the project'));
  76. shell.rm('-rf', project_dir);
  77. });
  78. it('Test#006 : should call mkdir -p on target path', function () {
  79. shell.mkdir('-p', java_dir);
  80. fs.writeFileSync(java_file, 'contents', 'utf-8');
  81. var s = spyOn(shell, 'mkdir').and.callThrough();
  82. var resolvedDest = path.resolve(project_dir, dest);
  83. copyFile(test_dir, java_file, project_dir, dest);
  84. expect(s).toHaveBeenCalled();
  85. expect(s).toHaveBeenCalledWith('-p', path.dirname(resolvedDest));
  86. shell.rm('-rf', project_dir);
  87. });
  88. it('Test#007 : should call cp source/dest paths', function () {
  89. shell.mkdir('-p', java_dir);
  90. fs.writeFileSync(java_file, 'contents', 'utf-8');
  91. var s = spyOn(shell, 'cp').and.callThrough();
  92. var resolvedDest = path.resolve(project_dir, dest);
  93. copyFile(test_dir, java_file, project_dir, dest);
  94. expect(s).toHaveBeenCalled();
  95. expect(s).toHaveBeenCalledWith('-f', java_file, resolvedDest);
  96. shell.rm('-rf', project_dir);
  97. });
  98. });
  99. describe('copyNewFile', function () {
  100. it('Test#008 : should throw if target path exists', function () {
  101. shell.mkdir('-p', dest);
  102. expect(function () { copyNewFile(test_dir, src, project_dir, dest); })
  103. .toThrow(new Error('"' + dest + '" already exists!'));
  104. shell.rm('-rf', dest);
  105. });
  106. });
  107. describe('deleteJava', function () {
  108. beforeEach(function () {
  109. shell.mkdir('-p', java_dir);
  110. fs.writeFileSync(java_file, 'contents', 'utf-8');
  111. });
  112. afterEach(function () {
  113. shell.rm('-rf', java_dir);
  114. });
  115. it('Test#009 : should call fs.unlinkSync on the provided paths', function () {
  116. var s = spyOn(fs, 'unlinkSync').and.callThrough();
  117. deleteJava(project_dir, java_file);
  118. expect(s).toHaveBeenCalled();
  119. expect(s).toHaveBeenCalledWith(path.resolve(project_dir, java_file));
  120. });
  121. it('Test#010 : should delete empty directories after removing source code in a java src path hierarchy', function () {
  122. deleteJava(project_dir, java_file);
  123. expect(fs.existsSync(java_file)).not.toBe(true);
  124. expect(fs.existsSync(java_dir)).not.toBe(true);
  125. expect(fs.existsSync(path.join(src, 'one'))).not.toBe(true);
  126. });
  127. it('Test#011 : should never delete the top-level src directory, even if all plugins added were removed', function () {
  128. deleteJava(project_dir, java_file);
  129. expect(fs.existsSync(src)).toBe(true);
  130. });
  131. });
  132. });
  133. function ignoreEPERMonWin32 (symlink_src, symlink_dest) {
  134. try {
  135. fs.symlinkSync(symlink_src, symlink_dest);
  136. } catch (e) {
  137. if (process.platform === 'win32' && e.message.indexOf('Error: EPERM, operation not permitted' > -1)) {
  138. return true;
  139. }
  140. throw e;
  141. }
  142. return false;
  143. }