From a05be3912efb0209dd85e08acbce64253570f6f3 Mon Sep 17 00:00:00 2001 From: Maddy <12754347+MaddyDev@users.noreply.github.com> Date: Thu, 21 Oct 2021 12:47:01 -0700 Subject: [PATCH] add path.posix while reading relative paths (#17326) * path.posix * add test for nested folders scenario * update message and remove the redundant check --- .../notebook/src/book/bookTocManager.ts | 10 ++-- .../src/test/book/bookTocManager.test.ts | 50 ++++++++++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/extensions/notebook/src/book/bookTocManager.ts b/extensions/notebook/src/book/bookTocManager.ts index 2acabb186f..9415a6a7c6 100644 --- a/extensions/notebook/src/book/bookTocManager.ts +++ b/extensions/notebook/src/book/bookTocManager.ts @@ -89,26 +89,26 @@ export class BookTocManager implements IBookTocManager { let toc: JupyterBookSection[] = []; for (const content of contents) { try { - const contentStat = (await fs.promises.stat(path.join(directory, content))); + const contentStat = (await fs.promises.stat(path.posix.join(directory, content))); const parsedFile = path.parse(content); if (contentStat.isFile() && allowedFileExtensions.includes(parsedFile.ext)) { - let filePath = directory === rootDirectory ? path.posix.join(path.posix.sep, parsedFile.name) : path.posix.join(path.posix.sep, path.relative(rootDirectory, directory), parsedFile.name); + let filePath = directory === rootDirectory ? path.posix.join(path.posix.sep, parsedFile.name) : path.posix.join(path.posix.sep, path.posix.relative(rootDirectory, directory), parsedFile.name); const section: JupyterBookSection = { title: parsedFile.name, file: filePath }; toc.push(section); } else if (contentStat.isDirectory()) { - let files = await fs.promises.readdir(path.join(directory, content)); + let files = await fs.promises.readdir(path.posix.join(directory, content)); let initFile = this.getInitFile(files); - let filePath = directory === rootDirectory ? path.posix.join(path.posix.sep, parsedFile.name, initFile.name) : path.posix.join(path.posix.sep, path.relative(rootDirectory, directory), parsedFile.name, initFile.name); + let filePath = directory === rootDirectory ? path.posix.join(path.posix.sep, parsedFile.name, initFile.name) : path.posix.join(path.posix.sep, path.posix.relative(rootDirectory, directory), parsedFile.name, initFile.name); let section: JupyterBookSection = {}; section = { title: parsedFile.name, file: filePath, expand_sections: true, numbered: false, - sections: await this.createTocFromDir(files, path.join(directory, content), rootDirectory) + sections: await this.createTocFromDir(files, path.posix.join(directory, content), rootDirectory) }; toc.push(section); } diff --git a/extensions/notebook/src/test/book/bookTocManager.test.ts b/extensions/notebook/src/test/book/bookTocManager.test.ts index c7f3472690..3704d49e14 100644 --- a/extensions/notebook/src/test/book/bookTocManager.test.ts +++ b/extensions/notebook/src/test/book/bookTocManager.test.ts @@ -85,6 +85,7 @@ describe('BookTocManagerTests', function () { let rootFolderPath: string; let root2FolderPath: string; const subfolder = 'Subfolder'; + const subfolder2 = 'Subfolder2'; afterEach(function (): void { sinon.restore(); @@ -94,7 +95,7 @@ describe('BookTocManagerTests', function () { rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`); bookFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`); root2FolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`); - notebooks = ['notebook1.ipynb', 'notebook2.ipynb', 'notebook3.ipynb', 'index.md']; + notebooks = ['notebook1.ipynb', 'notebook2.ipynb', 'notebook3.ipynb', 'index.md', 'notebook4.ipynb', 'notebook5.ipynb']; await fs.mkdir(rootFolderPath); await fs.writeFile(path.join(rootFolderPath, notebooks[0]), ''); @@ -104,6 +105,8 @@ describe('BookTocManagerTests', function () { await fs.mkdir(root2FolderPath); await fs.mkdir(path.join(root2FolderPath, subfolder)); + await fs.mkdir(path.join(root2FolderPath, subfolder, subfolder2)); + await fs.writeFile(path.join(root2FolderPath, notebooks[0]), ''); await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[1]), ''); await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[2]), ''); @@ -145,6 +148,51 @@ describe('BookTocManagerTests', function () { should(bookTocManager.tableofContents.length).be.equal(4); should(listFiles.length).be.equal(7); }); + + it ('should create a table of contents with sections if folder contains subfolders', async () => { + await fs.writeFile(path.join(root2FolderPath, subfolder, subfolder2, notebooks[4]), ''); + await fs.writeFile(path.join(root2FolderPath, subfolder, subfolder2, notebooks[5]), ''); + + let bookTocManager: BookTocManager = new BookTocManager(); + await bookTocManager.createBook(bookFolderPath, root2FolderPath); + let listFiles = await fs.promises.readdir(bookFolderPath); + should(bookTocManager.tableofContents.length).be.equal(3); + should(listFiles.length).be.equal(5); + + let expectedSubSections: IJupyterBookSectionV2[] = [{ + title: 'notebook4', + file: path.posix.join(path.posix.sep, subfolder, subfolder2, 'notebook4') + }, + { + title: 'notebook5', + file: path.posix.join(path.posix.sep, subfolder, subfolder2, 'notebook5') + }]; + + let expectedSection: IJupyterBookSectionV2[] = [{ + title: 'index', + file: path.posix.join(path.posix.sep, subfolder, 'index') + }, + { + title: 'notebook2', + file: path.posix.join(path.posix.sep, subfolder, 'notebook2') + }, + { + title: 'notebook3', + file: path.posix.join(path.posix.sep, subfolder, 'notebook3') + }, + { + title: 'Subfolder2', + file: path.posix.join(path.posix.sep, subfolder, subfolder2, 'notebook4'), + sections : expectedSubSections + }]; + + const index = bookTocManager.tableofContents.findIndex(entry => entry.file === path.posix.join(path.posix.sep, subfolder, 'index')); + should(index).not.be.equal(-1, 'Should find a section with the Subfolder entry'); + if (index !== -1) { + let subsection = bookTocManager.tableofContents[index].sections.find(entry => entry.file === path.posix.join(path.posix.sep, subfolder, subfolder2, 'notebook4')); + should(equalSections(subsection, expectedSection[3])).be.true('Should find a subsection with the subfolder2 inside the subfolder'); + } + }); }); describe('EditingBooks', () => {