[PnP migration] Remove mock-fs dependency (#33835)

GitOrigin-RevId: ff8df32d85b2ecd2837c9eee6d6d2b3b95285239
This commit is contained in:
Anna Claire Fields
2026-06-05 08:06:54 +00:00
committed by Copybot
parent 3fcd133198
commit 97247b8ea5
10 changed files with 242 additions and 137 deletions
-1
View File
@@ -46,7 +46,6 @@
"mocha": "^11.1.0",
"mocha-junit-reporter": "^2.2.1",
"mocha-multi-reporters": "^1.5.1",
"mock-fs": "^5.1.2",
"node-fetch": "^2.7.0",
"nyc": "^17.1.0",
"sinon": "~9.0.1",
@@ -1,7 +1,8 @@
import { vi, expect, describe, beforeEach, afterEach, it } from 'vitest'
import Path from 'node:path'
import fs from 'node:fs'
import fsPromises from 'node:fs/promises'
import mockFs from 'mock-fs'
import os from 'node:os'
const MODULE_PATH = Path.join(
import.meta.dirname,
@@ -15,20 +16,19 @@ describe('DraftModeManager', () => {
}))
ctx.DraftModeManager = (await import(MODULE_PATH)).default
ctx.filename = '/mock/filename.tex'
ctx.tmpDir = fs.mkdtempSync(Path.join(os.tmpdir(), 'draft-mode-test-'))
ctx.filename = Path.join(ctx.tmpDir, 'filename.tex')
ctx.contents = `\
\\documentclass{article}
\\begin{document}
Hello world
\\end{document}\
`
mockFs({
[ctx.filename]: ctx.contents,
})
fs.writeFileSync(ctx.filename, ctx.contents)
})
afterEach(() => {
mockFs.restore()
afterEach(ctx => {
fs.rmSync(ctx.tmpDir, { recursive: true })
})
describe('injectDraftMode', () => {
@@ -1,6 +1,6 @@
import sinon from 'sinon'
import { expect, describe, beforeEach, afterEach, it } from 'vitest'
import mockFs from 'mock-fs'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
const modulePath = path.join(
@@ -8,30 +8,40 @@ const modulePath = path.join(
'../../../app/js/OutputFileFinder'
)
function createTree(base, tree) {
fs.mkdirSync(base, { recursive: true })
for (const [name, content] of Object.entries(tree)) {
const fullPath = path.join(base, name)
if (Buffer.isBuffer(content) || typeof content === 'string') {
fs.writeFileSync(fullPath, content)
} else if (content && content.symlink) {
fs.symlinkSync(content.symlink, fullPath)
} else {
createTree(fullPath, content)
}
}
}
describe('OutputFileFinder', function () {
beforeEach(async function (ctx) {
ctx.OutputFileFinder = (await import(modulePath)).default
ctx.directory = '/test/dir'
ctx.callback = sinon.stub()
mockFs({
[ctx.directory]: {
resource: {
'path.tex': 'a source file',
},
'output.pdf': 'a generated pdf file',
extra: {
'file.tex': 'a generated tex file',
},
'sneaky-file': mockFs.symlink({
path: '../foo',
}),
ctx.directory = fs.mkdtempSync(
path.join(os.tmpdir(), 'output-finder-test-')
)
createTree(ctx.directory, {
resource: {
'path.tex': 'a source file',
},
'output.pdf': 'a generated pdf file',
extra: {
'file.tex': 'a generated tex file',
},
'sneaky-file': { symlink: '../foo' },
})
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.directory, { recursive: true })
})
describe('findOutputFiles', function () {
-1
View File
@@ -370,7 +370,6 @@
"mocha-each": "^2.0.1",
"mocha-junit-reporter": "^2.2.1",
"mocha-multi-reporters": "^1.5.1",
"mock-fs": "^5.1.2",
"nock": "^13.5.6",
"nvd3": "^1.8.6",
"nyc": "^17.1.0",
@@ -1,6 +1,8 @@
import { vi, expect } from 'vitest'
import sinon from 'sinon'
import mockFs from 'mock-fs'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import mongodb from 'mongodb-legacy'
import Settings from '@overleaf/settings'
@@ -9,6 +11,20 @@ const { ObjectId } = mongodb
const MODULE_PATH =
'../../../../app/src/Features/Uploads/FileSystemImportManager.mjs'
function createTree(base, tree) {
fs.mkdirSync(base, { recursive: true })
for (const [name, content] of Object.entries(tree)) {
const fullPath = path.join(base, name)
if (Buffer.isBuffer(content) || typeof content === 'string') {
fs.writeFileSync(fullPath, content)
} else if (content && typeof content.symlink === 'string') {
fs.symlinkSync(content.symlink, fullPath)
} else {
createTree(fullPath, content)
}
}
}
describe('FileSystemImportManager', function () {
beforeEach(async function (ctx) {
ctx.projectId = new ObjectId()
@@ -48,14 +64,16 @@ describe('FileSystemImportManager', function () {
describe('importDir', function () {
beforeEach(async function (ctx) {
mockFs({
ctx.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'import-test-'))
ctx.importPath = path.join(ctx.tmpDir, 'import-test')
createTree(ctx.tmpDir, {
'import-test': {
'main.tex': 'My thesis',
'link-to-main.tex': mockFs.symlink({ path: 'import-test/main.tex' }),
'.DS_Store': 'Should be ignored',
images: {
'cat.jpg': Buffer.from([1, 2, 3, 4]),
'link-to-main.tex': {
symlink: path.join(ctx.tmpDir, 'import-test', 'main.tex'),
},
'.DS_Store': 'Should be ignored',
images: { 'cat.jpg': Buffer.from([1, 2, 3, 4]) },
'line-endings': {
'unix.txt': 'one\ntwo\nthree',
'mac.txt': 'uno\rdos\rtres',
@@ -67,15 +85,16 @@ describe('FileSystemImportManager', function () {
'latin1.txt': Buffer.from('tétanisant!', 'latin1'),
},
},
symlink: mockFs.symlink({ path: 'import-test' }),
symlink: { symlink: path.join(ctx.tmpDir, 'import-test') },
})
ctx.entries =
await ctx.FileSystemImportManager.promises.importDir('import-test')
ctx.entries = await ctx.FileSystemImportManager.promises.importDir(
ctx.importPath
)
ctx.projectPaths = ctx.entries.map(x => x.projectPath)
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true })
})
it('should import regular docs', function (ctx) {
@@ -98,7 +117,7 @@ describe('FileSystemImportManager', function () {
expect(ctx.entries).to.deep.include({
type: 'file',
projectPath: '/images/cat.jpg',
fsPath: 'import-test/images/cat.jpg',
fsPath: path.join(ctx.importPath, 'images', 'cat.jpg'),
})
})
@@ -142,15 +161,20 @@ describe('FileSystemImportManager', function () {
})
it('should error when the root folder is a symlink', async function (ctx) {
await expect(ctx.FileSystemImportManager.promises.importDir('symlink')).to
.be.rejected
await expect(
ctx.FileSystemImportManager.promises.importDir(
path.join(ctx.tmpDir, 'symlink')
)
).to.be.rejected
})
})
describe('addEntity', function () {
describe('with directory', function () {
beforeEach(async function (ctx) {
mockFs({
ctx.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'addentity-dir-'))
ctx.fsPath = path.join(ctx.tmpDir, 'path', 'to', 'folder')
createTree(ctx.tmpDir, {
path: {
to: {
folder: {
@@ -160,19 +184,18 @@ describe('FileSystemImportManager', function () {
},
},
})
await ctx.FileSystemImportManager.promises.addEntity(
ctx.userId,
ctx.projectId,
ctx.folderId,
'folder',
'path/to/folder',
ctx.fsPath,
false
)
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true })
})
it('should add a folder to the project', function (ctx) {
@@ -197,7 +220,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.newFolderId,
'image.jpg',
'path/to/folder/image.jpg',
path.join(ctx.fsPath, 'image.jpg'),
null,
'upload',
ctx.userId
@@ -206,12 +229,16 @@ describe('FileSystemImportManager', function () {
})
describe('with binary file', function () {
beforeEach(function () {
mockFs({ 'uploaded-file': Buffer.from([1, 2, 3, 4]) })
beforeEach(function (ctx) {
ctx.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'addentity-bin-'))
ctx.fsPath = path.join(ctx.tmpDir, 'uploaded-file')
createTree(ctx.tmpDir, {
'uploaded-file': Buffer.from([1, 2, 3, 4]),
})
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true })
})
describe('with replace set to false', function () {
@@ -221,7 +248,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'image.jpg',
'uploaded-file',
ctx.fsPath,
false
)
})
@@ -231,7 +258,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'image.jpg',
'uploaded-file',
ctx.fsPath,
null,
'upload',
ctx.userId
@@ -246,7 +273,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'image.jpg',
'uploaded-file',
ctx.fsPath,
true
)
})
@@ -256,7 +283,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'image.jpg',
'uploaded-file',
ctx.fsPath,
null,
'upload',
ctx.userId
@@ -271,16 +298,20 @@ describe('FileSystemImportManager', function () {
['Windows', '\r\n'],
]) {
describe(`with text file (${lineEndingDescription} line endings)`, function () {
beforeEach(function () {
mockFs({
beforeEach(function (ctx) {
ctx.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'addentity-txt-'))
ctx.fsPath = path.join(ctx.tmpDir, 'path', 'to', 'uploaded-file')
createTree(ctx.tmpDir, {
path: {
to: { 'uploaded-file': `one${lineEnding}two${lineEnding}three` },
to: {
'uploaded-file': `one${lineEnding}two${lineEnding}three`,
},
},
})
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true })
})
describe('with replace set to false', function () {
@@ -290,7 +321,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'doc.tex',
'path/to/uploaded-file',
ctx.fsPath,
false
)
})
@@ -314,7 +345,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'doc.tex',
'path/to/uploaded-file',
ctx.fsPath,
true
)
})
@@ -334,14 +365,20 @@ describe('FileSystemImportManager', function () {
}
describe('with symlink', function () {
beforeEach(function () {
mockFs({
path: { to: { symlink: mockFs.symlink({ path: '/etc/passwd' }) } },
beforeEach(function (ctx) {
ctx.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'addentity-sym-'))
ctx.fsPath = path.join(ctx.tmpDir, 'path', 'to', 'symlink')
createTree(ctx.tmpDir, {
path: {
to: {
symlink: { symlink: '/etc/passwd' },
},
},
})
})
afterEach(function () {
mockFs.restore()
afterEach(function (ctx) {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true })
})
it('should stop with an error', async function (ctx) {
@@ -351,7 +388,7 @@ describe('FileSystemImportManager', function () {
ctx.projectId,
ctx.folderId,
'main.tex',
'path/to/symlink',
ctx.fsPath,
false
)
).to.be.rejectedWith('path is symlink')