2012-08-28 02:36:36 +04:00
File = require ' file '
fs = require ' fs '
describe ' File ' , ->
[ path , file ] = [ ]
beforeEach ->
path = fs . join ( require . resolve ( ' fixtures ' ) , " atom-file-test.txt " ) # Don't put in /tmp because /tmp symlinks to /private/tmp and screws up the rename test
fs . remove ( path ) if fs . exists ( path )
fs . write ( path , " this is old! " )
file = new File ( path )
2012-11-29 23:26:42 +04:00
file . read ( )
2012-08-28 02:36:36 +04:00
afterEach ->
file . off ( )
fs . remove ( path ) if fs . exists ( path )
describe " when the contents of the file change " , ->
2013-01-04 04:21:07 +04:00
it " triggers ' contents-changed ' event handlers " , ->
2012-08-28 02:36:36 +04:00
changeHandler = null
changeHandler = jasmine . createSpy ( ' changeHandler ' )
2013-01-04 04:21:07 +04:00
file . on ' contents-changed ' , changeHandler
2012-08-28 02:36:36 +04:00
fs . write ( file . getPath ( ) , " this is new! " )
waitsFor " change event " , ->
changeHandler . callCount > 0
runs ->
changeHandler . reset ( )
fs . write ( file . getPath ( ) , " this is newer! " )
waitsFor " second change event " , ->
changeHandler . callCount > 0
describe " when the file is removed " , ->
it " triggers ' remove ' event handlers " , ->
removeHandler = null
removeHandler = jasmine . createSpy ( ' removeHandler ' )
2013-01-04 22:22:17 +04:00
file . on ' removed ' , removeHandler
2012-08-28 02:36:36 +04:00
fs . remove ( file . getPath ( ) )
waitsFor " remove event " , ->
removeHandler . callCount > 0
describe " when a file is moved (via the filesystem) " , ->
newPath = null
beforeEach ->
newPath = fs . join ( fs . directory ( path ) , " atom-file-was-moved-test.txt " )
afterEach ->
if fs . exists ( newPath )
fs . remove ( newPath )
2013-01-04 22:22:17 +04:00
waitsFor " remove event " , (done) -> file . on ' removed ' , done
2012-08-28 02:36:36 +04:00
it " it updates its path " , ->
moveHandler = null
moveHandler = jasmine . createSpy ( ' moveHandler ' )
2013-01-04 22:22:17 +04:00
file . on ' moved ' , moveHandler
2012-08-28 02:36:36 +04:00
fs . move ( path , newPath )
waitsFor " move event " , ->
moveHandler . callCount > 0
runs ->
expect ( file . getPath ( ) ) . toBe newPath
2013-01-04 04:21:07 +04:00
it " maintains ' contents-changed ' events set on previous path " , ->
2012-08-28 02:36:36 +04:00
moveHandler = null
moveHandler = jasmine . createSpy ( ' moveHandler ' )
2013-01-04 22:22:17 +04:00
file . on ' moved ' , moveHandler
2012-08-28 02:36:36 +04:00
changeHandler = null
changeHandler = jasmine . createSpy ( ' changeHandler ' )
2013-01-04 04:21:07 +04:00
file . on ' contents-changed ' , changeHandler
2012-08-28 02:36:36 +04:00
fs . move ( path , newPath )
waitsFor " move event " , ->
moveHandler . callCount > 0
runs ->
expect ( changeHandler ) . not . toHaveBeenCalled ( )
fs . write ( file . getPath ( ) , " this is new! " )
waitsFor " change event " , ->
changeHandler . callCount > 0
2012-11-17 04:13:34 +04:00
describe " when a file is deleted and the recreated within a small amount of time (git sometimes does this) " , ->
it " triggers a contents change event if the contents change " , ->
2012-11-20 00:15:35 +04:00
jasmine . unspy ( File . prototype , ' detectResurrectionAfterDelay ' )
2012-11-17 04:13:34 +04:00
jasmine . unspy ( window , " setTimeout " )
changeHandler = jasmine . createSpy ( " file changed " )
removeHandler = jasmine . createSpy ( " file removed " )
2013-01-04 04:21:07 +04:00
file . on ' contents-changed ' , changeHandler
2013-01-04 22:22:17 +04:00
file . on ' removed ' , removeHandler
2012-11-17 04:13:34 +04:00
2012-11-20 00:15:35 +04:00
expect ( changeHandler ) . not . toHaveBeenCalled ( )
2012-11-17 04:13:34 +04:00
fs . remove ( path )
2012-11-20 00:15:35 +04:00
expect ( changeHandler ) . not . toHaveBeenCalled ( )
waits 20
runs ->
fs . write ( path , " HE HAS RISEN! " )
expect ( changeHandler ) . not . toHaveBeenCalled ( )
waitsFor " resurrection change event " , ->
changeHandler . callCount == 1
2012-11-17 04:13:34 +04:00
runs ->
expect ( removeHandler ) . not . toHaveBeenCalled ( )
fs . write ( path , " Hallelujah! " )
changeHandler . reset ( )
2012-11-20 00:15:35 +04:00
waitsFor " post-resurrection change event " , ->
2012-11-19 22:01:53 +04:00
changeHandler . callCount > 0