|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import * as platform from 'vs/base/common/platform'; |
| 8 | +import * as os from 'os'; |
| 9 | +import * as path from 'vs/base/common/path'; |
| 10 | +import * as pfs from 'vs/base/node/pfs'; |
| 11 | +import { URI } from 'vs/base/common/uri'; |
| 12 | +import { createTextBufferFactory } from 'vs/editor/common/model/textModel'; |
| 13 | +import { getRandomTestPath } from 'vs/base/test/node/testUtils'; |
| 14 | +import { DefaultEndOfLine } from 'vs/editor/common/model'; |
| 15 | +import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; |
| 16 | +import { hashPath } from 'vs/workbench/services/backup/node/backupFileService'; |
| 17 | +import { BackupModelTracker } from 'vs/workbench/contrib/backup/common/backupModelTracker'; |
| 18 | +import { TestTextFileService, workbenchInstantiationService } from 'vs/workbench/test/workbenchTestServices'; |
| 19 | +import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService'; |
| 20 | +import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager'; |
| 21 | +import { BackupRestorer } from 'vs/workbench/contrib/backup/common/backupRestorer'; |
| 22 | +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; |
| 23 | +import { EditorPart } from 'vs/workbench/browser/parts/editor/editorPart'; |
| 24 | +import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'; |
| 25 | +import { EditorService } from 'vs/workbench/services/editor/browser/editorService'; |
| 26 | +import { Registry } from 'vs/platform/registry/common/platform'; |
| 27 | +import { EditorInput } from 'vs/workbench/common/editor'; |
| 28 | +import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; |
| 29 | +import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; |
| 30 | +import { IEditorRegistry, EditorDescriptor, Extensions as EditorExtensions } from 'vs/workbench/browser/editor'; |
| 31 | +import { TextFileEditor } from 'vs/workbench/contrib/files/browser/editors/textFileEditor'; |
| 32 | +import { IBackupFileService } from 'vs/workbench/services/backup/common/backup'; |
| 33 | +import { NodeTestBackupFileService } from 'vs/workbench/services/backup/test/electron-browser/backupFileService.test'; |
| 34 | +import { dispose, IDisposable } from 'vs/base/common/lifecycle'; |
| 35 | +import { Schemas } from 'vs/base/common/network'; |
| 36 | +import { isEqual } from 'vs/base/common/resources'; |
| 37 | + |
| 38 | +const userdataDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backuprestorer'); |
| 39 | +const backupHome = path.join(userdataDir, 'Backups'); |
| 40 | +const workspacesJsonPath = path.join(backupHome, 'workspaces.json'); |
| 41 | + |
| 42 | +const workspaceResource = URI.file(platform.isWindows ? 'c:\\workspace' : '/workspace'); |
| 43 | +const workspaceBackupPath = path.join(backupHome, hashPath(workspaceResource)); |
| 44 | +const fooFile = URI.file(platform.isWindows ? 'c:\\Foo' : '/Foo'); |
| 45 | +const barFile = URI.file(platform.isWindows ? 'c:\\Bar' : '/Bar'); |
| 46 | +const untitledFile1 = URI.from({ scheme: Schemas.untitled, path: 'Untitled-1' }); |
| 47 | +const untitledFile2 = URI.from({ scheme: Schemas.untitled, path: 'Untitled-2' }); |
| 48 | + |
| 49 | +class TestBackupRestorer extends BackupRestorer { |
| 50 | + async doRestoreBackups(): Promise<URI[] | undefined> { |
| 51 | + return super.doRestoreBackups(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class ServiceAccessor { |
| 56 | + constructor( |
| 57 | + @ITextFileService public textFileService: TestTextFileService, |
| 58 | + @IUntitledTextEditorService public untitledTextEditorService: IUntitledTextEditorService |
| 59 | + ) { |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +suite('BackupModelRestorer', () => { |
| 64 | + let accessor: ServiceAccessor; |
| 65 | + |
| 66 | + let disposables: IDisposable[] = []; |
| 67 | + |
| 68 | + setup(async () => { |
| 69 | + disposables.push(Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor( |
| 70 | + EditorDescriptor.create( |
| 71 | + TextFileEditor, |
| 72 | + TextFileEditor.ID, |
| 73 | + 'Text File Editor' |
| 74 | + ), |
| 75 | + [new SyncDescriptor<EditorInput>(FileEditorInput)] |
| 76 | + )); |
| 77 | + |
| 78 | + // Delete any existing backups completely and then re-create it. |
| 79 | + await pfs.rimraf(backupHome, pfs.RimRafMode.MOVE); |
| 80 | + await pfs.mkdirp(backupHome); |
| 81 | + |
| 82 | + return pfs.writeFile(workspacesJsonPath, ''); |
| 83 | + }); |
| 84 | + |
| 85 | + teardown(async () => { |
| 86 | + dispose(disposables); |
| 87 | + disposables = []; |
| 88 | + |
| 89 | + (<TextFileEditorModelManager>accessor.textFileService.models).clear(); |
| 90 | + (<TextFileEditorModelManager>accessor.textFileService.models).dispose(); |
| 91 | + accessor.untitledTextEditorService.revertAll(); |
| 92 | + |
| 93 | + return pfs.rimraf(backupHome, pfs.RimRafMode.MOVE); |
| 94 | + }); |
| 95 | + |
| 96 | + test('Restore backups', async () => { |
| 97 | + const backupFileService = new NodeTestBackupFileService(workspaceBackupPath); |
| 98 | + const instantiationService = workbenchInstantiationService(); |
| 99 | + instantiationService.stub(IBackupFileService, backupFileService); |
| 100 | + |
| 101 | + const part = instantiationService.createInstance(EditorPart); |
| 102 | + part.create(document.createElement('div')); |
| 103 | + part.layout(400, 300); |
| 104 | + |
| 105 | + instantiationService.stub(IEditorGroupsService, part); |
| 106 | + |
| 107 | + const editorService: EditorService = instantiationService.createInstance(EditorService); |
| 108 | + instantiationService.stub(IEditorService, editorService); |
| 109 | + |
| 110 | + accessor = instantiationService.createInstance(ServiceAccessor); |
| 111 | + |
| 112 | + const tracker = instantiationService.createInstance(BackupModelTracker); |
| 113 | + const restorer = instantiationService.createInstance(TestBackupRestorer); |
| 114 | + |
| 115 | + // Backup 2 normal files and 2 untitled file |
| 116 | + await backupFileService.backupResource(untitledFile1, createTextBufferFactory('untitled-1').create(DefaultEndOfLine.LF).createSnapshot(false)); |
| 117 | + await backupFileService.backupResource(untitledFile2, createTextBufferFactory('untitled-2').create(DefaultEndOfLine.LF).createSnapshot(false)); |
| 118 | + await backupFileService.backupResource(fooFile, createTextBufferFactory('fooFile').create(DefaultEndOfLine.LF).createSnapshot(false)); |
| 119 | + await backupFileService.backupResource(barFile, createTextBufferFactory('barFile').create(DefaultEndOfLine.LF).createSnapshot(false)); |
| 120 | + |
| 121 | + // Verify backups restored and opened as dirty |
| 122 | + await restorer.doRestoreBackups(); |
| 123 | + assert.equal(editorService.editors.length, 4); |
| 124 | + assert.ok(editorService.editors.every(editor => editor.isDirty())); |
| 125 | + |
| 126 | + let counter = 0; |
| 127 | + for (const editor of editorService.editors) { |
| 128 | + const resource = editor.getResource(); |
| 129 | + if (isEqual(resource, untitledFile1)) { |
| 130 | + const model = await accessor.untitledTextEditorService.createOrGet(resource).resolve(); |
| 131 | + assert.equal(model.textEditorModel.getValue(), 'untitled-1'); |
| 132 | + counter++; |
| 133 | + } else if (isEqual(resource, untitledFile2)) { |
| 134 | + const model = await accessor.untitledTextEditorService.createOrGet(resource).resolve(); |
| 135 | + assert.equal(model.textEditorModel.getValue(), 'untitled-2'); |
| 136 | + counter++; |
| 137 | + } else if (isEqual(resource, fooFile)) { |
| 138 | + const model = await accessor.textFileService.models.get(resource!)?.load(); |
| 139 | + assert.equal(model?.textEditorModel?.getValue(), 'fooFile'); |
| 140 | + counter++; |
| 141 | + } else { |
| 142 | + const model = await accessor.textFileService.models.get(resource!)?.load(); |
| 143 | + assert.equal(model?.textEditorModel?.getValue(), 'barFile'); |
| 144 | + counter++; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + assert.equal(counter, 4); |
| 149 | + |
| 150 | + part.dispose(); |
| 151 | + tracker.dispose(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments