-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #7102: missing "out of sync error"
which should be shown if one try to save pending editor changes to a file which was modified in background. Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
- Loading branch information
Showing
7 changed files
with
274 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
// @ts-check | ||
describe('Saveable', function () { | ||
|
||
const { assert } = chai; | ||
|
||
const { EditorManager } = require('@theia/editor/lib/browser/editor-manager'); | ||
const Uri = require('@theia/core/lib/common/uri'); | ||
const { Saveable } = require('@theia/core/lib/browser/saveable'); | ||
const { WorkspaceService } = require('@theia/workspace/lib/browser/workspace-service'); | ||
const { FileSystem, FileStat } = require('@theia/filesystem/lib/common/filesystem'); | ||
const { MonacoEditor } = require('@theia/monaco/lib/browser/monaco-editor'); | ||
|
||
/** @type {import('inversify').Container} */ | ||
const container = window['theia'].container; | ||
const editorManager = container.get(EditorManager); | ||
const workspaceService = container.get(WorkspaceService); | ||
/** @type {import('@theia/filesystem/lib/common/filesystem').FileSystem} */ | ||
const fileSystem = container.get(FileSystem); | ||
|
||
const rootUri = new Uri.default(workspaceService.tryGetRoots()[0].uri); | ||
const fileUri = rootUri.resolve('.test/foo.txt'); | ||
/** @type {import('@theia/editor/lib/browser/editor-widget').EditorWidget} */ | ||
let widget; | ||
/** @type {MonacoEditor} */ | ||
let editor; | ||
/** @type {{ stat: FileStat, content: string }} */ | ||
let state; | ||
|
||
before(async () => { | ||
await Promise.all([ | ||
fileSystem.createFile(fileUri.toString(), { content: 'foo' }), | ||
editorManager.closeAll() | ||
]); | ||
widget = await editorManager.open(fileUri, { mode: 'reveal' }); | ||
editor = MonacoEditor.get(widget); | ||
}); | ||
|
||
after(() => Promise.all([ | ||
fileSystem.delete(fileUri.parent.toString(), { moveToTrash: false }), | ||
(widget.close(), widget = undefined), | ||
editor = undefined, | ||
state = undefined | ||
])); | ||
|
||
const client = fileSystem.getClient(); | ||
const originalShouldOverwrite = client.shouldOverwrite; | ||
|
||
afterEach(() => client.shouldOverwrite = originalShouldOverwrite); | ||
|
||
it('normal save', async () => { | ||
assert.isFalse(Saveable.isDirty(widget), 'should NOT be dirty before change'); | ||
editor.getControl().setValue('bar'); | ||
assert.isTrue(Saveable.isDirty(widget), 'should be dirty before save'); | ||
await Saveable.save(widget); | ||
assert.isFalse(Saveable.isDirty(widget), 'should NOT be dirty after save'); | ||
assert.equal(editor.getControl().getValue(), 'bar', 'model should be updated'); | ||
state = await fileSystem.resolveContent(fileUri.toString()); | ||
assert.equal(state.content, 'bar', 'fs should be updated'); | ||
}); | ||
|
||
it('reject save', async () => { | ||
let outOfSync = false; | ||
client.shouldOverwrite = async () => { | ||
outOfSync = true; | ||
return false; | ||
}; | ||
editor.getControl().setValue('FOO'); | ||
assert.isTrue(Saveable.isDirty(widget), 'should be dirty before save'); | ||
await fileSystem.touchFile(fileUri.toString()); | ||
await Saveable.save(widget); | ||
assert.isTrue(outOfSync, 'file should be out of sync'); | ||
assert.isTrue(Saveable.isDirty(widget), 'should be dirty after rejected save'); | ||
assert.equal(editor.getControl().getValue(), 'FOO', 'model should be updated'); | ||
state = await fileSystem.resolveContent(fileUri.toString()); | ||
assert.equal(state.content, 'bar', 'fs should NOT be updated'); | ||
}); | ||
|
||
it('accept rejected save', async () => { | ||
let outOfSync = false; | ||
client.shouldOverwrite = async () => { | ||
outOfSync = true; | ||
return true; | ||
}; | ||
assert.isTrue(Saveable.isDirty(widget), 'should be dirty before save'); | ||
await Saveable.save(widget); | ||
assert.isTrue(outOfSync, 'file should be out of sync'); | ||
assert.isFalse(Saveable.isDirty(widget), 'should NOT be dirty after save'); | ||
assert.equal(editor.getControl().getValue(), 'FOO', 'model should be updated'); | ||
state = await fileSystem.resolveContent(fileUri.toString()); | ||
assert.equal(state.content, 'FOO', 'fs should be updated'); | ||
}); | ||
|
||
it('accept new save', async () => { | ||
let outOfSync = false; | ||
client.shouldOverwrite = async () => { | ||
outOfSync = true; | ||
return true; | ||
}; | ||
editor.getControl().setValue('BAR'); | ||
assert.isTrue(Saveable.isDirty(widget), 'should be dirty before save'); | ||
await fileSystem.touchFile(fileUri.toString()); | ||
await Saveable.save(widget); | ||
assert.isTrue(outOfSync, 'file should be out of sync'); | ||
assert.isFalse(Saveable.isDirty(widget), 'should NOT be dirty after save'); | ||
assert.equal(editor.getControl().getValue(), 'BAR', 'model should be updated'); | ||
state = await fileSystem.resolveContent(fileUri.toString()); | ||
assert.equal(state.content, 'BAR', 'fs should be updated'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.