-
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 handling malformed recentworkspace.json
In case a recentworkspace.json contains any non string element in its recentRoots array, the JSON is not considered valid. Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com> fix test on windows Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com> one more try to fix test Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com> use uri for windows to try to make test pass Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com> ignore non-string array entries Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com> Replace check for undefined with null Signed-off-by: Alexander Flammer <alex.flammer.dev@outlook.com>
- Loading branch information
Showing
2 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/workspace/src/node/default-workspace-server.spec.ts
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,101 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2022 Alexander Flammer 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 | ||
********************************************************************************/ | ||
|
||
import { Container } from '@theia/core/shared/inversify'; | ||
import { MockEnvVariablesServerImpl } from '@theia/core/lib/browser/test/mock-env-variables-server'; | ||
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { FileUri } from '@theia/core/lib/node'; | ||
import { CommonWorkspaceUtils } from '../common'; | ||
import { DefaultWorkspaceServer, WorkspaceCliContribution } from './default-workspace-server'; | ||
import { expect } from 'chai'; | ||
import * as temp from 'temp'; | ||
import * as fs from 'fs'; | ||
|
||
/* eslint-disable no-unused-expressions */ | ||
|
||
describe('DefaultWorkspaceServer', function (): void { | ||
|
||
describe('getRecentWorkspaces()', async () => { | ||
let workspaceServer: DefaultWorkspaceServer; | ||
let tmpConfigDir: URI; | ||
let recentWorkspaceFile: string; | ||
|
||
beforeEach(() => { | ||
// create a temporary directory | ||
const tempDirPath = temp.track().mkdirSync(); | ||
tmpConfigDir = FileUri.create(fs.realpathSync(tempDirPath)); | ||
recentWorkspaceFile = FileUri.fsPath(tmpConfigDir.resolve('recentworkspace.json')); | ||
|
||
// create a container with the necessary bindings for the DefaultWorkspaceServer | ||
const container = new Container(); | ||
container.bind(WorkspaceCliContribution).toSelf().inSingletonScope(); | ||
container.bind(CommonWorkspaceUtils).toSelf().inSingletonScope(); | ||
container.bind(DefaultWorkspaceServer).toSelf().inSingletonScope(); | ||
container.bind(EnvVariablesServer).toConstantValue(new MockEnvVariablesServerImpl(tmpConfigDir)); | ||
|
||
workspaceServer = container.get(DefaultWorkspaceServer); | ||
}); | ||
|
||
it('should return empty list of workspaces if no recent workspaces file is existing', async function (): Promise<void> { | ||
const recent = await workspaceServer.getRecentWorkspaces(); | ||
expect(recent).to.be.empty; | ||
}); | ||
|
||
it('should not return non-existing workspaces from recent workspaces file', async function (): Promise<void> { | ||
fs.writeFileSync(recentWorkspaceFile, JSON.stringify({ | ||
recentRoots: [ | ||
tmpConfigDir.resolve('somethingNotExisting').toString(), | ||
tmpConfigDir.resolve('somethingElseNotExisting').toString() | ||
] | ||
})); | ||
|
||
const recent = await workspaceServer.getRecentWorkspaces(); | ||
|
||
expect(recent).to.be.empty; | ||
}); | ||
|
||
it('should return only existing workspaces from recent workspaces file', async function (): Promise<void> { | ||
fs.writeFileSync(recentWorkspaceFile, JSON.stringify({ | ||
recentRoots: [ | ||
tmpConfigDir.toString(), | ||
tmpConfigDir.resolve('somethingNotExisting').toString() | ||
] | ||
})); | ||
|
||
const recent = await workspaceServer.getRecentWorkspaces(); | ||
|
||
expect(recent).to.have.members([FileUri.fsPath(tmpConfigDir)]); | ||
}); | ||
|
||
it('should ignore non-string array entries but return remaining existing file paths', async function (): Promise<void> { | ||
// previously caused: 'TypeError: Cannot read property 'fsPath' of undefined', see issue #10250 | ||
fs.writeFileSync(recentWorkspaceFile, JSON.stringify({ | ||
recentRoots: [ | ||
[tmpConfigDir.toString()], | ||
{}, | ||
12345678, | ||
undefined, | ||
tmpConfigDir.toString(), | ||
] | ||
})); | ||
|
||
const recent = await workspaceServer.getRecentWorkspaces(); | ||
|
||
expect(recent).to.have.members([FileUri.fsPath(tmpConfigDir)]); | ||
}); | ||
}); | ||
}); |
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