Skip to content

Commit 4e34bfc

Browse files
committed
Use a temp folder for backupservice tests
1 parent 0c48efc commit 4e34bfc

2 files changed

Lines changed: 52 additions & 24 deletions

File tree

src/vs/platform/backup/node/backupService.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@ export class BackupService implements IBackupService {
2727

2828
private workspaceResource: Uri;
2929
private fileContent: IBackupFormat;
30+
private backupHome: string;
31+
private backupWorkspacesPath: string;
3032

3133
constructor(
32-
@IEnvironmentService private environmentService: IEnvironmentService
34+
@IEnvironmentService environmentService: IEnvironmentService
3335
) {
36+
this.backupHome = environmentService.backupHome;
37+
this.backupWorkspacesPath = environmentService.backupWorkspacesPath;
3438
}
3539

3640
public setCurrentWorkspace(resource: Uri): void {
3741
this.workspaceResource = resource;
3842
}
3943

44+
/**
45+
* Due to the Environment service not being initialized when it's needed on the main thread
46+
* side, this is here so that tests can override the paths pulled from it.
47+
*/
48+
public setBackupPathsForTest(backupHome: string, backupWorkspacesPath: string) {
49+
this.backupHome = backupHome;
50+
this.backupWorkspacesPath = backupWorkspacesPath;
51+
}
52+
4053
public getWorkspaceBackupPaths(): TPromise<string[]> {
4154
return this.load().then(() => {
4255
return Object.keys(this.fileContent.folderWorkspaces);
@@ -92,7 +105,7 @@ export class BackupService implements IBackupService {
92105
}
93106

94107
const workspaceHash = crypto.createHash('md5').update(workspace.fsPath).digest('hex');
95-
const untitledDir = path.join(this.environmentService.backupHome, workspaceHash, 'untitled');
108+
const untitledDir = path.join(this.backupHome, workspaceHash, 'untitled');
96109

97110
// Allow sync here as it's only used in workbench initialization's critical path
98111
try {
@@ -110,7 +123,7 @@ export class BackupService implements IBackupService {
110123

111124
const workspaceHash = crypto.createHash('md5').update(this.workspaceResource.fsPath).digest('hex');
112125
const backupName = crypto.createHash('md5').update(resource.fsPath).digest('hex');
113-
const backupPath = path.join(this.environmentService.backupHome, workspaceHash, resource.scheme, backupName);
126+
const backupPath = path.join(this.backupHome, workspaceHash, resource.scheme, backupName);
114127
return Uri.file(backupPath);
115128
}
116129

@@ -149,15 +162,15 @@ export class BackupService implements IBackupService {
149162
}
150163

151164
private load(): TPromise<void> {
152-
return pfs.fileExists(this.environmentService.backupWorkspacesPath).then(exists => {
165+
return pfs.fileExists(this.backupWorkspacesPath).then(exists => {
153166
if (!exists) {
154167
this.fileContent = {
155168
folderWorkspaces: Object.create(null)
156169
};
157170
return TPromise.as(void 0);
158171
}
159172

160-
return pfs.readFile(this.environmentService.backupWorkspacesPath, 'utf8').then(content => {
173+
return pfs.readFile(this.backupWorkspacesPath, 'utf8').then(content => {
161174
try {
162175
return JSON.parse(content.toString());
163176
} catch (ex) {
@@ -174,30 +187,35 @@ export class BackupService implements IBackupService {
174187
}
175188

176189
private loadSync(): void {
177-
try {
178-
this.fileContent = JSON.parse(fs.readFileSync(this.environmentService.backupWorkspacesPath, 'utf8').toString()); // invalid JSON or permission issue can happen here
179-
} catch (error) {
190+
if (fs.existsSync(this.backupWorkspacesPath)) {
191+
try {
192+
this.fileContent = JSON.parse(fs.readFileSync(this.backupWorkspacesPath, 'utf8').toString()); // invalid JSON or permission issue can happen here
193+
} catch (error) {
194+
this.fileContent = Object.create(null);
195+
}
196+
} else {
180197
this.fileContent = Object.create(null);
181198
}
199+
182200
if (!this.fileContent.folderWorkspaces) {
183201
this.fileContent.folderWorkspaces = Object.create(null);
184202
}
185203
}
186204

187205
private save(): TPromise<void> {
188206
const data = JSON.stringify(this.fileContent);
189-
return pfs.mkdirp(this.environmentService.backupHome).then(() => {
190-
return pfs.writeFile(this.environmentService.backupWorkspacesPath, data);
207+
return pfs.mkdirp(this.backupHome).then(() => {
208+
return pfs.writeFile(this.backupWorkspacesPath, data);
191209
});
192210
}
193211

194212
private saveSync(): void {
195213
try {
196214
// The user data directory must exist so only the Backup directory needs to be checked.
197-
if (!fs.existsSync(this.environmentService.backupHome)) {
198-
fs.mkdirSync(this.environmentService.backupHome);
215+
if (!fs.existsSync(this.backupHome)) {
216+
fs.mkdirSync(this.backupHome);
199217
}
200-
fs.writeFileSync(this.environmentService.backupWorkspacesPath, JSON.stringify(this.fileContent));
218+
fs.writeFileSync(this.backupWorkspacesPath, JSON.stringify(this.fileContent));
201219
} catch (ex) {
202220
}
203221
}

src/vs/platform/backup/test/backupService.test.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,34 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment'
1919
import { BackupService } from 'vs/platform/backup/node/backupService';
2020

2121
suite('BackupService', () => {
22+
const parentDir = path.join(os.tmpdir(), 'vsctests', 'service')
23+
const backupHome = path.join(parentDir, 'Backups');
24+
const backupWorkspacesHome = path.join(backupHome, 'workspaces.json');
25+
2226
const fooFile = Uri.file(platform.isWindows ? 'C:\\foo' : '/foo');
2327
const barFile = Uri.file(platform.isWindows ? 'C:\\bar' : '/bar');
2428
const bazFile = Uri.file(platform.isWindows ? 'C:\\baz' : '/baz');
2529

26-
let environmentService: IEnvironmentService;
2730
let backupService: BackupService;
2831

2932
setup(done => {
30-
environmentService = TestEnvironmentService;
33+
const environmentService = TestEnvironmentService;
34+
3135
backupService = new BackupService(environmentService);
36+
backupService.setBackupPathsForTest(backupHome, backupWorkspacesHome);
3237

33-
// Delete any existing backups completely, this in itself is a test to ensure that the
34-
// the backupHome directory is re-created
35-
extfs.del(environmentService.backupHome, os.tmpdir(), done);
38+
// Delete any existing backups completely and then re-create it.
39+
extfs.del(backupHome, os.tmpdir(), () => {
40+
pfs.mkdirp(backupHome).then(() => {
41+
pfs.writeFileAndFlush(backupWorkspacesHome, '').then(() => {
42+
done();
43+
});
44+
});
45+
});
3646
});
3747

3848
teardown(done => {
39-
extfs.del(environmentService.backupHome, os.tmpdir(), done);
49+
extfs.del(backupHome, os.tmpdir(), done);
4050
});
4151

4252
test('pushWorkspaceBackupPathsSync should persist paths to workspaces.json', () => {
@@ -96,7 +106,7 @@ suite('BackupService', () => {
96106
const backupResource = barFile;
97107
const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
98108
const filePathHash = crypto.createHash('md5').update(backupResource.fsPath).digest('hex');
99-
const expectedPath = Uri.file(path.join(environmentService.backupHome, workspaceHash, 'file', filePathHash)).fsPath;
109+
const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'file', filePathHash)).fsPath;
100110
assert.equal(backupService.getBackupResource(backupResource).fsPath, expectedPath);
101111
});
102112

@@ -107,7 +117,7 @@ suite('BackupService', () => {
107117
const backupResource = Uri.from({ scheme: 'untitled' });
108118
const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
109119
const filePathHash = crypto.createHash('md5').update(backupResource.fsPath).digest('hex');
110-
const expectedPath = Uri.file(path.join(environmentService.backupHome, workspaceHash, 'untitled', filePathHash)).fsPath;
120+
const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'untitled', filePathHash)).fsPath;
111121
assert.equal(backupService.getBackupResource(backupResource).fsPath, expectedPath);
112122
});
113123

@@ -118,7 +128,7 @@ suite('BackupService', () => {
118128
const backupResource = barFile;
119129
const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
120130
const filePathHash = crypto.createHash('md5').update(backupResource.fsPath).digest('hex');
121-
const expectedPath = Uri.file(path.join(environmentService.backupHome, workspaceHash, 'file', filePathHash)).fsPath;
131+
const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'file', filePathHash)).fsPath;
122132
assert.equal(backupService.getBackupResource(backupResource).fsPath, expectedPath);
123133
});
124134

@@ -129,7 +139,7 @@ suite('BackupService', () => {
129139
const backupResource = Uri.from({ scheme: 'untitled' });
130140
const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
131141
const filePathHash = crypto.createHash('md5').update(backupResource.fsPath).digest('hex');
132-
const expectedPath = Uri.file(path.join(environmentService.backupHome, workspaceHash, 'untitled', filePathHash)).fsPath;
142+
const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'untitled', filePathHash)).fsPath;
133143
assert.equal(backupService.getBackupResource(backupResource).fsPath, expectedPath);
134144
});
135145

@@ -149,7 +159,7 @@ suite('BackupService', () => {
149159
const workspaceResource = fooFile;
150160
backupService.setCurrentWorkspace(workspaceResource);
151161
const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
152-
const untitledBackupDir = path.join(environmentService.backupHome, workspaceHash, 'untitled');
162+
const untitledBackupDir = path.join(backupHome, workspaceHash, 'untitled');
153163
const untitledBackup1 = path.join(untitledBackupDir, 'bar');
154164
const untitledBackup2 = path.join(untitledBackupDir, 'foo');
155165
pfs.mkdirp(untitledBackupDir).then(() => {

0 commit comments

Comments
 (0)