OutputFileFinder.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import sinon from 'sinon'
  2. import { expect, describe, beforeEach, afterEach, it } from 'vitest'
  3. import mockFs from 'mock-fs'
  4. import path from 'node:path'
  5. const modulePath = path.join(
  6. import.meta.dirname,
  7. '../../../app/js/OutputFileFinder'
  8. )
  9. describe('OutputFileFinder', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.OutputFileFinder = (await import(modulePath)).default
  12. ctx.directory = '/test/dir'
  13. ctx.callback = sinon.stub()
  14. mockFs({
  15. [ctx.directory]: {
  16. resource: {
  17. 'path.tex': 'a source file',
  18. },
  19. 'output.pdf': 'a generated pdf file',
  20. extra: {
  21. 'file.tex': 'a generated tex file',
  22. },
  23. 'sneaky-file': mockFs.symlink({
  24. path: '../foo',
  25. }),
  26. },
  27. })
  28. })
  29. afterEach(function () {
  30. mockFs.restore()
  31. })
  32. describe('findOutputFiles', function () {
  33. beforeEach(async function (ctx) {
  34. ctx.resource_path = 'resource/path.tex'
  35. ctx.output_paths = ['output.pdf', 'extra/file.tex']
  36. ctx.all_paths = ctx.output_paths.concat([ctx.resource_path])
  37. ctx.resources = [{ path: (ctx.resource_path = 'resource/path.tex') }]
  38. const { outputFiles, allEntries } =
  39. await ctx.OutputFileFinder.promises.findOutputFiles(
  40. ctx.resources,
  41. ctx.directory
  42. )
  43. ctx.outputFiles = outputFiles
  44. ctx.allEntries = allEntries
  45. })
  46. it('should only return the output files, not directories or resource paths', function (ctx) {
  47. expect(ctx.outputFiles).to.have.deep.members([
  48. {
  49. path: 'output.pdf',
  50. type: 'pdf',
  51. },
  52. {
  53. path: 'extra/file.tex',
  54. type: 'tex',
  55. },
  56. ])
  57. expect(ctx.allEntries).to.deep.equal([
  58. 'extra/file.tex',
  59. 'extra/',
  60. 'output.pdf',
  61. 'resource/path.tex',
  62. 'resource/',
  63. 'sneaky-file',
  64. ])
  65. })
  66. })
  67. })