@@ -282,67 +282,101 @@ suite('FileService', () => {
282282 const environment = TestEnvironmentService ;
283283 const fooResource = uri . file ( '/foo' ) ;
284284 const barResource = uri . file ( '/bar' ) ;
285+ const untitledResource = uri . from ( { scheme : 'untitled' } ) ;
285286
286287 let _service : FileService ;
287288 let backup : TestBackupService ;
288289 let workspaceHash ;
289290 let workspaceBackupRoot ;
290- let fooFileHash ;
291- let barFileHash ;
292291 let fooBackupPath ;
293292 let barBackupPath ;
293+ let untitledBackupPath ;
294294
295295 setup ( ( done ) => {
296296 extfs . del ( TestEnvironmentService . backupHome , os . tmpdir ( ) , done ) ;
297297 backup = new TestBackupService ( ) ;
298298 _service = new FileService ( testDir , { disableWatcher : true } , events , environment , null , backup ) ;
299299 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 ) ;
300+ workspaceBackupRoot = path . join ( environment . backupHome , workspaceHash ) ;
301+ const fooFileHash = crypto . createHash ( 'md5' ) . update ( fooResource . fsPath ) . digest ( 'hex' ) ;
302+ const barFileHash = crypto . createHash ( 'md5' ) . update ( barResource . fsPath ) . digest ( 'hex' ) ;
303+ const untitledFileHash = crypto . createHash ( 'md5' ) . update ( untitledResource . fsPath ) . digest ( 'hex' ) ;
304+ fooBackupPath = path . join ( workspaceBackupRoot , 'file' , fooFileHash ) ;
305+ barBackupPath = path . join ( workspaceBackupRoot , 'file' , barFileHash ) ;
306+ untitledBackupPath = path . join ( workspaceBackupRoot , 'untitled' , untitledFileHash ) ;
305307 } ) ;
306308
307309 teardown ( ( done ) => {
308310 extfs . del ( TestEnvironmentService . backupHome , os . tmpdir ( ) , done ) ;
309311 } ) ;
310312
311- test ( 'backupFile' , function ( done : ( ) => void ) {
313+ test ( 'backupFile - text file ' , function ( done : ( ) => void ) {
312314 _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
313- assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
315+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'file' ) ) . length , 1 ) ;
314316 assert . equal ( fs . existsSync ( fooBackupPath ) , true ) ;
315317 assert . deepEqual ( backup . registeredResources , [ fooResource ] ) ;
316318 assert . equal ( fs . readFileSync ( fooBackupPath ) , 'test' ) ;
317319 done ( ) ;
318320 } ) ;
319321 } ) ;
320322
321- test ( 'discardBackup' , function ( done : ( ) => void ) {
323+ test ( 'backupFile - untitled file' , function ( done : ( ) => void ) {
324+ _service . backupFile ( untitledResource , 'test' ) . then ( ( ) => {
325+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'untitled' ) ) . length , 1 ) ;
326+ assert . equal ( fs . existsSync ( untitledBackupPath ) , true ) ;
327+ // Untitled files are not registered to workspaces.json as they do not have paths
328+ assert . equal ( fs . readFileSync ( untitledBackupPath ) , 'test' ) ;
329+ done ( ) ;
330+ } ) ;
331+ } ) ;
332+
333+ test ( 'discardBackup - text file' , function ( done : ( ) => void ) {
322334 _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
323- assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
335+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'file' ) ) . length , 1 ) ;
324336 _service . discardBackup ( fooResource ) . then ( ( ) => {
325337 assert . equal ( fs . existsSync ( fooBackupPath ) , false ) ;
326- assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 0 ) ;
338+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'file' ) ) . length , 0 ) ;
339+ done ( ) ;
340+ } ) ;
341+ } ) ;
342+ } ) ;
343+
344+ test ( 'discardBackup - untitled file' , function ( done : ( ) => void ) {
345+ _service . backupFile ( untitledResource , 'test' ) . then ( ( ) => {
346+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'untitled' ) ) . length , 1 ) ;
347+ _service . discardBackup ( untitledResource ) . then ( ( ) => {
348+ assert . equal ( fs . existsSync ( untitledBackupPath ) , false ) ;
349+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'untitled' ) ) . length , 0 ) ;
327350 done ( ) ;
328351 } ) ;
329352 } ) ;
330353 } ) ;
331354
332- test ( 'discardBackups' , function ( done : ( ) => void ) {
355+ test ( 'discardBackups - text file ' , function ( done : ( ) => void ) {
333356 _service . backupFile ( fooResource , 'test' ) . then ( ( ) => {
334- assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 1 ) ;
357+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'file' ) ) . length , 1 ) ;
335358 _service . backupFile ( barResource , 'test' ) . then ( ( ) => {
336- assert . equal ( fs . readdirSync ( workspaceBackupRoot ) . length , 2 ) ;
359+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'file' ) ) . length , 2 ) ;
337360 _service . discardBackups ( ) . then ( ( ) => {
338361 assert . equal ( fs . existsSync ( fooBackupPath ) , false ) ;
339362 assert . equal ( fs . existsSync ( barBackupPath ) , false ) ;
340- assert . equal ( fs . existsSync ( workspaceBackupRoot ) , false ) ;
363+ assert . equal ( fs . existsSync ( path . join ( workspaceBackupRoot , 'file' ) ) , false ) ;
341364 done ( ) ;
342365 } ) ;
343366 } ) ;
344367 } ) ;
345368 } ) ;
369+
370+ test ( 'discardBackups - untitled file' , function ( done : ( ) => void ) {
371+ _service . backupFile ( untitledResource , 'test' ) . then ( ( ) => {
372+ assert . equal ( fs . readdirSync ( path . join ( workspaceBackupRoot , 'untitled' ) ) . length , 1 ) ;
373+ _service . discardBackups ( ) . then ( ( ) => {
374+ assert . equal ( fs . existsSync ( untitledBackupPath ) , false ) ;
375+ assert . equal ( fs . existsSync ( path . join ( workspaceBackupRoot , 'untitled' ) ) , false ) ;
376+ done ( ) ;
377+ } ) ;
378+ } ) ;
379+ } ) ;
346380 } ) ;
347381
348382 test ( 'resolveFile' , function ( done : ( ) => void ) {
0 commit comments