@@ -204,6 +204,134 @@ describe("androidPluginMirrorService", () => {
204204 ) ;
205205 } ) ;
206206
207+ describe ( "an interrupted sync" , ( ) => {
208+ const interruptSync = ( ) => {
209+ const copyFile = fs . copyFile ;
210+ fs . copyFile = function ( source : string , destination : string ) {
211+ if ( path . basename ( source ) === "zzz.txt" ) {
212+ throw new Error ( "interrupted" ) ;
213+ }
214+ return copyFile . call ( this , source , destination ) ;
215+ } ;
216+ fs . writeFile ( upstream ( "platforms" , "android" , "zzz.txt" ) , "z" ) ;
217+ try {
218+ assert . throws (
219+ ( ) => mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ,
220+ "interrupted" ,
221+ ) ;
222+ } finally {
223+ fs . copyFile = copyFile ;
224+ }
225+ } ;
226+ const assertRecovers = ( ) => {
227+ assert . deepStrictEqual ( mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) , [
228+ "@foo/plugin-x" ,
229+ ] ) ;
230+ assert . deepStrictEqual (
231+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ,
232+ [ ] ,
233+ ) ;
234+ } ;
235+ let config : string ;
236+
237+ beforeEach ( ( ) => {
238+ config = upstream ( "platforms" , "android" , "assets" , "config" ) ;
239+ } ) ;
240+
241+ it ( "recovers when a file turned into a directory upstream" , ( ) => {
242+ fs . writeFile ( config , "file" ) ;
243+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ;
244+ fs . deleteFile ( config ) ;
245+ fs . writeFile ( path . join ( config , "settings.json" ) , "{}" ) ;
246+ interruptSync ( ) ;
247+
248+ assertRecovers ( ) ;
249+
250+ assert . equal (
251+ fs . readText (
252+ mirrored ( "platforms" , "android" , "assets" , "config" , "settings.json" ) ,
253+ ) ,
254+ "{}" ,
255+ ) ;
256+ } ) ;
257+
258+ it ( "recovers when a directory turned into a file upstream" , ( ) => {
259+ fs . writeFile ( path . join ( config , "settings.json" ) , "{}" ) ;
260+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ;
261+ fs . deleteDirectory ( config ) ;
262+ fs . writeFile ( config , "file" ) ;
263+ interruptSync ( ) ;
264+
265+ assertRecovers ( ) ;
266+
267+ assert . equal (
268+ fs . readText ( mirrored ( "platforms" , "android" , "assets" , "config" ) ) ,
269+ "file" ,
270+ ) ;
271+ } ) ;
272+
273+ it ( "does not leave files behind that upstream removed before the next sync" , ( ) => {
274+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ;
275+ const extra = upstream ( "platforms" , "android" , "libs" , "extra.aar" ) ;
276+ fs . writeFile ( extra , "extra" ) ;
277+ interruptSync ( ) ;
278+ fs . deleteFile ( extra ) ;
279+
280+ assertRecovers ( ) ;
281+
282+ assert . isFalse (
283+ fs . exists ( mirrored ( "platforms" , "android" , "libs" , "extra.aar" ) ) ,
284+ ) ;
285+ } ) ;
286+ } ) ;
287+
288+ describe ( "onChanged" , ( ) => {
289+ let changed : string [ ] ;
290+ const onChanged = ( { pluginName } : IAndroidPluginMirror ) => {
291+ changed . push ( pluginName ) ;
292+ } ;
293+
294+ beforeEach ( ( ) => {
295+ changed = [ ] ;
296+ } ) ;
297+
298+ it ( "is called once the files of a changed mirror are in place" , ( ) => {
299+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] , ( syncedMirror ) => {
300+ onChanged ( syncedMirror ) ;
301+ assert . equal (
302+ fs . readText ( mirrored ( "platforms" , "android" , "include.gradle" ) ) ,
303+ "a" ,
304+ ) ;
305+ } ) ;
306+
307+ assert . deepStrictEqual ( changed , [ "@foo/plugin-x" ] ) ;
308+ } ) ;
309+
310+ it ( "is not called for an unchanged mirror" , ( ) => {
311+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ;
312+
313+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] , onChanged ) ;
314+
315+ assert . deepStrictEqual ( changed , [ ] ) ;
316+ } ) ;
317+
318+ it ( "is called again after it failed" , ( ) => {
319+ assert . throws (
320+ ( ) =>
321+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] , ( ) => {
322+ throw new Error ( "failed" ) ;
323+ } ) ,
324+ "failed" ,
325+ ) ;
326+
327+ assert . deepStrictEqual (
328+ mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] , onChanged ) ,
329+ [ "@foo/plugin-x" ] ,
330+ ) ;
331+ assert . deepStrictEqual ( changed , [ "@foo/plugin-x" ] ) ;
332+ } ) ;
333+ } ) ;
334+
207335 it ( "removes the mirrors of plugins that are gone" , ( ) => {
208336 mirrorService . syncMirrors ( mirrorsRoot , [ mirror ] ) ;
209337 assert . isTrue ( fs . exists ( mirror . mirrorDir ) ) ;
0 commit comments