mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-15 18:46:36 -05:00
Merge from vscode e3c4990c67c40213af168300d1cfeb71d680f877 (#16569)
This commit is contained in:
@@ -7,7 +7,7 @@ import * as fs from 'fs';
|
||||
import { createHash } from 'crypto';
|
||||
import { join } from 'vs/base/common/path';
|
||||
import { isLinux } from 'vs/base/common/platform';
|
||||
import { writeFileSync, writeFile, readdir, exists, rimraf, RimRafMode } from 'vs/base/node/pfs';
|
||||
import { writeFileSync, RimRafMode, Promises } from 'vs/base/node/pfs';
|
||||
import { IBackupMainService, IWorkspaceBackupInfo, isWorkspaceBackupInfo } from 'vs/platform/backup/electron-main/backup';
|
||||
import { IBackupWorkspacesFormat, IEmptyWindowBackupInfo } from 'vs/platform/backup/node/backup';
|
||||
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
|
||||
@@ -49,26 +49,28 @@ export class BackupMainService implements IBackupMainService {
|
||||
async initialize(): Promise<void> {
|
||||
let backups: IBackupWorkspacesFormat;
|
||||
try {
|
||||
backups = JSON.parse(await fs.promises.readFile(this.workspacesJsonPath, 'utf8')); // invalid JSON or permission issue can happen here
|
||||
backups = JSON.parse(await Promises.readFile(this.workspacesJsonPath, 'utf8')); // invalid JSON or permission issue can happen here
|
||||
} catch (error) {
|
||||
backups = Object.create(null);
|
||||
}
|
||||
|
||||
// read empty workspaces backups first
|
||||
if (backups.emptyWorkspaceInfos) {
|
||||
this.emptyWindows = await this.validateEmptyWorkspaces(backups.emptyWorkspaceInfos);
|
||||
}
|
||||
// validate empty workspaces backups first
|
||||
this.emptyWindows = await this.validateEmptyWorkspaces(backups.emptyWorkspaceInfos);
|
||||
|
||||
// read workspace backups
|
||||
let rootWorkspaces: IWorkspaceBackupInfo[] = [];
|
||||
try {
|
||||
if (Array.isArray(backups.rootURIWorkspaces)) {
|
||||
rootWorkspaces = backups.rootURIWorkspaces.map(workspace => ({ workspace: { id: workspace.id, configPath: URI.parse(workspace.configURIPath) }, remoteAuthority: workspace.remoteAuthority }));
|
||||
rootWorkspaces = backups.rootURIWorkspaces.map(workspace => ({
|
||||
workspace: { id: workspace.id, configPath: URI.parse(workspace.configURIPath) },
|
||||
remoteAuthority: workspace.remoteAuthority
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore URI parsing exceptions
|
||||
}
|
||||
|
||||
// validate workspace backups
|
||||
this.workspaces = await this.validateWorkspaces(rootWorkspaces);
|
||||
|
||||
// read folder backups
|
||||
@@ -81,6 +83,7 @@ export class BackupMainService implements IBackupMainService {
|
||||
// ignore URI parsing exceptions
|
||||
}
|
||||
|
||||
// validate folder backups
|
||||
this.folders = await this.validateFolders(workspaceFolders);
|
||||
|
||||
// save again in case some workspaces or folders have been removed
|
||||
@@ -230,7 +233,7 @@ export class BackupMainService implements IBackupMainService {
|
||||
|
||||
// If the workspace has no backups, ignore it
|
||||
if (hasBackups) {
|
||||
if (workspace.configPath.scheme !== Schemas.file || await exists(workspace.configPath.fsPath)) {
|
||||
if (workspace.configPath.scheme !== Schemas.file || await Promises.exists(workspace.configPath.fsPath)) {
|
||||
result.push(workspaceInfo);
|
||||
} else {
|
||||
// If the workspace has backups, but the target workspace is missing, convert backups to empty ones
|
||||
@@ -262,7 +265,7 @@ export class BackupMainService implements IBackupMainService {
|
||||
|
||||
// If the folder has no backups, ignore it
|
||||
if (hasBackups) {
|
||||
if (folderURI.scheme !== Schemas.file || await exists(folderURI.fsPath)) {
|
||||
if (folderURI.scheme !== Schemas.file || await Promises.exists(folderURI.fsPath)) {
|
||||
result.push(folderURI);
|
||||
} else {
|
||||
// If the folder has backups, but the target workspace is missing, convert backups to empty ones
|
||||
@@ -309,8 +312,8 @@ export class BackupMainService implements IBackupMainService {
|
||||
|
||||
private async deleteStaleBackup(backupPath: string): Promise<void> {
|
||||
try {
|
||||
if (await exists(backupPath)) {
|
||||
await rimraf(backupPath, RimRafMode.MOVE);
|
||||
if (await Promises.exists(backupPath)) {
|
||||
await Promises.rm(backupPath, RimRafMode.MOVE);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logService.error(`Backup: Could not delete stale backup: ${error.toString()}`);
|
||||
@@ -328,7 +331,7 @@ export class BackupMainService implements IBackupMainService {
|
||||
// Rename backupPath to new empty window backup path
|
||||
const newEmptyWindowBackupPath = this.getBackupPath(newBackupFolder);
|
||||
try {
|
||||
await fs.promises.rename(backupPath, newEmptyWindowBackupPath);
|
||||
await Promises.rename(backupPath, newEmptyWindowBackupPath);
|
||||
} catch (error) {
|
||||
this.logService.error(`Backup: Could not rename backup folder: ${error.toString()}`);
|
||||
return false;
|
||||
@@ -402,11 +405,11 @@ export class BackupMainService implements IBackupMainService {
|
||||
|
||||
private async doHasBackups(backupPath: string): Promise<boolean> {
|
||||
try {
|
||||
const backupSchemas = await readdir(backupPath);
|
||||
const backupSchemas = await Promises.readdir(backupPath);
|
||||
|
||||
for (const backupSchema of backupSchemas) {
|
||||
try {
|
||||
const backupSchemaChildren = await readdir(join(backupPath, backupSchema));
|
||||
const backupSchemaChildren = await Promises.readdir(join(backupPath, backupSchema));
|
||||
if (backupSchemaChildren.length > 0) {
|
||||
return true;
|
||||
}
|
||||
@@ -431,7 +434,7 @@ export class BackupMainService implements IBackupMainService {
|
||||
|
||||
private async save(): Promise<void> {
|
||||
try {
|
||||
await writeFile(this.workspacesJsonPath, JSON.stringify(this.serializeBackups()));
|
||||
await Promises.writeFile(this.workspacesJsonPath, JSON.stringify(this.serializeBackups()));
|
||||
} catch (error) {
|
||||
this.logService.error(`Backup: Could not save workspaces.json: ${error.toString()}`);
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ flakySuite('BackupMainService', () => {
|
||||
|
||||
async function ensureWorkspaceExists(workspace: IWorkspaceIdentifier): Promise<IWorkspaceIdentifier> {
|
||||
if (!fs.existsSync(workspace.configPath.fsPath)) {
|
||||
await pfs.writeFile(workspace.configPath.fsPath, 'Hello');
|
||||
await pfs.Promises.writeFile(workspace.configPath.fsPath, 'Hello');
|
||||
}
|
||||
|
||||
const backupFolder = service.toBackupPath(workspace.id);
|
||||
@@ -79,7 +79,7 @@ flakySuite('BackupMainService', () => {
|
||||
if (!fs.existsSync(backupFolder)) {
|
||||
fs.mkdirSync(backupFolder);
|
||||
fs.mkdirSync(path.join(backupFolder, Schemas.file));
|
||||
await pfs.writeFile(path.join(backupFolder, Schemas.file, 'foo.txt'), 'Hello');
|
||||
await pfs.Promises.writeFile(path.join(backupFolder, Schemas.file, 'foo.txt'), 'Hello');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ flakySuite('BackupMainService', () => {
|
||||
|
||||
environmentService = new EnvironmentMainService(parseArgs(process.argv, OPTIONS), { _serviceBrand: undefined, ...product });
|
||||
|
||||
await fs.promises.mkdir(backupHome, { recursive: true });
|
||||
await pfs.Promises.mkdir(backupHome, { recursive: true });
|
||||
|
||||
configService = new TestConfigurationService();
|
||||
service = new class TestBackupMainService extends BackupMainService {
|
||||
@@ -132,7 +132,7 @@ flakySuite('BackupMainService', () => {
|
||||
});
|
||||
|
||||
teardown(() => {
|
||||
return pfs.rimraf(testDir);
|
||||
return pfs.Promises.rm(testDir);
|
||||
});
|
||||
|
||||
test('service validates backup workspaces on startup and cleans up (folder workspaces)', async function () {
|
||||
@@ -443,10 +443,10 @@ flakySuite('BackupMainService', () => {
|
||||
folderURIWorkspaces: [existingTestFolder1.toString(), existingTestFolder1.toString()],
|
||||
emptyWorkspaceInfos: []
|
||||
};
|
||||
await pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await service.initialize();
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [existingTestFolder1.toString()]);
|
||||
});
|
||||
@@ -460,9 +460,9 @@ flakySuite('BackupMainService', () => {
|
||||
folderURIWorkspaces: [existingTestFolder1.toString(), existingTestFolder1.toString().toLowerCase()],
|
||||
emptyWorkspaceInfos: []
|
||||
};
|
||||
await pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await service.initialize();
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [existingTestFolder1.toString()]);
|
||||
});
|
||||
@@ -481,10 +481,10 @@ flakySuite('BackupMainService', () => {
|
||||
folderURIWorkspaces: [],
|
||||
emptyWorkspaceInfos: []
|
||||
};
|
||||
await pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await service.initialize();
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
assert.strictEqual(json.rootURIWorkspaces.length, platform.isLinux ? 3 : 1);
|
||||
if (platform.isLinux) {
|
||||
@@ -500,7 +500,7 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerFolderBackupSync(fooFile);
|
||||
service.registerFolderBackupSync(barFile);
|
||||
assertEqualUris(service.getFolderBackupPaths(), [fooFile, barFile]);
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [fooFile.toString(), barFile.toString()]);
|
||||
});
|
||||
@@ -515,7 +515,7 @@ flakySuite('BackupMainService', () => {
|
||||
assert.strictEqual(ws1.workspace.id, service.getWorkspaceBackups()[0].workspace.id);
|
||||
assert.strictEqual(ws2.workspace.id, service.getWorkspaceBackups()[1].workspace.id);
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
|
||||
assert.deepStrictEqual(json.rootURIWorkspaces.map(b => b.configURIPath), [fooFile.toString(), barFile.toString()]);
|
||||
@@ -528,7 +528,7 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerFolderBackupSync(URI.file(fooFile.fsPath.toUpperCase()));
|
||||
assertEqualUris(service.getFolderBackupPaths(), [URI.file(fooFile.fsPath.toUpperCase())]);
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [URI.file(fooFile.fsPath.toUpperCase()).toString()]);
|
||||
});
|
||||
@@ -538,7 +538,7 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(upperFooPath));
|
||||
assertEqualUris(service.getWorkspaceBackups().map(b => b.workspace.configPath), [URI.file(upperFooPath)]);
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
|
||||
assert.deepStrictEqual(json.rootURIWorkspaces.map(b => b.configURIPath), [URI.file(upperFooPath).toString()]);
|
||||
});
|
||||
@@ -549,12 +549,12 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerFolderBackupSync(barFile);
|
||||
service.unregisterFolderBackupSync(fooFile);
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [barFile.toString()]);
|
||||
service.unregisterFolderBackupSync(barFile);
|
||||
|
||||
const content = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
|
||||
assert.deepStrictEqual(json2.folderURIWorkspaces, []);
|
||||
});
|
||||
@@ -566,12 +566,12 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerWorkspaceBackupSync(ws2);
|
||||
service.unregisterWorkspaceBackupSync(ws1.workspace);
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
|
||||
assert.deepStrictEqual(json.rootURIWorkspaces.map(r => r.configURIPath), [barFile.toString()]);
|
||||
service.unregisterWorkspaceBackupSync(ws2.workspace);
|
||||
|
||||
const content = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
|
||||
assert.deepStrictEqual(json2.rootURIWorkspaces, []);
|
||||
});
|
||||
@@ -581,12 +581,12 @@ flakySuite('BackupMainService', () => {
|
||||
service.registerEmptyWindowBackupSync('bar');
|
||||
service.unregisterEmptyWindowBackupSync('foo');
|
||||
|
||||
const buffer = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
|
||||
assert.deepStrictEqual(json.emptyWorkspaceInfos, [{ backupFolder: 'bar' }]);
|
||||
service.unregisterEmptyWindowBackupSync('bar');
|
||||
|
||||
const content = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
|
||||
assert.deepStrictEqual(json2.emptyWorkspaceInfos, []);
|
||||
});
|
||||
@@ -596,11 +596,11 @@ flakySuite('BackupMainService', () => {
|
||||
await ensureFolderExists(existingTestFolder1); // make sure backup folder exists, so the folder is not removed on loadSync
|
||||
|
||||
const workspacesJson: IBackupWorkspacesFormat = { rootURIWorkspaces: [], folderURIWorkspaces: [existingTestFolder1.toString()], emptyWorkspaceInfos: [] };
|
||||
await pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
|
||||
await service.initialize();
|
||||
service.unregisterFolderBackupSync(barFile);
|
||||
service.unregisterEmptyWindowBackupSync('test');
|
||||
const content = await fs.promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
|
||||
const json = (<IBackupWorkspacesFormat>JSON.parse(content));
|
||||
assert.deepStrictEqual(json.folderURIWorkspaces, [existingTestFolder1.toString()]);
|
||||
});
|
||||
@@ -670,8 +670,8 @@ flakySuite('BackupMainService', () => {
|
||||
assert.strictEqual(((await service.getDirtyWorkspaces()).length), 0);
|
||||
|
||||
try {
|
||||
await fs.promises.mkdir(path.join(folderBackupPath, Schemas.file), { recursive: true });
|
||||
await fs.promises.mkdir(path.join(workspaceBackupPath, Schemas.untitled), { recursive: true });
|
||||
await pfs.Promises.mkdir(path.join(folderBackupPath, Schemas.file), { recursive: true });
|
||||
await pfs.Promises.mkdir(path.join(workspaceBackupPath, Schemas.untitled), { recursive: true });
|
||||
} catch (error) {
|
||||
// ignore - folder might exist already
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user