mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-17 02:51:36 -05:00
add path.posix while reading relative paths (#17326)
* path.posix * add test for nested folders scenario * update message and remove the redundant check
This commit is contained in:
@@ -89,26 +89,26 @@ export class BookTocManager implements IBookTocManager {
|
|||||||
let toc: JupyterBookSection[] = [];
|
let toc: JupyterBookSection[] = [];
|
||||||
for (const content of contents) {
|
for (const content of contents) {
|
||||||
try {
|
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);
|
const parsedFile = path.parse(content);
|
||||||
if (contentStat.isFile() && allowedFileExtensions.includes(parsedFile.ext)) {
|
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 = {
|
const section: JupyterBookSection = {
|
||||||
title: parsedFile.name,
|
title: parsedFile.name,
|
||||||
file: filePath
|
file: filePath
|
||||||
};
|
};
|
||||||
toc.push(section);
|
toc.push(section);
|
||||||
} else if (contentStat.isDirectory()) {
|
} 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 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 = {};
|
let section: JupyterBookSection = {};
|
||||||
section = {
|
section = {
|
||||||
title: parsedFile.name,
|
title: parsedFile.name,
|
||||||
file: filePath,
|
file: filePath,
|
||||||
expand_sections: true,
|
expand_sections: true,
|
||||||
numbered: false,
|
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);
|
toc.push(section);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ describe('BookTocManagerTests', function () {
|
|||||||
let rootFolderPath: string;
|
let rootFolderPath: string;
|
||||||
let root2FolderPath: string;
|
let root2FolderPath: string;
|
||||||
const subfolder = 'Subfolder';
|
const subfolder = 'Subfolder';
|
||||||
|
const subfolder2 = 'Subfolder2';
|
||||||
|
|
||||||
afterEach(function (): void {
|
afterEach(function (): void {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
@@ -94,7 +95,7 @@ describe('BookTocManagerTests', function () {
|
|||||||
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
|
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
|
||||||
bookFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
|
bookFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
|
||||||
root2FolderPath = 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.mkdir(rootFolderPath);
|
||||||
await fs.writeFile(path.join(rootFolderPath, notebooks[0]), '');
|
await fs.writeFile(path.join(rootFolderPath, notebooks[0]), '');
|
||||||
@@ -104,6 +105,8 @@ describe('BookTocManagerTests', function () {
|
|||||||
|
|
||||||
await fs.mkdir(root2FolderPath);
|
await fs.mkdir(root2FolderPath);
|
||||||
await fs.mkdir(path.join(root2FolderPath, subfolder));
|
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, notebooks[0]), '');
|
||||||
await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[1]), '');
|
await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[1]), '');
|
||||||
await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[2]), '');
|
await fs.writeFile(path.join(root2FolderPath, subfolder, notebooks[2]), '');
|
||||||
@@ -145,6 +148,51 @@ describe('BookTocManagerTests', function () {
|
|||||||
should(bookTocManager.tableofContents.length).be.equal(4);
|
should(bookTocManager.tableofContents.length).be.equal(4);
|
||||||
should(listFiles.length).be.equal(7);
|
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', () => {
|
describe('EditingBooks', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user