OutputFileFinder.test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { expect, describe, beforeEach, afterEach, it } from 'vitest'
  2. import fs from 'node:fs'
  3. import os from 'node:os'
  4. import path from 'node:path'
  5. const modulePath = path.join(
  6. import.meta.dirname,
  7. '../../../app/js/OutputFileFinder'
  8. )
  9. function createTree(base, tree) {
  10. fs.mkdirSync(base, { recursive: true })
  11. for (const [name, content] of Object.entries(tree)) {
  12. const fullPath = path.join(base, name)
  13. if (Buffer.isBuffer(content) || typeof content === 'string') {
  14. fs.writeFileSync(fullPath, content)
  15. } else if (content && content.symlink) {
  16. fs.symlinkSync(content.symlink, fullPath)
  17. } else {
  18. createTree(fullPath, content)
  19. }
  20. }
  21. }
  22. describe('OutputFileFinder', function () {
  23. beforeEach(async function (ctx) {
  24. ctx.OutputFileFinder = (await import(modulePath)).default
  25. ctx.directory = fs.mkdtempSync(
  26. path.join(os.tmpdir(), 'output-finder-test-')
  27. )
  28. createTree(ctx.directory, {
  29. resource: {
  30. 'path.tex': 'a source file',
  31. },
  32. 'output.pdf': 'a generated pdf file',
  33. extra: {
  34. 'file.tex': 'a generated tex file',
  35. },
  36. 'sneaky-file': { symlink: '../foo' },
  37. })
  38. })
  39. afterEach(function (ctx) {
  40. fs.rmSync(ctx.directory, { recursive: true })
  41. })
  42. describe('findOutputFiles', function () {
  43. beforeEach(async function (ctx) {
  44. ctx.resource_path = 'resource/path.tex'
  45. ctx.output_paths = ['output.pdf', 'extra/file.tex']
  46. ctx.all_paths = ctx.output_paths.concat([ctx.resource_path])
  47. ctx.resources = [{ path: (ctx.resource_path = 'resource/path.tex') }]
  48. const { outputFiles, allEntries } =
  49. await ctx.OutputFileFinder.promises.findOutputFiles(
  50. ctx.resources,
  51. ctx.directory
  52. )
  53. ctx.outputFiles = outputFiles
  54. ctx.allEntries = allEntries
  55. })
  56. it('should only return the output files, not directories or resource paths', function (ctx) {
  57. expect(ctx.outputFiles).to.have.deep.members([
  58. {
  59. path: 'output.pdf',
  60. type: 'pdf',
  61. },
  62. {
  63. path: 'extra/file.tex',
  64. type: 'tex',
  65. },
  66. ])
  67. expect(ctx.allEntries).to.deep.equal([
  68. 'extra/file.tex',
  69. 'extra/',
  70. 'output.pdf',
  71. 'resource/path.tex',
  72. 'resource/',
  73. 'sneaky-file',
  74. ])
  75. })
  76. })
  77. })