walk.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const mocha = require('mocha');
  20. const expect = require('chai').expect;
  21. const temp = require('temp').track();
  22. const fs = require('fs');
  23. const path = require('path');
  24. const fswalk = require('@joystream/storage-utils/fs/walk');
  25. function walktest(archive, base, done)
  26. {
  27. var results = new Map();
  28. fswalk(base, archive, (err, relname, stat, linktarget) => {
  29. expect(err).to.be.null;
  30. if (relname) {
  31. results.set(relname, [stat, linktarget]);
  32. return;
  33. }
  34. // End of data, do testing
  35. const entries = Array.from(results.keys());
  36. expect(entries).to.include('foo');
  37. expect(results.get('foo')[0].isDirectory()).to.be.true;
  38. expect(entries).to.include('bar');
  39. expect(results.get('bar')[0].isFile()).to.be.true;
  40. if (archive === fs) {
  41. expect(entries).to.include('quux');
  42. expect(results.get('quux')[0].isSymbolicLink()).to.be.true;
  43. expect(results.get('quux')[1]).to.equal('foo/baz');
  44. }
  45. expect(entries).to.include('foo/baz');
  46. expect(results.get('foo/baz')[0].isFile()).to.be.true;
  47. done();
  48. });
  49. }
  50. describe('util/fs/walk', function()
  51. {
  52. it('reports all files in a file system hierarchy', function(done)
  53. {
  54. walktest(fs, path.resolve(__dirname, '../data'), done)
  55. });
  56. });