web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.9 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const assert = require("assert");
  4. const path = require("path");
  5. const sinon = require("sinon");
  6. const fs_macchiato_1 = require("../../../fs.macchiato");
  7. const constants_1 = require("../constants");
  8. const settings_1 = require("../settings");
  9. const provider = require("./async");
  10. const ROOT_PATH = 'root';
  11. const FIRST_FILE_PATH = 'first.txt';
  12. const SECOND_FILE_PATH = 'second.txt';
  13. const FIRST_ENTRY_PATH = path.join(ROOT_PATH, FIRST_FILE_PATH);
  14. const SECOND_ENTRY_PATH = path.join(ROOT_PATH, SECOND_FILE_PATH);
  15. describe('Providers → Async', () => {
  16. describe('.read', () => {
  17. it('should call correct method based on Node.js version', (done) => {
  18. const readdir = sinon.stub();
  19. readdir.yields(null, []);
  20. const settings = new settings_1.default({
  21. fs: { readdir: readdir }
  22. });
  23. provider.read(ROOT_PATH, settings, (error, entries) => {
  24. assert.strictEqual(error, null);
  25. assert.deepStrictEqual(entries, []);
  26. if (constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  27. sinon.assert.match(readdir.args, [[ROOT_PATH, { withFileTypes: true }, sinon.match.func]]);
  28. }
  29. else {
  30. sinon.assert.match(readdir.args, [[ROOT_PATH, sinon.match.func]]);
  31. }
  32. done();
  33. });
  34. });
  35. it('should always use `readdir` method when the `stats` option is enabled', (done) => {
  36. const readdir = sinon.stub();
  37. readdir.yields(null, []);
  38. const settings = new settings_1.default({
  39. fs: { readdir: readdir },
  40. stats: true
  41. });
  42. provider.read(ROOT_PATH, settings, (error, entries) => {
  43. assert.strictEqual(error, null);
  44. assert.deepStrictEqual(entries, []);
  45. sinon.assert.match(readdir.args, [[ROOT_PATH, sinon.match.func]]);
  46. done();
  47. });
  48. });
  49. });
  50. describe('.readdirWithFileTypes', () => {
  51. it('should return entries', (done) => {
  52. const dirent = new fs_macchiato_1.Dirent({ name: FIRST_FILE_PATH });
  53. const readdir = sinon.stub();
  54. readdir.yields(null, [dirent]);
  55. const settings = new settings_1.default({
  56. fs: { readdir: readdir }
  57. });
  58. const expected = [
  59. {
  60. dirent,
  61. name: FIRST_FILE_PATH,
  62. path: FIRST_ENTRY_PATH
  63. }
  64. ];
  65. provider.readdirWithFileTypes(ROOT_PATH, settings, (error, entries) => {
  66. assert.strictEqual(error, null);
  67. sinon.assert.match(readdir.args, [[ROOT_PATH, { withFileTypes: true }, sinon.match.func]]);
  68. assert.deepStrictEqual(entries, expected);
  69. done();
  70. });
  71. });
  72. it('should call fs.stat for symbolic link when the "followSymbolicLink" option is enabled', (done) => {
  73. const firstDirent = new fs_macchiato_1.Dirent({ name: FIRST_FILE_PATH });
  74. const secondDirent = new fs_macchiato_1.Dirent({ name: SECOND_FILE_PATH, isSymbolicLink: true });
  75. const stats = new fs_macchiato_1.Stats();
  76. const readdir = sinon.stub();
  77. const stat = sinon.stub();
  78. readdir.yields(null, [firstDirent, secondDirent]);
  79. stat.yields(null, stats);
  80. const settings = new settings_1.default({
  81. followSymbolicLinks: true,
  82. fs: {
  83. readdir: readdir,
  84. stat: stat
  85. }
  86. });
  87. provider.readdirWithFileTypes(ROOT_PATH, settings, (error, entries) => {
  88. assert.strictEqual(error, null);
  89. assert.strictEqual(entries.length, 2);
  90. assert.ok(!entries[1].dirent.isSymbolicLink());
  91. sinon.assert.match(stat.args, [[SECOND_ENTRY_PATH, sinon.match.func]]);
  92. done();
  93. });
  94. });
  95. it('should return lstat for broken symbolic link when the "throwErrorOnBrokenSymbolicLink" option is disabled', (done) => {
  96. const firstDirent = new fs_macchiato_1.Dirent({ name: FIRST_FILE_PATH, isSymbolicLink: true });
  97. const readdir = sinon.stub();
  98. const stat = sinon.stub();
  99. readdir.yields(null, [firstDirent]);
  100. stat.yields(new Error('error'));
  101. const settings = new settings_1.default({
  102. followSymbolicLinks: true,
  103. throwErrorOnBrokenSymbolicLink: false,
  104. fs: {
  105. readdir: readdir,
  106. stat: stat
  107. }
  108. });
  109. provider.readdirWithFileTypes(ROOT_PATH, settings, (error, entries) => {
  110. assert.strictEqual(error, null);
  111. assert.strictEqual(entries.length, 1);
  112. assert.ok(entries[0].dirent.isSymbolicLink());
  113. done();
  114. });
  115. });
  116. it('should throw an error fro broken symbolic link when the "throwErrorOnBrokenSymbolicLink" option is enabled', (done) => {
  117. const firstDirent = new fs_macchiato_1.Dirent({ name: FIRST_FILE_PATH, isSymbolicLink: true });
  118. const readdir = sinon.stub();
  119. const stat = sinon.stub();
  120. readdir.yields(null, [firstDirent]);
  121. stat.yields(new Error('error'));
  122. const settings = new settings_1.default({
  123. followSymbolicLinks: true,
  124. throwErrorOnBrokenSymbolicLink: true,
  125. fs: {
  126. readdir: readdir,
  127. stat: stat
  128. }
  129. });
  130. provider.readdirWithFileTypes(ROOT_PATH, settings, (error, entries) => {
  131. assert.strictEqual(error.message, 'error');
  132. assert.strictEqual(entries, undefined);
  133. done();
  134. });
  135. });
  136. });
  137. describe('.readdir', () => {
  138. it('should return entries', (done) => {
  139. const stats = new fs_macchiato_1.Stats();
  140. const readdir = sinon.stub();
  141. const lstat = sinon.stub();
  142. readdir.yields(null, [FIRST_FILE_PATH]);
  143. lstat.yields(null, stats);
  144. const settings = new settings_1.default({
  145. fs: {
  146. readdir: readdir,
  147. lstat: lstat
  148. }
  149. });
  150. provider.readdir(ROOT_PATH, settings, (error, entries) => {
  151. assert.strictEqual(error, null);
  152. sinon.assert.match(readdir.args, [[ROOT_PATH, sinon.match.func]]);
  153. sinon.assert.match(lstat.args, [[FIRST_ENTRY_PATH, sinon.match.func]]);
  154. assert.strictEqual(entries[0].name, FIRST_FILE_PATH);
  155. assert.strictEqual(entries[0].path, FIRST_ENTRY_PATH);
  156. assert.strictEqual(entries[0].dirent.name, FIRST_FILE_PATH);
  157. done();
  158. });
  159. });
  160. it('should return entries with `stats` property', (done) => {
  161. const stats = new fs_macchiato_1.Stats();
  162. const readdir = sinon.stub();
  163. const lstat = sinon.stub();
  164. readdir.yields(null, [FIRST_FILE_PATH]);
  165. lstat.yields(null, stats);
  166. const settings = new settings_1.default({
  167. fs: {
  168. readdir: readdir,
  169. lstat: lstat
  170. },
  171. stats: true
  172. });
  173. provider.readdir(ROOT_PATH, settings, (error, entries) => {
  174. assert.strictEqual(error, null);
  175. assert.deepStrictEqual(entries[0].stats, stats);
  176. done();
  177. });
  178. });
  179. });
  180. });