walk.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 expect = require('chai').expect
  20. const temp = require('temp').track()
  21. const fs = require('fs')
  22. const path = require('path')
  23. const fswalk = require('@joystream/storage-utils/fs/walk')
  24. function walktest(archive, base, done) {
  25. const results = new Map()
  26. fswalk(base, archive, (err, relname, stat, linktarget) => {
  27. expect(err).to.be.null
  28. if (relname) {
  29. results.set(relname, [stat, linktarget])
  30. return
  31. }
  32. // End of data, do testing
  33. const entries = Array.from(results.keys())
  34. expect(entries).to.include('foo')
  35. expect(results.get('foo')[0].isDirectory()).to.be.true
  36. expect(entries).to.include('bar')
  37. expect(results.get('bar')[0].isFile()).to.be.true
  38. if (archive === fs) {
  39. expect(entries).to.include('quux')
  40. expect(results.get('quux')[0].isSymbolicLink()).to.be.true
  41. expect(results.get('quux')[1]).to.equal('foo/baz')
  42. }
  43. expect(entries).to.include('foo/baz')
  44. expect(results.get('foo/baz')[0].isFile()).to.be.true
  45. done()
  46. })
  47. }
  48. describe('util/fs/walk', function () {
  49. it('reports all files in a file system hierarchy', function (done) {
  50. walktest(fs, path.resolve(__dirname, '../data'), done)
  51. })
  52. })