Skip to content

Commit

Permalink
Books/ Unit Tests for error handling (microsoft#6817)
Browse files Browse the repository at this point in the history
* added tests for getBooks and getTableOfContents

* update tests for new getTableOfContents method

* wait for all toc.yml to be found

* add event to signal all toc.yml files read

* add workspae folder parameter back

* remove toc filter

* added timeout logic

* added test for invalid toc.yml

* added tests for invalid toc.yml file format

* added tests for error handling

* update uuid package

* increase timeout time

* change workspacefolder to string
  • Loading branch information
lucyzhang929 authored Aug 23, 2019
1 parent 9274f22 commit 7f32473
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 8 deletions.
31 changes: 23 additions & 8 deletions extensions/notebook/src/book/bookTreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
private _extensionContext: vscode.ExtensionContext;
private _throttleTimer: any;
private _resource: string;
// For testing
private _errorMessage: string;
private _onReadAllTOCFiles: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
private _openAsUntitled: boolean;

Expand Down Expand Up @@ -194,8 +196,12 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
}

private flattenArray(array: any[]): any[] {
return array.reduce((acc, val) => Array.isArray(val.sections) ? acc.concat(val).concat(this.flattenArray(val.sections)) : acc.concat(val), []);
private flattenArray(array: any[], title: string): any[] {
try {
return array.reduce((acc, val) => Array.isArray(val.sections) ? acc.concat(val).concat(this.flattenArray(val.sections, title)) : acc.concat(val), []);
} catch (e) {
throw localize('Invalid toc.yml', 'Error: {0} has an incorrect toc.yml file', title);
}
}

public getBooks(): BookTreeItem[] {
Expand All @@ -208,7 +214,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
let book = new BookTreeItem({
title: config.title,
root: root,
tableOfContents: this.flattenArray(tableOfContents),
tableOfContents: this.flattenArray(tableOfContents, config.title),
page: tableOfContents,
type: BookTreeItemType.Book,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Expanded,
Expand All @@ -220,15 +226,15 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
);
books.push(book);
} catch (e) {
vscode.window.showErrorMessage(localize('openConfigFileError', "Open file {0} failed: {1}",
path.join(root, '_config.yml'),
e instanceof Error ? e.message : e));
let error = e instanceof Error ? e.message : e;
this._errorMessage = error;
vscode.window.showErrorMessage(error);
}
}
return books;
}

private getSections(tableOfContents: any[], sections: any[], root: string): BookTreeItem[] {
public getSections(tableOfContents: any[], sections: any[], root: string): BookTreeItem[] {
let notebooks: BookTreeItem[] = [];
for (let i = 0; i < sections.length; i++) {
if (sections[i].url) {
Expand Down Expand Up @@ -288,7 +294,9 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
);
notebooks.push(markdown);
} else {
vscode.window.showErrorMessage(localize('missingFileError', "Missing file : {0}", sections[i].title));
let error = localize('missingFileError', 'Missing file : {0}', sections[i].title);
this._errorMessage = error;
vscode.window.showErrorMessage(error);
}
}
} else {
Expand Down Expand Up @@ -317,6 +325,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
return Promise.resolve(result);
}

public get errorMessage() {
return this._errorMessage;
}

public get tableOfContentPaths() {
return this._tableOfContentPaths;
}
getUntitledNotebookUri(resource: string): vscode.Uri {
let title = this.findNextUntitledFileName(resource);
let untitledFileName: vscode.Uri = vscode.Uri.parse(`untitled:${title}`);
Expand Down
141 changes: 141 additions & 0 deletions extensions/notebook/src/test/book/book.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,144 @@ describe('BookTreeViewProvider.getChildren', function (): void {
console.log('Successfully removed temporary files.');
});
});

