@@ -9,11 +9,14 @@ import fs = require('fs');
99import path = require( 'path' ) ;
1010import os = require( 'os' ) ;
1111import assert = require( 'assert' ) ;
12+ import crypto = require( 'crypto' ) ;
1213
1314import { TPromise } from 'vs/base/common/winjs.base' ;
1415import { FileService , IEncodingOverride } from 'vs/workbench/services/files/node/fileService' ;
1516import { EventType , FileChangesEvent , FileOperationResult , IFileOperationResult } from 'vs/platform/files/common/files' ;
17+ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace' ;
1618import { nfcall } from 'vs/base/common/async' ;
19+ import { TestBackupService , TestEnvironmentService } from 'vs/test/utils/servicesTestUtils' ;
1720import uri from 'vs/base/common/uri' ;
1821import uuid = require( 'vs/base/common/uuid' ) ;
1922import extfs = require( 'vs/base/node/extfs' ) ;
@@ -33,7 +36,7 @@ suite('FileService', () => {
3336
3437 extfs . copy ( sourceDir , testDir , ( ) => {
3538 events = new utils . TestEventService ( ) ;
36- service = new FileService ( testDir , { disableWatcher : true } , events , null , null , null , null ) ;
39+ service = new FileService ( testDir , { disableWatcher : true } , events , null , null , null ) ;
3740 done ( ) ;
3841 } ) ;
3942 } ) ;
@@ -275,6 +278,73 @@ suite('FileService', () => {
275278 } ) ;
276279 } ) ;
277280
281+ suite ( 'backups' , ( ) => {
282+ const environment = TestEnvironmentService ;
283+ const fooResource = uri . file ( '/foo' ) ;
284+ const barResource = uri . file ( '/bar' ) ;
285+
286+ let _service : FileService ;
287+ let backup : TestBackupService ;
288+ let workspaceHash ;
289+ let workspaceBackupRoot ;
290+ let fooFileHash ;
291+ let barFileHash ;
292+ let fooBackupPath ;
293+ let barBackupPath ;
294+
295+ setup ( ( done ) => {
296+ extfs . del ( TestEnvironmentService . backupHome , os . tmpdir ( ) , done ) ;
297+ backup = new TestBackupService ( ) ;
298+ _service = new FileService ( testDir , { disableWatcher : true } , events , environment , null , backup ) ;
299+ workspaceHash = crypto . createHash ( 'md5' ) . update ( testDir ) . digest ( 'hex' ) ;
300+ workspaceBackupRoot = path . join ( environment . backupHome , workspaceHash , 'file' ) ;
301+ fooFileHash = crypto . createHash ( 'md5' ) . update ( fooResource . fsPath ) . digest ( 'hex' ) ;
302+ barFileHash = crypto . createHash ( 'md5' ) . update ( barResource . fsPath ) . digest ( 'hex' ) ;
303+ fooBackupPath = path . join ( workspaceBackupRoot , fooFileHash ) ;
304+ barBackupPath = path . join ( workspaceBackupRoot , barFileHash ) ;
305+ } ) ;
306+
307+ teardown ( ( done ) => {
308+ extfs . del ( TestEnvironmentService . backupHome , os . tmpdir ( ) , done ) ;
309+ } ) ;
310+
311+ test ( 'backupFile' , function ( done : ( ) => void ) {
312+ _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
313+ assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
314+ assert . equal ( fs . existsSync ( fooBackupPath ) , true ) ;
315+ assert . deepEqual ( backup . registeredResources , [ fooResource ] ) ;
316+ assert . equal ( fs . readFileSync ( fooBackupPath ) , 'test' ) ;
317+ done ( ) ;
318+ } ) ;
319+ } ) ;
320+
321+ test ( 'discardBackup' , function ( done : ( ) => void ) {
322+ _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
323+ assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
324+ _service . discardBackup ( fooResource ) . then ( ( ) => {
325+ assert . equal ( fs . existsSync ( fooBackupPath ) , false ) ;
326+ assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 0 ) ;
327+ done ( ) ;
328+ } ) ;
329+ } ) ;
330+ } ) ;
331+
332+ test ( 'discardBackups' , function ( done : ( ) => void ) {
333+ _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
334+ assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
335+ _service . backupFile ( barResource , 'test' ) . then ( ( ) => {
336+ assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 2 ) ;
337+ _service . discardBackups ( ) . then ( ( ) => {
338+ assert . equal ( fs . existsSync ( fooBackupPath ) , false ) ;
339+ assert . equal ( fs . existsSync ( barBackupPath ) , false ) ;
340+ assert . equal ( fs . existsSync ( workspaceBackupRoot ) , false ) ;
341+ done ( ) ;
342+ } ) ;
343+ } ) ;
344+ } ) ;
345+ } ) ;
346+ } ) ;
347+
278348 test ( 'resolveFile' , function ( done : ( ) => void ) {
279349 service . resolveFile ( uri . file ( testDir ) , { resolveTo : [ uri . file ( path . join ( testDir , 'deep' ) ) ] } ) . done ( r => {
280350 assert . equal ( r . children . length , 6 ) ;
@@ -494,7 +564,7 @@ suite('FileService', () => {
494564 encoding : 'windows1252' ,
495565 encodingOverride : encodingOverride ,
496566 disableWatcher : true
497- } , null , null , null , null , null ) ;
567+ } , null , null , null , null ) ;
498568
499569 _service . resolveContent ( uri . file ( path . join ( testDir , 'index.html' ) ) ) . done ( c => {
500570 assert . equal ( c . encoding , 'windows1252' ) ;
@@ -520,7 +590,7 @@ suite('FileService', () => {
520590
521591 let _service = new FileService ( _testDir , {
522592 disableWatcher : true
523- } , null , null , null , null , null ) ;
593+ } , null , null , null , null ) ;
524594
525595 extfs . copy ( _sourceDir , _testDir , ( ) => {
526596 fs . readFile ( resource . fsPath , ( error , data ) => {
0 commit comments