mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-06 01:25:38 -05:00
Book Editing: Support adding new sections to a book (#17074)
* add a new section command
This commit is contained in:
@@ -8,7 +8,7 @@ import * as should from 'should';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs-extra';
|
||||
import { AddFileDialog } from '../../dialog/addFileDialog';
|
||||
import { AddTocEntryDialog } from '../../dialog/addTocEntryDialog';
|
||||
import { IBookTocManager } from '../../book/bookTocManager';
|
||||
import { BookTreeItem, BookTreeItemFormat, BookTreeItemType } from '../../book/bookTreeItem';
|
||||
import * as utils from '../../common/utils';
|
||||
@@ -23,7 +23,7 @@ describe('Add File Dialog', function () {
|
||||
|
||||
beforeEach(() => {
|
||||
let mockBookManager = TypeMoq.Mock.ofType<IBookTocManager>();
|
||||
mockBookManager.setup(m => m.addNewFile(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve());
|
||||
mockBookManager.setup(m => m.addNewTocEntry(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve());
|
||||
bookTocManager = mockBookManager.object;
|
||||
|
||||
let mockTreeItem = TypeMoq.Mock.ofType<BookTreeItem>();
|
||||
@@ -39,14 +39,14 @@ describe('Add File Dialog', function () {
|
||||
});
|
||||
|
||||
it('Create dialog', async () => {
|
||||
let fileDialog = new AddFileDialog(bookTocManager, bookTreeItem, fileExtension);
|
||||
let fileDialog = new AddTocEntryDialog(bookTocManager, bookTreeItem, fileExtension);
|
||||
await fileDialog.createDialog();
|
||||
should(fileDialog.dialog).not.be.undefined();
|
||||
should(fileDialog.dialog.message).be.undefined();
|
||||
});
|
||||
|
||||
it('Validate path', async () => {
|
||||
let fileDialog = new AddFileDialog(bookTocManager, bookTreeItem, fileExtension);
|
||||
let fileDialog = new AddTocEntryDialog(bookTocManager, bookTreeItem, fileExtension);
|
||||
await fileDialog.createDialog();
|
||||
|
||||
let tempDir = os.tmpdir();
|
||||
@@ -84,9 +84,9 @@ describe('Add File Dialog', function () {
|
||||
|
||||
// Error case
|
||||
let mockBookManager = TypeMoq.Mock.ofType<IBookTocManager>();
|
||||
mockBookManager.setup(m => m.addNewFile(TypeMoq.It.isAny(), TypeMoq.It.isAny())).throws(new Error('Expected test error.'));
|
||||
mockBookManager.setup(m => m.addNewTocEntry(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).throws(new Error('Expected test error.'));
|
||||
|
||||
let fileDialog = new AddFileDialog(mockBookManager.object, bookTreeItem, fileExtension);
|
||||
let fileDialog = new AddTocEntryDialog(mockBookManager.object, bookTreeItem, fileExtension);
|
||||
await fileDialog.createDialog();
|
||||
|
||||
await should(fileDialog.createFile(testFileName, testTitle)).be.resolvedWith(false);
|
||||
@@ -97,13 +97,13 @@ describe('Add File Dialog', function () {
|
||||
// Success case
|
||||
let testPathDetails: TocEntryPathHandler[] = [];
|
||||
mockBookManager = TypeMoq.Mock.ofType<IBookTocManager>();
|
||||
mockBookManager.setup(m => m.addNewFile(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns((path, item) => { testPathDetails.push(path); return Promise.resolve(); });
|
||||
mockBookManager.setup(m => m.addNewTocEntry(TypeMoq.It.isAny(), TypeMoq.It.isAny(),TypeMoq.It.isAny())).returns((path, item) => { testPathDetails.push(path); return Promise.resolve(); });
|
||||
|
||||
let mockTreeItem = TypeMoq.Mock.ofType<BookTreeItem>();
|
||||
mockTreeItem.setup(i => i.contextValue).returns(() => BookTreeItemType.savedBook);
|
||||
mockTreeItem.setup(i => i.rootContentPath).returns(() => testDir);
|
||||
|
||||
fileDialog = new AddFileDialog(mockBookManager.object, mockTreeItem.object, fileExtension);
|
||||
fileDialog = new AddTocEntryDialog(mockBookManager.object, mockTreeItem.object, fileExtension);
|
||||
await fileDialog.createDialog();
|
||||
|
||||
let createFileResult = await fileDialog.createFile(testFileName, testTitle);
|
||||
@@ -20,6 +20,8 @@ import { BookTreeViewProvider } from '../../book/bookTreeView';
|
||||
import { NavigationProviders } from '../../common/constants';
|
||||
import { BookVersion } from '../../book/bookVersionHandler';
|
||||
import * as yaml from 'js-yaml';
|
||||
import { TocEntryPathHandler } from '../../book/tocEntryPathHandler';
|
||||
import * as utils from '../../common/utils';
|
||||
|
||||
export function equalTOC(actualToc: IJupyterBookSectionV2[], expectedToc: IJupyterBookSectionV2[]): boolean {
|
||||
for (let [i, section] of actualToc.entries()) {
|
||||
@@ -552,6 +554,26 @@ describe('BookTocManagerTests', function () {
|
||||
should(JSON.stringify(listFiles).includes('test')).be.true('Empty directories within the moving element directory are not deleted');
|
||||
});
|
||||
|
||||
it('Add new section', async () => {
|
||||
bookTocManager = new BookTocManager(sourceBookModel);
|
||||
const fileBasename = `addSectionTest-${utils.generateGuid()}`;
|
||||
const sectionTitle = 'Section Test';
|
||||
const testFilePath = path.join(run.sectionA.sectionRoot, fileBasename).concat(utils.FileExtension.Markdown);
|
||||
await fs.writeFile(testFilePath, '');
|
||||
const pathDetails = new TocEntryPathHandler(testFilePath, run.sourceBook.rootBookFolderPath, sectionTitle);
|
||||
await bookTocManager.addNewTocEntry(pathDetails, sectionA, true);
|
||||
let toc: JupyterBookSection[] = yaml.safeLoad((await fs.promises.readFile(run.sourceBook.tocPath)).toString());
|
||||
const sectionAIndex = toc.findIndex(entry => entry.title === sectionA.title);
|
||||
let newSectionIndex = -1;
|
||||
let newSection = undefined;
|
||||
if (sectionAIndex) {
|
||||
newSectionIndex = toc[sectionAIndex].sections?.findIndex(entry => entry.title === sectionTitle);
|
||||
newSection = toc[sectionAIndex].sections[newSectionIndex];
|
||||
}
|
||||
should(newSectionIndex).not.be.equal(-1, 'The new section should exist in the toc file');
|
||||
should(newSection.sections).not.undefined();
|
||||
});
|
||||
|
||||
afterEach(async function (): Promise<void> {
|
||||
sinon.restore();
|
||||
if (await exists(sourceBookFolderPath)) {
|
||||
|
||||
Reference in New Issue
Block a user