describe('BookTreeViewProvider.getTableOfContentFiles', function (): void {
let rootFolderPath: string;
let tableOfContentsFile: string;
let bookTreeViewProvider: BookTreeViewProvider;
let folder: vscode.WorkspaceFolder;

this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath = path.join(rootFolderPath, '_data');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let tableOfContentsFileIgnore = path.join(rootFolderPath, 'toc.yml');
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.writeFile(tableOfContentsFile, '');
await fs.writeFile(tableOfContentsFileIgnore, '');
let mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext.object);
let tocRead = new Promise((resolve, reject) => bookTreeViewProvider.onReadAllTOCFiles(() => resolve()));
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([tocRead, errorCase.then(() => { throw new Error('Table of Contents were not ready in time'); })]);
});

it('should ignore toc.yml files not in _data folder', function(): void {
bookTreeViewProvider.getTableOfContentFiles([folder.uri.toString()]);
for (let p of bookTreeViewProvider.tableOfContentPaths) {
should(p.toLocaleLowerCase()).equal(tableOfContentsFile.replace(/\\/g, '/').toLocaleLowerCase());
}
});

this.afterAll(async function () {
if (fs.existsSync(rootFolderPath)) {
rimraf.sync(rootFolderPath);
}
});
});


describe('BookTreeViewProvider.getBooks', function (): void {
let rootFolderPath: string;
let configFile: string;
let folder: vscode.WorkspaceFolder;
let bookTreeViewProvider: BookTreeViewProvider;
let mockExtensionContext: TypeMoq.IMock<vscode.ExtensionContext>;

this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath = path.join(rootFolderPath, '_data');
configFile = path.join(rootFolderPath, '_config.yml');
let tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.writeFile(tableOfContentsFile, 'title: Test');
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext.object);
let tocRead = new Promise((resolve, reject) => bookTreeViewProvider.onReadAllTOCFiles(() => resolve()));
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([tocRead, errorCase.then(() => { throw new Error('Table of Contents were not ready in time'); })]);
});

it('should show error message if config.yml file not found', function(): void {
bookTreeViewProvider.getBooks();
should(bookTreeViewProvider.errorMessage.toLocaleLowerCase()).equal(('ENOENT: no such file or directory, open \'' + configFile + '\'').toLocaleLowerCase());
});
it('should show error if toc.yml file format is invalid', async function(): Promise<void> {
await fs.writeFile(configFile, 'title: Test Book');
bookTreeViewProvider.getBooks();
should(bookTreeViewProvider.errorMessage).equal('Error: Test Book has an incorrect toc.yml file');
});

this.afterAll(async function () {
if (fs.existsSync(rootFolderPath)) {
rimraf.sync(rootFolderPath);
}
});
});


describe('BookTreeViewProvider.getSections', function (): void {
let rootFolderPath: string;
let tableOfContentsFile: string;
let bookTreeViewProvider: BookTreeViewProvider;
let folder: vscode.WorkspaceFolder;
let expectedNotebook2: ExpectedBookItem;

this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath = path.join(rootFolderPath, '_data');
let contentFolderPath = path.join(rootFolderPath, 'content');
let configFile = path.join(rootFolderPath, '_config.yml');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let notebook2File = path.join(contentFolderPath, 'notebook2.ipynb');
expectedNotebook2 = {
title: 'Notebook2',
url: '/notebook2',
previousUri: undefined,
nextUri: undefined
};
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.mkdir(contentFolderPath);
await fs.writeFile(configFile, 'title: Test Book');
await fs.writeFile(tableOfContentsFile, '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2');
await fs.writeFile(notebook2File, '');

let mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext.object);
let tocRead = new Promise((resolve, reject) => bookTreeViewProvider.onReadAllTOCFiles(() => resolve()));
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([tocRead, errorCase.then(() => { throw new Error('Table of Contents were not ready in time'); })]);
});

it('should show error if notebook or markdown file is missing', function(): void {
let books = bookTreeViewProvider.getBooks();
let children = bookTreeViewProvider.getSections([], books[0].sections, rootFolderPath);
should(bookTreeViewProvider.errorMessage).equal('Missing file : Notebook1');
// Rest of book should be detected correctly even with a missing file
equalBookItems(children[0], expectedNotebook2);
});

this.afterAll(async function () {
if (fs.existsSync(rootFolderPath)) {
rimraf.sync(rootFolderPath);
}
});
});

0 comments on commit 7f32473

Please sign in to comment.