Editing Books (#13535)

* start work on ui

* Move notebooks complete

* Simplify version handling

* fix issues with add section method

* fix issues with the edit experience and add the quick pick for editing

* add version handler for re-writing tocs

* fix book toc manager tests

* add notebook test

* fix localize constant

* normalize paths on windows

* check that a section has sections before attempting to mve files

* Implement method for renaming duplicated files

* moving last notebook from section converts the section into notebook

* Add recovery method restore original state of file system

* Add clean up, for files that are copied instead of renamed

* remove dir complexity

* divide edit book in methods for easier testing and remove promise chain

* Keep structure of toc

* normalize paths on windows

* fix external link

* Add other fields for versions

* fix paths in uri of findSection

* add section to section test

* Add error messages

* address pr comments and add tests

* check that the new path of a notebook is different from the original path before deleting
This commit is contained in:
Barbara Valdez
2021-02-02 20:39:11 -08:00
committed by GitHub
parent 41915bda8d
commit 9ac180d772
13 changed files with 1296 additions and 595 deletions

View File

@@ -19,12 +19,13 @@ import { exists } from '../../common/utils';
import { BookModel } from '../../book/bookModel';
import { BookTrustManager } from '../../book/bookTrustManager';
import { NavigationProviders } from '../../common/constants';
import { readBookError } from '../../common/localizedConstants';
import { openFileError } from '../../common/localizedConstants';
import * as sinon from 'sinon';
import { AppContext } from '../../common/appContext';
export interface IExpectedBookItem {
title: string;
file?: string;
url?: string;
sections?: any[];
external?: boolean;
@@ -34,7 +35,11 @@ export interface IExpectedBookItem {
export function equalBookItems(book: BookTreeItem, expectedBook: IExpectedBookItem, errorMsg?: string): void {
should(book.title).equal(expectedBook.title, `Book titles do not match, expected ${expectedBook?.title} and got ${book?.title}`);
should(path.posix.parse(book.uri)).deepEqual(path.posix.parse(expectedBook.url));
if (expectedBook.file) {
should(path.posix.parse(book.uri)).deepEqual(path.posix.parse(expectedBook.file));
} else {
should(path.posix.parse(book.uri)).deepEqual(path.posix.parse(expectedBook.url));
}
if (expectedBook.previousUri || expectedBook.nextUri) {
let prevUri = book.previousUri ? book.previousUri.toLocaleLowerCase() : undefined;
let expectedPrevUri = expectedBook.previousUri ? expectedBook.previousUri.replace(/\\/g, '/') : undefined;
@@ -75,25 +80,25 @@ describe('BooksTreeViewTests', function () {
expectedNotebook1 = {
// tslint:disable-next-line: quotemark
title: 'Notebook1',
url: '/notebook1',
file: '/notebook1',
previousUri: undefined,
nextUri: notebook2File.toLocaleLowerCase()
};
expectedNotebook2 = {
title: 'Notebook2',
url: '/notebook2',
file: '/notebook2',
previousUri: notebook1File.toLocaleLowerCase(),
nextUri: notebook3File.toLocaleLowerCase()
};
expectedNotebook3 = {
title: 'Notebook3',
url: '/notebook3',
file: '/notebook3',
previousUri: notebook2File.toLocaleLowerCase(),
nextUri: undefined
};
expectedMarkdown = {
title: 'Markdown',
url: '/markdown'
file: '/markdown'
};
expectedExternalLink = {
title: 'GitHub',
@@ -227,7 +232,7 @@ describe('BooksTreeViewTests', function () {
});
it('getParent should return when element is a valid child notebook', async () => {
it.skip('getParent should return when element is a valid child notebook', async () => {
let parent = await bookTreeViewProvider.getParent();
should(parent).be.undefined();
@@ -417,53 +422,54 @@ describe('BooksTreeViewTests', function () {
});
describe('BookTreeViewProvider.getTableOfContentFiles', function() {
describe('BookTreeViewProvider.getTableOfContentFiles', function () {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'tableOfContentsFileIgnore' : path.join(rootFolderPath, 'toc.yml')
folderPaths: {
'dataFolderPath': path.join(rootFolderPath, '_data'),
'contentFolderPath': path.join(rootFolderPath, 'content'),
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile': path.join(rootFolderPath, '_data', 'toc.yml'),
'notebook2File': path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'tableOfContentsFileIgnore': path.join(rootFolderPath, 'toc.yml')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n sections:\n - title: Notebook2\n url: /notebook2\n - title: Notebook3\n url: /notebook3\n- title: Markdown\n url: /markdown\n- title: GitHub\n url: https://github.com/\n external: true'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n url: /notebook1\n sections:\n - title: Notebook2\n url: /notebook2\n - title: Notebook3\n url: /notebook3\n- title: Markdown\n url: /markdown\n- title: GitHub\n url: https://github.com/\n external: true'
}
},
{
it: 'v2',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook2File' : path.join(rootFolderPath,'notebook2.ipynb'),
'tableOfContentsFileIgnore' : path.join(rootFolderPath, '_data', 'toc.yml')
folderPaths: {
'dataFolderPath': path.join(rootFolderPath, '_data'),
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile': path.join(rootFolderPath, '_toc.yml'),
'notebook2File': path.join(rootFolderPath, 'notebook2.ipynb'),
'tableOfContentsFileIgnore': path.join(rootFolderPath, '_data', 'toc.yml')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n sections:\n - title: Notebook2\n file: /notebook2\n - title: Notebook3\n file: /notebook3\n- title: Markdown\n file: /markdown\n- title: GitHub\n url: https://github.com/\n'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n file: /notebook1\n sections:\n - title: Notebook2\n file: /notebook2\n - title: Notebook3\n file: /notebook3\n- title: Markdown\n file: /markdown\n- title: GitHub\n url: https://github.com/\n'
}
}
];
runs.forEach(function (run){
runs.forEach(function (run) {
describe('BookTreeViewProvider.getTableOfContentFiles on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder;
before(async () => {
await fs.mkdir(rootFolderPath);
await fs.mkdir(run.folderPaths.dataFolderPath);
if(run.it === 'v1') {
if (run.it === 'v1') {
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.tableOfContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.tableOfContentsFileIgnore, '');
await fs.writeFile(run.folderPaths.notebook2File, '');
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
const mockExtensionContext = new MockExtensionContext();
folder = {
@@ -474,16 +480,17 @@ describe('BooksTreeViewTests', function () {
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
await bookTreeViewProvider.openBook(rootFolderPath, undefined, false, false);
});
if(run.it === 'v1') {
if (run.it === 'v1') {
it('should ignore toc.yml files not in _data folder', async () => {
await bookTreeViewProvider.currentBook.readBookStructure(rootFolderPath);
await bookTreeViewProvider.currentBook.loadTableOfContentFiles();
let path = bookTreeViewProvider.currentBook.tableOfContentsPath;
should(vscode.Uri.file(path).fsPath).equal(vscode.Uri.file(run.folderPaths.tableOfContentsFile).fsPath);
});
} else if (run.it === 'v2'){
} else if (run.it === 'v2') {
it('should ignore toc.yml files not under the root book folder', async () => {
await bookTreeViewProvider.currentBook.readBookStructure(rootFolderPath);
await bookTreeViewProvider.currentBook.loadTableOfContentFiles();
@@ -501,46 +508,47 @@ describe('BooksTreeViewTests', function () {
});
});
describe('BookTreeViewProvider.getBooks', function() {
describe('BookTreeViewProvider.getBooks', function () {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
folderPaths: {
'dataFolderPath': path.join(rootFolderPath, '_data'),
'contentFolderPath': path.join(rootFolderPath, 'content'),
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_data', 'toc.yml'),
'notebook2File': path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
}, {
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'notebook2.ipynb'),
folderPaths: {
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_toc.yml'),
'notebook2File': path.join(rootFolderPath, 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
runs.forEach(function (run) {
describe('BookTreeViewProvider.getBooks on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder;
before(async () => {
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
if (run.it === 'v1') {
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.config);
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
@@ -553,14 +561,22 @@ describe('BooksTreeViewTests', function () {
});
it('should show error message if config.yml file not found', async () => {
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `ENOENT: no such file or directory, open '${run.folderPaths.configFile}'`));
try {
await bookTreeViewProvider.openBook(rootFolderPath, undefined, false, false);
} catch (error) {
should(error).equal(openFileError(bookTreeViewProvider.currentBook.bookPath, `ENOENT: no such file or directory, open '${run.folderPaths.configFile}'`));
}
});
it('should show error if toc.yml file format is invalid', async function (): Promise<void> {
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `Invalid toc file`));
try {
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await bookTreeViewProvider.openBook(rootFolderPath, undefined, false, false);
} catch (error) {
should(error).equal(openFileError(bookTreeViewProvider.currentBook.bookPath, `Invalid toc file`));
}
});
after(async function (): Promise<void> {
@@ -568,125 +584,127 @@ describe('BooksTreeViewTests', function () {
await promisify(rimraf)(rootFolderPath);
}
});
});
})});
});
})
});
describe('BookTreeViewProvider.getSections', function() {
describe('BookTreeViewProvider.getSections', function () {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
folderPaths: {
'dataFolderPath': path.join(rootFolderPath, '_data'),
'contentFolderPath': path.join(rootFolderPath, 'content'),
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_data', 'toc.yml'),
'notebook2File': path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
},{
}, {
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_toc.yml'),
'notebook2File' : path.join(rootFolderPath,'notebook2.ipynb'),
folderPaths: {
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_toc.yml'),
'notebook2File': path.join(rootFolderPath, 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
runs.forEach(function (run) {
describe('BookTreeViewProvider.getSections on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder[];
let expectedNotebook2: IExpectedBookItem;
let folder: vscode.WorkspaceFolder[];
let expectedNotebook2: IExpectedBookItem;
before(async () => {
expectedNotebook2 = {
title: 'Notebook2',
url: '/notebook2',
previousUri: undefined,
nextUri: undefined
};
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.notebook2File, '');
before(async () => {
expectedNotebook2 = {
title: 'Notebook2',
file: '/notebook2',
previousUri: undefined,
nextUri: undefined
};
await fs.mkdir(rootFolderPath);
if (run.it === 'v1') {
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.notebook2File, '');
const mockExtensionContext = new MockExtensionContext();
folder = [{
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
}];
bookTreeViewProvider = new BookTreeViewProvider(folder, mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
const mockExtensionContext = new MockExtensionContext();
folder = [{
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
}];
bookTreeViewProvider = new BookTreeViewProvider(folder, mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should show error if notebook or markdown file is missing', async function (): Promise<void> {
let books: BookTreeItem[] = bookTreeViewProvider.currentBook.bookItems;
let children = await bookTreeViewProvider.currentBook.getSections({ sections: [] }, books[0].sections, rootFolderPath, books[0].book);
should(bookTreeViewProvider.currentBook.errorMessage).equal('Missing file : Notebook1 from '.concat(bookTreeViewProvider.currentBook.bookPath));
// rest of book should be detected correctly even with a missing file
equalBookItems(children[0], expectedNotebook2);
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) await promisify(rimraf)(rootFolderPath);
});
});
})
});
it('should show error if notebook or markdown file is missing', async function (): Promise<void> {
let books: BookTreeItem[] = bookTreeViewProvider.currentBook.bookItems;
let children = await bookTreeViewProvider.currentBook.getSections({ sections: [] }, books[0].sections, rootFolderPath, books[0].book);
should(bookTreeViewProvider.currentBook.errorMessage).equal('Missing file : Notebook1');
// rest of book should be detected correctly even with a missing file
equalBookItems(children[0], expectedNotebook2);
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) await promisify(rimraf)(rootFolderPath);
});
});
})});
describe('BookTreeViewProvider.Commands', function() {
describe('BookTreeViewProvider.Commands', function () {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook1File' : path.join(rootFolderPath, 'content', 'notebook1.ipynb'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'markdownFile' : path.join(rootFolderPath, 'content', 'readme.md')
folderPaths: {
'dataFolderPath': path.join(rootFolderPath, '_data'),
'contentFolderPath': path.join(rootFolderPath, 'content'),
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_data', 'toc.yml'),
'notebook1File': path.join(rootFolderPath, 'content', 'notebook1.ipynb'),
'notebook2File': path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'markdownFile': path.join(rootFolderPath, 'content', 'readme.md')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Home\n url: /readme\n- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Home\n url: /readme\n- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
},
{
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook1File' : path.join(rootFolderPath, 'notebook1.ipynb'),
'notebook2File' : path.join(rootFolderPath, 'notebook2.ipynb'),
'markdownFile' : path.join(rootFolderPath, 'readme.md')
folderPaths: {
'configFile': path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile': path.join(rootFolderPath, '_toc.yml'),
'notebook1File': path.join(rootFolderPath, 'notebook1.ipynb'),
'notebook2File': path.join(rootFolderPath, 'notebook2.ipynb'),
'markdownFile': path.join(rootFolderPath, 'readme.md')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Home\n file: /readme\n- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Home\n file: /readme\n- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
runs.forEach(function (run) {
describe('BookTreeViewProvider.Commands on ' + run.it, function (): void {
before(async () => {
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
if (run.it === 'v1') {
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
@@ -836,53 +854,53 @@ describe('BooksTreeViewTests', function () {
});
});
describe('BookTreeViewProvider.openNotebookFolder', function() {
describe('BookTreeViewProvider.openNotebookFolder', function () {
let rootFolderPath = path.join(os.tmpdir(), `BookFolderTest_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'bookFolderPath' : path.join(rootFolderPath, 'BookTestData'),
'dataFolderPath' : path.join(rootFolderPath, 'BookTestData', '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'BookTestData', 'content'),
'configFile' : path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'BookTestData','_data', 'toc.yml'),
'bookNotebookFile' : path.join(rootFolderPath, 'BookTestData', 'content', 'notebook1.ipynb'),
'notebookFolderPath' : path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile' : path.join(rootFolderPath, 'NotebookTestData','notebook2.ipynb')
folderPaths: {
'bookFolderPath': path.join(rootFolderPath, 'BookTestData'),
'dataFolderPath': path.join(rootFolderPath, 'BookTestData', '_data'),
'contentFolderPath': path.join(rootFolderPath, 'BookTestData', 'content'),
'configFile': path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile': path.join(rootFolderPath, 'BookTestData', '_data', 'toc.yml'),
'bookNotebookFile': path.join(rootFolderPath, 'BookTestData', 'content', 'notebook1.ipynb'),
'notebookFolderPath': path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile': path.join(rootFolderPath, 'NotebookTestData', 'notebook2.ipynb')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1',
'bookTitle' : 'Test Book',
'standaloneNotebookTitle' : 'notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n url: /notebook1',
'bookTitle': 'Test Book',
'standaloneNotebookTitle': 'notebook2'
}
},
{
it: 'v2',
folderPaths : {
'bookFolderPath' : path.join(rootFolderPath, 'BookTestData'),
'configFile' : path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'BookTestData', '_toc.yml'),
'bookNotebookFile' : path.join(rootFolderPath, 'BookTestData','notebook1.ipynb'),
'notebookFolderPath' : path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile' : path.join(rootFolderPath, 'NotebookTestData','notebook2.ipynb')
folderPaths: {
'bookFolderPath': path.join(rootFolderPath, 'BookTestData'),
'configFile': path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile': path.join(rootFolderPath, 'BookTestData', '_toc.yml'),
'bookNotebookFile': path.join(rootFolderPath, 'BookTestData', 'notebook1.ipynb'),
'notebookFolderPath': path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile': path.join(rootFolderPath, 'NotebookTestData', 'notebook2.ipynb')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1',
'bookTitle' : 'Test Book',
'standaloneNotebookTitle' : 'notebook2'
contents: {
'config': 'title: Test Book',
'toc': '- title: Notebook1\n file: /notebook1',
'bookTitle': 'Test Book',
'standaloneNotebookTitle': 'notebook2'
}
}];
runs.forEach(function (run){
}];
runs.forEach(function (run) {
describe('BookTreeViewProvider.openNotebookFolder on ' + run.it, function (): void {
before(async () => {
await fs.mkdir(rootFolderPath);
await fs.mkdir(run.folderPaths.bookFolderPath);
if(run.it === 'v1') {
if (run.it === 'v1') {
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}

View File

@@ -2,17 +2,25 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as path from 'path';
import { BookTocManager, hasSections } from '../../book/bookTocManager';
import { BookTocManager, hasSections, quickPickResults } from '../../book/bookTocManager';
import { BookTreeItem, BookTreeItemFormat, BookTreeItemType } from '../../book/bookTreeItem';
import * as yaml from 'js-yaml';
import * as sinon from 'sinon';
import { IJupyterBookSectionV1, IJupyterBookSectionV2, JupyterBookSection } from '../../contracts/content';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as uuid from 'uuid';
import { exists } from '../../common/utils';
import * as rimraf from 'rimraf';
import { promisify } from 'util';
import { BookModel } from '../../book/bookModel';
import { MockExtensionContext } from '../common/stubs';
import { BookVersion } from '../../book/bookVersionHandler';
import { BookTreeViewProvider } from '../../book/bookTreeView';
import { NavigationProviders } from '../../common/constants';
import * as loc from '../../common/localizedConstants';
export function equalTOC(actualToc: IJupyterBookSectionV2[], expectedToc: IJupyterBookSectionV2[]): boolean {
for (let [i, section] of actualToc.entries()) {
@@ -89,7 +97,7 @@ describe('BookTocManagerTests', function () {
}];
await bookTocManager.createBook(bookFolderPath, root2FolderPath);
should(equalTOC(bookTocManager.tableofContents[2].sections, expectedSection)).be.true;
should(bookTocManager.tableofContents[2].file).be.equal(path.join(subfolder, 'readme'));
should((bookTocManager.tableofContents[2] as IJupyterBookSectionV2).file).be.equal(path.join(subfolder, 'readme'));
});
it('should ignore invalid file extensions', async () => {
@@ -103,267 +111,466 @@ describe('BookTocManagerTests', function () {
});
describe('EditingBooks', () => {
let book: BookTreeItem;
let bookSection: BookTreeItem;
let bookSection2: BookTreeItem;
let notebook: BookTreeItem;
let rootBookFolderPath: string = path.join(os.tmpdir(), uuid.v4(), 'Book');
let rootSectionFolderPath: string = path.join(os.tmpdir(), uuid.v4(), 'BookSection');
let rootSection2FolderPath: string = path.join(os.tmpdir(), uuid.v4(), 'BookSection2');
let notebookFolder: string = path.join(os.tmpdir(), uuid.v4(), 'Notebook');
let bookTocManager: BookTocManager;
let sourceBookModel: BookModel;
let targetBookModel: BookModel;
let targetBook: BookTreeItem;
let sectionC: BookTreeItem;
let sectionA: BookTreeItem;
let sectionB: BookTreeItem;
let notebook: BookTreeItem;
let duplicatedNotebook: BookTreeItem;
let sourceBookFolderPath: string = path.join(os.tmpdir(), uuid.v4(), 'sourceBook');
let targetBookFolderPath: string = path.join(os.tmpdir(), uuid.v4(), 'targetBook');
let duplicatedNotebookPath: string = path.join(os.tmpdir(), uuid.v4(), 'duplicatedNotebook');
let runs = [
{
it: 'using the jupyter-book legacy version < 0.7.0',
version: 'v1',
url: 'file',
book: {
'rootBookFolderPath': rootBookFolderPath,
'bookContentFolderPath': path.join(rootBookFolderPath, 'content', 'sample'),
'bookDataFolderPath': path.join(rootBookFolderPath, '_data'),
'notebook1': path.join(rootBookFolderPath, 'content', 'notebook'),
'notebook2': path.join(rootBookFolderPath, 'content', 'notebook2'),
'tocPath': path.join(rootBookFolderPath, '_data', 'toc.yml')
},
bookSection1: {
'contentPath': path.join(rootSectionFolderPath, 'content', 'sample', 'readme.md'),
'sectionRoot': rootSectionFolderPath,
'sectionName': 'Sample',
'bookContentFolderPath': path.join(rootSectionFolderPath, 'content', 'sample'),
'bookDataFolderPath': path.join(rootSectionFolderPath, '_data'),
'notebook3': path.join(rootSectionFolderPath, 'content', 'sample', 'notebook3'),
'notebook4': path.join(rootSectionFolderPath, 'content', 'sample', 'notebook4'),
'tocPath': path.join(rootSectionFolderPath, '_data', 'toc.yml')
},
bookSection2: {
'contentPath': path.join(rootSection2FolderPath, 'content', 'test', 'readme.md'),
'sectionRoot': rootSection2FolderPath,
'sectionName': 'Test',
'bookContentFolderPath': path.join(rootSection2FolderPath, 'content', 'test'),
'bookDataFolderPath': path.join(rootSection2FolderPath, '_data'),
'notebook5': path.join(rootSection2FolderPath, 'content', 'test', 'notebook5'),
'notebook6': path.join(rootSection2FolderPath, 'content', 'test', 'notebook6'),
'tocPath': path.join(rootSection2FolderPath, '_data', 'toc.yml')
},
notebook: {
'contentPath': path.join(notebookFolder, 'test', 'readme.md')
},
section: [
{
'title': 'Notebook',
'url': path.join(path.sep, 'notebook')
},
{
'title': 'Notebook 2',
'url': path.join(path.sep, 'notebook2')
}
],
section1: [
{
'title': 'Notebook 3',
'url': path.join('sample', 'notebook3')
},
{
'title': 'Notebook 4',
'url': path.join('sample', 'notebook4')
}
],
section2: [
sourceBook: {
'rootBookFolderPath': sourceBookFolderPath,
'bookContentFolderPath': path.join(sourceBookFolderPath, 'content'),
'tocPath': path.join(sourceBookFolderPath, '_data', 'toc.yml'),
'readme': path.join(sourceBookFolderPath, 'content', 'readme.md'),
'toc': [
{
'title': 'Notebook 5',
'url': path.join(path.sep, 'test', 'notebook5')
'title': 'Notebook 1',
'file': path.join(path.sep, 'sectionA', 'notebook1')
},
{
'title': 'Notebook 6',
'url': path.join(path.sep, 'test', 'notebook6')
'title': 'Notebook 2',
'file': path.join(path.sep, 'sectionA', 'notebook2')
}
]
},
sectionA: {
'contentPath': path.join(sourceBookFolderPath, 'content', 'sectionA', 'readme.md'),
'sectionRoot': path.join(sourceBookFolderPath, 'content', 'sectionA'),
'sectionName': 'Section A',
'notebook1': path.join(sourceBookFolderPath, 'content', 'sectionA', 'notebook1.ipynb'),
'notebook2': path.join(sourceBookFolderPath, 'content', 'sectionA', 'notebook2.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 1',
'file': path.join(path.sep, 'sectionA', 'notebook1')
},
{
'title': 'Notebook 2',
'file': path.join(path.sep, 'sectionA', 'notebook2')
}
]
},
sectionB: {
'contentPath': path.join(sourceBookFolderPath, 'content', 'sectionB', 'readme.md'),
'sectionRoot': path.join(sourceBookFolderPath, 'content', 'sectionB'),
'sectionName': 'Section B',
'notebook3': path.join(sourceBookFolderPath, 'content', 'sectionB', 'notebook3.ipynb'),
'notebook4': path.join(sourceBookFolderPath, 'content', 'sectionB', 'notebook4.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 3',
'file': path.join(path.sep, 'sectionB', 'notebook3')
},
{
'title': 'Notebook 4',
'file': path.join(path.sep, 'sectionB', 'notebook4')
}
]
},
notebook5: {
'contentPath': path.join(sourceBookFolderPath, 'content', 'notebook5.ipynb')
},
targetBook: {
'rootBookFolderPath': targetBookFolderPath,
'bookContentFolderPath': path.join(targetBookFolderPath, 'content'),
'tocPath': path.join(targetBookFolderPath, '_data', 'toc.yml'),
'readme': path.join(targetBookFolderPath, 'content', 'readme.md'),
'toc': [
{
'title': 'Welcome page',
'file': path.join(path.sep, 'readme'),
},
{
'title': 'Section C',
'file': path.join(path.sep, 'sectionC', 'readme'),
'sections': [
{
'title': 'Notebook 6',
'file': path.join(path.sep, 'sectionC', 'notebook6')
}
]
}
]
},
sectionC: {
'contentPath': path.join(targetBookFolderPath, 'content', 'sectionC', 'readme.md'),
'sectionRoot': path.join(targetBookFolderPath, 'content', 'sectionC'),
'sectionName': 'Section C',
'notebook6': path.join(targetBookFolderPath, 'content', 'sectionC', 'notebook6.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 6',
'file': path.join(path.sep, 'sectionC', 'notebook6')
}
]
}
}, {
it: 'using jupyter-book versions >= 0.7.0',
it: 'using the jupyter-book legacy version >= 0.7.0',
version: 'v2',
url: 'file',
book: {
'bookContentFolderPath': path.join(rootBookFolderPath, 'sample'),
'rootBookFolderPath': rootBookFolderPath,
'notebook1': path.join(rootBookFolderPath, 'notebook'),
'notebook2': path.join(rootBookFolderPath, 'notebook2'),
'tocPath': path.join(rootBookFolderPath, '_toc.yml')
sourceBook: {
'rootBookFolderPath': sourceBookFolderPath,
'bookContentFolderPath': sourceBookFolderPath,
'tocPath': path.join(sourceBookFolderPath, '_toc.yml'),
'readme': path.join(sourceBookFolderPath, 'readme.md')
},
bookSection1: {
'bookContentFolderPath': path.join(rootSectionFolderPath, 'sample'),
'contentPath': path.join(rootSectionFolderPath, 'sample', 'readme.md'),
'sectionRoot': rootSectionFolderPath,
'sectionName': 'Sample',
'notebook3': path.join(rootSectionFolderPath, 'sample', 'notebook3'),
'notebook4': path.join(rootSectionFolderPath, 'sample', 'notebook4'),
'tocPath': path.join(rootSectionFolderPath, '_toc.yml')
},
bookSection2: {
'bookContentFolderPath': path.join(rootSection2FolderPath, 'test'),
'contentPath': path.join(rootSection2FolderPath, 'test', 'readme.md'),
'sectionRoot': rootSection2FolderPath,
'sectionName': 'Test',
'notebook5': path.join(rootSection2FolderPath, 'test', 'notebook5'),
'notebook6': path.join(rootSection2FolderPath, 'test', 'notebook6'),
'tocPath': path.join(rootSection2FolderPath, '_toc.yml')
},
notebook: {
'contentPath': path.join(notebookFolder, 'test', 'readme.md')
},
section: [
{
'title': 'Notebook',
'file': path.join(path.sep, 'notebook')
},
{
'title': 'Notebook 2',
'file': path.join(path.sep, 'notebook2')
}
],
section1: [
{
'title': 'Notebook 3',
'file': path.join('sample', 'notebook3')
},
{
'title': 'Notebook 4',
'file': path.join('sample', 'notebook4')
}
],
section2: [
sectionA: {
'contentPath': path.join(sourceBookFolderPath, 'sectionA', 'readme.md'),
'sectionRoot': path.join(sourceBookFolderPath, 'sectionA'),
'sectionName': 'Section A',
'notebook1': path.join(sourceBookFolderPath, 'sectionA', 'notebook1.ipynb'),
'notebook2': path.join(sourceBookFolderPath, 'sectionA', 'notebook2.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'test', 'notebook5')
'title': 'Notebook 1',
'file': path.join(path.sep, 'sectionA', 'notebook1')
},
{
'title': 'Notebook 6',
'file': path.join(path.sep, 'test', 'notebook6')
'title': 'Notebook 2',
'file': path.join(path.sep, 'sectionA', 'notebook2')
}
]
},
sectionB: {
'contentPath': path.join(sourceBookFolderPath, 'sectionB', 'readme.md'),
'sectionRoot': path.join(sourceBookFolderPath, 'sectionB'),
'sectionName': 'Section B',
'notebook3': path.join(sourceBookFolderPath, 'sectionB', 'notebook3.ipynb'),
'notebook4': path.join(sourceBookFolderPath, 'sectionB', 'notebook4.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 3',
'file': path.join(path.sep, 'sectionB', 'notebook3')
},
{
'title': 'Notebook 4',
'file': path.join(path.sep, 'sectionB', 'notebook4')
}
]
},
notebook5: {
'contentPath': path.join(sourceBookFolderPath, 'notebook5.ipynb')
},
targetBook: {
'rootBookFolderPath': targetBookFolderPath,
'bookContentFolderPath': targetBookFolderPath,
'tocPath': path.join(targetBookFolderPath, '_toc.yml'),
'readme': path.join(targetBookFolderPath, 'readme.md'),
'toc': [
{
'title': 'Welcome',
'file': path.join(path.sep, 'readme'),
},
{
'title': 'Section C',
'file': path.join(path.sep, 'sectionC', 'readme'),
'sections': [
{
'title': 'Notebook 6',
'file': path.join(path.sep, 'sectionC', 'notebook6')
}
]
}
]
},
sectionC: {
'contentPath': path.join(targetBookFolderPath, 'sectionC', 'readme.md'),
'sectionRoot': path.join(targetBookFolderPath, 'sectionC'),
'sectionName': 'Section C',
'notebook6': path.join(targetBookFolderPath, 'sectionC', 'notebook6.ipynb'),
'sectionFormat': [
{
'title': 'Notebook 6',
'file': path.join(path.sep, 'sectionC', 'notebook6')
}
]
}
}
];
runs.forEach(function (run) {
describe('Editing Books ' + run.it, function (): void {
beforeEach(async () => {
let bookTreeItemFormat1: BookTreeItemFormat = {
contentPath: run.version === 'v1' ? path.join(run.book.rootBookFolderPath, 'content', 'index.md') : path.join(run.book.rootBookFolderPath, 'index.md'),
root: run.book.rootBookFolderPath,
let targetBookTreeItemFormat: BookTreeItemFormat = {
contentPath: run.targetBook.readme,
root: run.targetBook.rootBookFolderPath,
tableOfContents: {
sections: run.section
sections: run.targetBook.toc
},
isUntitled: undefined,
title: undefined,
title: 'Target Book',
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Book,
version: run.version,
page: run.section
page: run.targetBook.toc
};
let bookTreeItemFormat2: BookTreeItemFormat = {
title: run.bookSection1.sectionName,
contentPath: run.bookSection1.contentPath,
root: run.bookSection1.sectionRoot,
let sectionCTreeItemFormat: BookTreeItemFormat = {
title: run.sectionC.sectionName,
contentPath: run.sectionC.contentPath,
root: run.targetBook.rootBookFolderPath,
tableOfContents: {
sections: run.section1
sections: run.sectionC.sectionFormat
},
isUntitled: undefined,
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Book,
type: BookTreeItemType.Markdown,
version: run.version,
page: run.section1
page: run.sectionC.sectionFormat
};
let bookTreeItemFormat3: BookTreeItemFormat = {
title: run.bookSection2.sectionName,
contentPath: run.bookSection2.contentPath,
root: run.bookSection2.sectionRoot,
// section A is from source book
let sectionATreeItemFormat: BookTreeItemFormat = {
title: run.sectionA.sectionName,
contentPath: run.sectionA.contentPath,
root: run.sourceBook.rootBookFolderPath,
tableOfContents: {
sections: run.section2
sections: run.sectionA.sectionFormat
},
isUntitled: undefined,
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Book,
type: BookTreeItemType.Markdown,
version: run.version,
page: run.section2
page: run.sectionA.sectionFormat
};
let bookTreeItemFormat4: BookTreeItemFormat = {
title: run.bookSection2.sectionName,
contentPath: run.notebook.contentPath,
root: run.bookSection2.sectionRoot,
// section B is from source book
let sectionBTreeItemFormat: BookTreeItemFormat = {
title: run.sectionB.sectionName,
contentPath: run.sectionB.contentPath,
root: run.sourceBook.rootBookFolderPath,
tableOfContents: {
sections: undefined
sections: run.sectionB.sectionFormat
},
isUntitled: undefined,
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Markdown,
version: run.version,
page: run.sectionB.sectionFormat
};
// notebook5 is from source book
let notebookTreeItemFormat: BookTreeItemFormat = {
title: '',
contentPath: run.notebook5.contentPath,
root: run.sourceBook.rootBookFolderPath,
tableOfContents: {
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
},
isUntitled: undefined,
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Notebook,
version: run.version,
page: {
sections: undefined
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
}
};
book = new BookTreeItem(bookTreeItemFormat1, undefined);
bookSection = new BookTreeItem(bookTreeItemFormat2, undefined);
bookSection2 = new BookTreeItem(bookTreeItemFormat3, undefined);
notebook = new BookTreeItem(bookTreeItemFormat4, undefined);
bookTocManager = new BookTocManager();
let duplicatedNbTreeItemFormat: BookTreeItemFormat = {
title: 'Duplicated Notebook',
contentPath: path.join(duplicatedNotebookPath, 'notebook5.ipynb'),
root: duplicatedNotebookPath,
tableOfContents: {
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
},
isUntitled: undefined,
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Notebook,
version: run.version,
page: {
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
}
};
bookSection.uri = path.join('sample', 'readme');
bookSection2.uri = path.join('test', 'readme');
targetBook = new BookTreeItem(targetBookTreeItemFormat, undefined);
sectionC = new BookTreeItem(sectionCTreeItemFormat, undefined);
sectionA = new BookTreeItem(sectionATreeItemFormat, undefined);
sectionB = new BookTreeItem(sectionBTreeItemFormat, undefined);
notebook = new BookTreeItem(notebookTreeItemFormat, undefined);
duplicatedNotebook = new BookTreeItem(duplicatedNbTreeItemFormat, undefined);
book.contextValue = 'savedBook';
bookSection.contextValue = 'section';
bookSection2.contextValue = 'section';
sectionC.uri = path.join('sectionC', 'readme');
sectionA.uri = path.join('sectionA', 'readme');
sectionB.uri = path.join('sectionB', 'readme');
targetBook.contextValue = 'savedBook';
sectionA.contextValue = 'section';
sectionB.contextValue = 'section';
sectionC.contextValue = 'section';
notebook.contextValue = 'savedNotebook';
duplicatedNotebook.contextValue = 'savedNotebook';
sectionC.tableOfContentsPath = run.targetBook.tocPath;
sectionA.tableOfContentsPath = run.sourceBook.tocPath;
sectionB.tableOfContentsPath = run.sourceBook.tocPath;
notebook.tableOfContentsPath = run.sourceBook.tocPath;
duplicatedNotebook.tableOfContentsPath = run.sourceBook.tocPath;
sectionA.sections = run.sectionA.sectionFormat;
sectionB.sections = run.sectionB.sectionFormat;
sectionC.sections = run.sectionC.sectionFormat;
notebook.sections = [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
];
duplicatedNotebook.sections = notebook.sections;
await fs.promises.mkdir(run.targetBook.bookContentFolderPath, { recursive: true });
await fs.promises.mkdir(run.sectionA.contentPath, { recursive: true });
await fs.promises.mkdir(run.sectionB.contentPath, { recursive: true });
await fs.promises.mkdir(run.sectionC.contentPath, { recursive: true });
await fs.promises.mkdir(duplicatedNotebookPath, { recursive: true });
await fs.writeFile(run.sectionA.notebook1, '');
await fs.writeFile(run.sectionA.notebook2, '');
await fs.writeFile(run.sectionB.notebook3, '');
await fs.writeFile(run.sectionB.notebook4, '');
await fs.writeFile(run.sectionC.notebook6, '');
await fs.writeFile(run.notebook5.contentPath, '');
await fs.writeFile(duplicatedNotebook.book.contentPath, '');
await fs.writeFile(path.join(run.targetBook.rootBookFolderPath, '_config.yml'), 'title: Target Book');
await fs.writeFile(path.join(run.sourceBook.rootBookFolderPath, '_config.yml'), 'title: Source Book');
await fs.promises.mkdir(run.book.bookContentFolderPath, { recursive: true });
await fs.promises.mkdir(run.bookSection1.bookContentFolderPath, { recursive: true });
await fs.promises.mkdir(run.bookSection2.bookContentFolderPath, { recursive: true });
await fs.promises.mkdir(path.dirname(run.notebook.contentPath), { recursive: true });
if (run.book.bookDataFolderPath && run.bookSection1.bookDataFolderPath && run.bookSection2.bookDataFolderPath) {
await fs.promises.mkdir(run.book.bookDataFolderPath, { recursive: true });
await fs.promises.mkdir(run.bookSection1.bookDataFolderPath, { recursive: true });
await fs.promises.mkdir(run.bookSection2.bookDataFolderPath, { recursive: true });
if (run.version === 'v1') {
await fs.promises.mkdir(path.dirname(run.targetBook.tocPath), { recursive: true });
await fs.promises.mkdir(path.dirname(run.sourceBook.tocPath), { recursive: true });
}
await fs.writeFile(run.book.notebook1, '');
await fs.writeFile(run.book.notebook2, '');
await fs.writeFile(run.bookSection1.notebook3, '');
await fs.writeFile(run.bookSection1.notebook4, '');
await fs.writeFile(run.bookSection2.notebook5, '');
await fs.writeFile(run.bookSection2.notebook6, '');
await fs.writeFile(run.notebook.contentPath, '');
// target book
await fs.writeFile(run.targetBook.tocPath, '- title: Welcome\n file: /readme\n- title: Section C\n file: /sectionC/readme\n sections:\n - title: Notebook6\n file: /sectionC/notebook6');
// source book
await fs.writeFile(run.sourceBook.tocPath, '- title: Notebook 5\n file: /notebook5\n- title: Section A\n file: /sectionA/readme\n sections:\n - title: Notebook1\n file: /sectionA/notebook1\n - title: Notebook2\n file: /sectionA/notebook2');
const mockExtensionContext = new MockExtensionContext();
sourceBookModel = new BookModel(run.sourceBook.rootBookFolderPath, false, false, mockExtensionContext);
targetBookModel = new BookModel(run.targetBook.rootBookFolderPath, false, false, mockExtensionContext);
// create book model mock objects
sinon.stub(sourceBookModel, 'bookItems').value([sectionA]);
sinon.stub(targetBookModel, 'bookItems').value([targetBook]);
});
it('Add section to book', async () => {
await bookTocManager.updateBook(bookSection, book);
const listFiles = await fs.promises.readdir(run.book.bookContentFolderPath);
const tocFile = await fs.promises.readFile(run.book.tocPath, 'utf8');
let toc = yaml.safeLoad(tocFile);
should(JSON.stringify(listFiles)).be.equal(JSON.stringify(['notebook3', 'notebook4']), 'The files of the section should be moved to the books folder');
should(equalSections(toc.sections[2], bookTocManager.newSection)).be.true;
bookTocManager = new BookTocManager(targetBookModel, sourceBookModel);
await bookTocManager.updateBook(sectionA, targetBook, undefined);
const listFiles = await fs.promises.readdir(path.join(run.targetBook.bookContentFolderPath, 'sectionA'));
const listSourceFiles = await fs.promises.readdir(path.join(run.sourceBook.bookContentFolderPath));
should(JSON.stringify(listSourceFiles).includes('sectionA')).be.false('The source book files should not contain the section A files');
should(JSON.stringify(listFiles)).be.equal(JSON.stringify(['notebook1.ipynb', 'notebook2.ipynb', 'readme.md']), 'The files of the section should be moved to the target book folder');
});
it('Add section to section', async () => {
await bookTocManager.updateBook(bookSection, bookSection2);
let listFiles = await fs.promises.readdir(path.join(run.bookSection2.bookContentFolderPath, 'sample'));
const tocFile = await fs.promises.readFile(path.join(run.bookSection2.tocPath), 'utf8');
let toc = yaml.safeLoad(tocFile);
should(JSON.stringify(listFiles)).be.equal(JSON.stringify(['notebook3', 'notebook4']), 'The files of the section should be moved to the books folder');
should(equalSections(toc[1].sections, bookTocManager.newSection)).be.true;
bookTocManager = new BookTocManager(targetBookModel, sourceBookModel);
await bookTocManager.updateBook(sectionB, sectionC, {
'title': 'Notebook 6',
'file': path.join(path.sep, 'sectionC', 'notebook6')
});
const sectionCFiles = await fs.promises.readdir(path.join(run.targetBook.bookContentFolderPath, 'sectionC'));
const sectionBFiles = await fs.promises.readdir(path.join(run.targetBook.bookContentFolderPath, 'sectionB'));
should(JSON.stringify(sectionCFiles)).be.equal(JSON.stringify(['notebook6.ipynb', 'readme.md']), 'sectionB has been moved under target book content directory');
should(JSON.stringify(sectionBFiles)).be.equal(JSON.stringify(['notebook3.ipynb', 'notebook4.ipynb', 'readme.md']), ' Verify that the files on sectionB had been moved to the targetBook');
});
it('Add notebook to book', async () => {
await bookTocManager.updateBook(notebook, book);
const folder = run.version === 'v1' ? path.join(run.book.rootBookFolderPath, 'content') : path.join(run.book.rootBookFolderPath);
let listFiles = await fs.promises.readdir(folder);
const tocFile = await fs.promises.readFile(run.book.tocPath, 'utf8');
let toc = yaml.safeLoad(tocFile);
should(listFiles.findIndex(f => f === 'readme.md')).not.equal(-1);
should(equalSections(toc.sections[2], bookTocManager.newSection)).be.true;
bookTocManager = new BookTocManager(targetBookModel);
await bookTocManager.updateBook(notebook, targetBook);
const listFiles = await fs.promises.readdir(run.targetBook.bookContentFolderPath);
should(JSON.stringify(listFiles).includes('notebook5.ipynb')).be.true('Notebook 5 should be under the target book content folder');
});
it('Add duplicated notebook to book', async () => {
bookTocManager = new BookTocManager(targetBookModel);
await bookTocManager.updateBook(notebook, targetBook);
await bookTocManager.updateBook(duplicatedNotebook, targetBook);
const listFiles = await fs.promises.readdir(run.targetBook.bookContentFolderPath);
if (run.version === BookVersion.v1) {
should(JSON.stringify(listFiles)).be.equal(JSON.stringify(['notebook5 - 2.ipynb', 'notebook5.ipynb', 'sectionC']), 'Should modify the name of the file');
} else {
should(JSON.stringify(listFiles)).be.equal(JSON.stringify(['_config.yml', '_toc.yml', 'notebook5 - 2.ipynb', 'notebook5.ipynb', 'sectionC']), 'Should modify the name of the file');
}
});
it('Recovery method is called after error', async () => {
const mockExtensionContext = new MockExtensionContext();
const recoverySpy = sinon.spy(BookTocManager.prototype, 'recovery');
sinon.stub(BookTocManager.prototype, 'updateBook').throws(new Error('Unexpected error.'));
const bookTreeViewProvider = new BookTreeViewProvider([], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
const results: quickPickResults = {
book: targetBook,
quickPickSection: {
label: loc.labelAddToLevel,
description: undefined
}
}
bookTocManager = new BookTocManager(targetBookModel);
sinon.stub(bookTreeViewProvider, 'getSelectionQuickPick').returns(Promise.resolve(results));
try {
await bookTreeViewProvider.editBook(notebook);
} catch (error) {
should(recoverySpy.calledOnce).be.true('If unexpected error then recovery method is called.');
}
});
it('Clean up folder with files didnt move', async () => {
bookTocManager = new BookTocManager(targetBookModel);
bookTocManager.movedFiles.set(notebook.book.contentPath, 'movedtest');
await fs.writeFile(path.join(run.sourceBook.bookContentFolderPath, 'test.ipynb'), '');
await bookTocManager.cleanUp(path.dirname(notebook.book.contentPath));
const listFiles = await fs.promises.readdir(path.dirname(notebook.book.contentPath));
should(JSON.stringify(listFiles).includes('test.ipynb')).be.true('Notebook test.ipynb should not be removed');
});
it('Clean up folder when there is an empty folder within the modified directory', async () => {
await fs.promises.mkdir(path.join(run.sourceBook.bookContentFolderPath, 'test'));
bookTocManager.modifiedDir.add(path.join(run.sourceBook.bookContentFolderPath, 'test'));
bookTocManager.movedFiles.set(notebook.book.contentPath, 'movedtest');
await bookTocManager.cleanUp(path.dirname(notebook.book.contentPath));
const listFiles = await fs.promises.readdir(run.sourceBook.bookContentFolderPath);
should(JSON.stringify(listFiles).includes('test')).be.true('Empty directories within the moving element directory are not deleted');
});
afterEach(async function (): Promise<void> {
sinon.restore();
if (await exists(sourceBookFolderPath)) {
await promisify(rimraf)(sourceBookFolderPath);
}
if (await exists(targetBookFolderPath)) {
await promisify(rimraf)(targetBookFolderPath);
}
});
});
});