@@ -780,48 +780,16 @@ suite('Disk File Service', () => {
780780 const toWatch = URI . file ( join ( testDir , 'index-watch1.html' ) ) ;
781781 writeFileSync ( toWatch . fsPath , 'Init' ) ;
782782
783- const watcherDisposable = service . watch ( toWatch ) ;
784-
785- const listenerDisposable = service . onFileChanges ( event => {
786- watcherDisposable . dispose ( ) ;
787- listenerDisposable . dispose ( ) ;
788-
789- try {
790- assert . equal ( event . changes . length , 1 ) ;
791- assert . equal ( event . changes [ 0 ] . type , FileChangeType . UPDATED ) ;
792- assert . equal ( event . changes [ 0 ] . resource . fsPath , toWatch . fsPath ) ;
793-
794- done ( ) ;
795- } catch ( error ) {
796- done ( error ) ;
797- }
798- } ) ;
783+ assertWatch ( toWatch , FileChangeType . UPDATED , toWatch , done ) ;
799784
800- setTimeout ( ( ) => {
801- writeFileSync ( toWatch . fsPath , 'Changes' ) ;
802- } , 50 ) ;
785+ setTimeout ( ( ) => writeFileSync ( toWatch . fsPath , 'Changes' ) , 50 ) ;
803786 } ) ;
804787
805- test ( 'watch - file (support atomic save)' , function ( done ) {
788+ test ( 'watch - file (atomic save)' , function ( done ) {
806789 const toWatch = URI . file ( join ( testDir , 'index-watch2.html' ) ) ;
807790 writeFileSync ( toWatch . fsPath , 'Init' ) ;
808791
809- const watcherDisposable = service . watch ( toWatch ) ;
810-
811- const listenerDisposable = service . onFileChanges ( event => {
812- watcherDisposable . dispose ( ) ;
813- listenerDisposable . dispose ( ) ;
814-
815- try {
816- assert . equal ( event . changes . length , 1 ) ;
817- assert . equal ( event . changes [ 0 ] . type , FileChangeType . UPDATED ) ;
818- assert . equal ( event . changes [ 0 ] . resource . fsPath , toWatch . fsPath ) ;
819-
820- done ( ) ;
821- } catch ( error ) {
822- done ( error ) ;
823- }
824- } ) ;
792+ assertWatch ( toWatch , FileChangeType . UPDATED , toWatch , done ) ;
825793
826794 setTimeout ( ( ) => {
827795 // Simulate atomic save by deleting the file, creating it under different name
@@ -840,26 +808,9 @@ suite('Disk File Service', () => {
840808 const file = URI . file ( join ( watchDir . fsPath , 'index.html' ) ) ;
841809 writeFileSync ( file . fsPath , 'Init' ) ;
842810
843- const listenerDisposable = service . onFileChanges ( event => {
844- watcherDisposable . dispose ( ) ;
845- listenerDisposable . dispose ( ) ;
811+ assertWatch ( watchDir , FileChangeType . UPDATED , file , done ) ;
846812
847- try {
848- assert . equal ( event . changes . length , 1 ) ;
849- assert . equal ( event . changes [ 0 ] . type , FileChangeType . UPDATED ) ;
850- assert . equal ( event . changes [ 0 ] . resource . fsPath , file . fsPath ) ;
851-
852- done ( ) ;
853- } catch ( error ) {
854- done ( error ) ;
855- }
856- } ) ;
857-
858- const watcherDisposable = service . watch ( watchDir ) ;
859-
860- setTimeout ( ( ) => {
861- writeFileSync ( file . fsPath , 'Changes' ) ;
862- } , 50 ) ;
813+ setTimeout ( ( ) => writeFileSync ( file . fsPath , 'Changes' ) , 50 ) ;
863814 } ) ;
864815
865816 test ( 'watch - folder (non recursive) - add file' , done => {
@@ -868,26 +819,9 @@ suite('Disk File Service', () => {
868819
869820 const file = URI . file ( join ( watchDir . fsPath , 'index.html' ) ) ;
870821
871- const listenerDisposable = service . onFileChanges ( event => {
872- watcherDisposable . dispose ( ) ;
873- listenerDisposable . dispose ( ) ;
874-
875- try {
876- assert . equal ( event . changes . length , 1 ) ;
877- assert . equal ( event . changes [ 0 ] . type , FileChangeType . ADDED ) ;
878- assert . equal ( event . changes [ 0 ] . resource . fsPath , file . fsPath ) ;
822+ assertWatch ( watchDir , FileChangeType . ADDED , file , done ) ;
879823
880- done ( ) ;
881- } catch ( error ) {
882- done ( error ) ;
883- }
884- } ) ;
885-
886- const watcherDisposable = service . watch ( watchDir ) ;
887-
888- setTimeout ( ( ) => {
889- writeFileSync ( file . fsPath , 'Changes' ) ;
890- } , 50 ) ;
824+ setTimeout ( ( ) => writeFileSync ( file . fsPath , 'Changes' ) , 50 ) ;
891825 } ) ;
892826
893827 test ( 'watch - folder (non recursive) - delete file' , done => {
@@ -897,26 +831,9 @@ suite('Disk File Service', () => {
897831 const file = URI . file ( join ( watchDir . fsPath , 'index.html' ) ) ;
898832 writeFileSync ( file . fsPath , 'Init' ) ;
899833
900- const listenerDisposable = service . onFileChanges ( event => {
901- watcherDisposable . dispose ( ) ;
902- listenerDisposable . dispose ( ) ;
834+ assertWatch ( watchDir , FileChangeType . DELETED , file , done ) ;
903835
904- try {
905- assert . equal ( event . changes . length , 1 ) ;
906- assert . equal ( event . changes [ 0 ] . type , FileChangeType . DELETED ) ;
907- assert . equal ( event . changes [ 0 ] . resource . fsPath , file . fsPath ) ;
908-
909- done ( ) ;
910- } catch ( error ) {
911- done ( error ) ;
912- }
913- } ) ;
914-
915- const watcherDisposable = service . watch ( watchDir ) ;
916-
917- setTimeout ( ( ) => {
918- unlinkSync ( file . fsPath ) ;
919- } , 50 ) ;
836+ setTimeout ( ( ) => unlinkSync ( file . fsPath ) , 50 ) ;
920837 } ) ;
921838
922839 test ( 'watch - folder (non recursive) - add folder' , done => {
@@ -925,26 +842,9 @@ suite('Disk File Service', () => {
925842
926843 const folder = URI . file ( join ( watchDir . fsPath , 'folder' ) ) ;
927844
928- const listenerDisposable = service . onFileChanges ( event => {
929- watcherDisposable . dispose ( ) ;
930- listenerDisposable . dispose ( ) ;
931-
932- try {
933- assert . equal ( event . changes . length , 1 ) ;
934- assert . equal ( event . changes [ 0 ] . type , FileChangeType . ADDED ) ;
935- assert . equal ( event . changes [ 0 ] . resource . fsPath , folder . fsPath ) ;
845+ assertWatch ( watchDir , FileChangeType . ADDED , folder , done ) ;
936846
937- done ( ) ;
938- } catch ( error ) {
939- done ( error ) ;
940- }
941- } ) ;
942-
943- const watcherDisposable = service . watch ( watchDir ) ;
944-
945- setTimeout ( ( ) => {
946- mkdirSync ( folder . fsPath ) ;
947- } , 50 ) ;
847+ setTimeout ( ( ) => mkdirSync ( folder . fsPath ) , 50 ) ;
948848 } ) ;
949849
950850 test ( 'watch - folder (non recursive) - delete folder' , done => {
@@ -954,25 +854,27 @@ suite('Disk File Service', () => {
954854 const folder = URI . file ( join ( watchDir . fsPath , 'folder' ) ) ;
955855 mkdirSync ( folder . fsPath ) ;
956856
857+ assertWatch ( watchDir , FileChangeType . DELETED , folder , done ) ;
858+
859+ setTimeout ( ( ) => rimrafSync ( folder . fsPath ) , 50 ) ;
860+ } ) ;
861+
862+ function assertWatch ( toWatch : URI , expectedType : FileChangeType , expectedPath : URI , done : MochaDone ) : void {
863+ const watcherDisposable = service . watch ( toWatch ) ;
864+
957865 const listenerDisposable = service . onFileChanges ( event => {
958866 watcherDisposable . dispose ( ) ;
959867 listenerDisposable . dispose ( ) ;
960868
961869 try {
962870 assert . equal ( event . changes . length , 1 ) ;
963- assert . equal ( event . changes [ 0 ] . type , FileChangeType . DELETED ) ;
964- assert . equal ( event . changes [ 0 ] . resource . fsPath , folder . fsPath ) ;
871+ assert . equal ( event . changes [ 0 ] . type , expectedType ) ;
872+ assert . equal ( event . changes [ 0 ] . resource . fsPath , expectedPath . fsPath ) ;
965873
966874 done ( ) ;
967875 } catch ( error ) {
968876 done ( error ) ;
969877 }
970878 } ) ;
971-
972- const watcherDisposable = service . watch ( watchDir ) ;
973-
974- setTimeout ( ( ) => {
975- rimrafSync ( folder . fsPath ) ;
976- } , 50 ) ;
977- } ) ;
879+ }
978880} ) ;
0 commit comments