@@ -64,6 +64,7 @@ function getFakeADBKit(
64
64
return [ ] ;
65
65
} ) ,
66
66
shell : sinon . spy ( ( ) => Promise . resolve ( '' ) ) ,
67
+ readdir : sinon . spy ( ( ) => Promise . resolve ( [ ] ) ) ,
67
68
startActivity : sinon . spy ( ( ) => { } ) ,
68
69
forward : sinon . spy ( ( ) => { } ) ,
69
70
push : sinon . spy ( ( ) => {
@@ -622,6 +623,75 @@ describe('utils/adb', () => {
622
623
} ) ;
623
624
} ) ;
624
625
626
+ describe ( 'checkOrCleanArtifacts' , ( ) => {
627
+ const artifactDir = `web-ext-artifacts-${ Date . now ( ) } ` ;
628
+ function makeFakeArtifact ( artifactName , isDirectory ) {
629
+ return {
630
+ name : artifactName ,
631
+ isDirectory : ( ) => {
632
+ return isDirectory ;
633
+ } ,
634
+ } ;
635
+ }
636
+ const files = [
637
+ makeFakeArtifact ( artifactDir . concat ( 'fake-dir1' ) , true ) ,
638
+ makeFakeArtifact ( artifactDir . concat ( 'fake-dir2' ) , true ) ,
639
+ makeFakeArtifact ( artifactDir . concat ( 'fake-file1' ) , false ) ,
640
+ makeFakeArtifact ( 'not-web-ext-artifacts-dir' , true ) ,
641
+ ] ;
642
+
643
+ it ( 'checks old artifacts' , async ( ) => {
644
+ const adb = getFakeADBKit ( {
645
+ adbClient : {
646
+ readdir : sinon . spy ( ( ) => Promise . resolve ( files ) ) ,
647
+ shell : sinon . spy ( ( ) => Promise . resolve ( '' ) ) ,
648
+ } ,
649
+ adbkitUtil : {
650
+ readAll : sinon . spy ( ( ) => Promise . resolve ( Buffer . from ( '1\n' ) ) ) ,
651
+ } ,
652
+ } ) ;
653
+ const adbUtils = new ADBUtils ( { adb} ) ;
654
+
655
+ const promise = adbUtils . checkOrCleanArtifacts ( 'device1' , false ) ;
656
+ const result = await assert . isFulfilled ( promise ) ;
657
+ assert . equal ( result , true ) ;
658
+
659
+ sinon . assert . calledOnce ( adb . fakeADBClient . readdir ) ;
660
+ //While checking of files shell shoudln't be called
661
+ sinon . assert . notCalled ( adb . fakeADBClient . shell ) ;
662
+ } ) ;
663
+
664
+ it ( 'removes plausible artifacts directory' , async ( ) => {
665
+ const adb = getFakeADBKit ( {
666
+ adbClient : {
667
+ readdir : sinon . spy ( ( ) => Promise . resolve ( files ) ) ,
668
+ shell : sinon . spy ( ( ) => Promise . resolve ( '' ) ) ,
669
+ } ,
670
+ adbkitUtil : {
671
+ readAll : sinon . spy ( ( ) => Promise . resolve ( Buffer . from ( '1\n' ) ) ) ,
672
+ } ,
673
+ } ) ;
674
+ const adbUtils = new ADBUtils ( { adb} ) ;
675
+ const artifactDirFullPath1 = `/sdcard/${ artifactDir } fake-dir1` ;
676
+ const artifactDirFullPath2 = `/sdcard/${ artifactDir } fake-dir2` ;
677
+
678
+ const promise = adbUtils . checkOrCleanArtifacts ( 'device1' , true ) ;
679
+ const result = await assert . isFulfilled ( promise ) ;
680
+ assert . equal ( result , true ) ;
681
+
682
+ sinon . assert . calledOnce ( adb . fakeADBClient . readdir ) ;
683
+ sinon . assert . calledTwice ( adb . fakeADBClient . shell ) ;
684
+ sinon . assert . calledWithMatch (
685
+ adb . fakeADBClient . shell , 'device1' ,
686
+ [ 'rm' , '-rf' , artifactDirFullPath1 ]
687
+ ) ;
688
+ sinon . assert . calledWithMatch (
689
+ adb . fakeADBClient . shell , 'device1' ,
690
+ [ 'rm' , '-rf' , artifactDirFullPath2 ]
691
+ ) ;
692
+ } ) ;
693
+ } ) ;
694
+
625
695
describe ( 'pushFile' , ( ) => {
626
696
it ( 'rejects an UsageError on adb binary not found' , async ( ) => {
627
697
const adb = await testSpawnADBUsageError ( {
0 commit comments