walk.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Disabling the rule because of the 'temp' package API.
  21. // eslint-disable-next-line no-unused-vars
  22. const temp = require('temp').track()
  23. const fs = require('fs')
  24. const path = require('path')
  25. const fswalk = require('@joystream/storage-utils/fs/walk')
  26. function walktest(archive, base, done) {
  27. const 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. it('reports all files in a file system hierarchy', function (done) {
  52. walktest(fs, path.resolve(__dirname, '../data'), done)
  53. })
  54. })