@@ -12,7 +12,7 @@ const {
12
12
} = require ( 'fs-extra' ) ;
13
13
const { sep } = require ( 'path' ) ;
14
14
15
- const { getUserCachePath } = require ( './lib/shared' ) ;
15
+ const { getUserCachePath, md5Path } = require ( './lib/shared' ) ;
16
16
17
17
const initialWorkingDir = process . cwd ( ) ;
18
18
@@ -688,3 +688,220 @@ test('py3.6 uses download cache with cacheLocation option', t => {
688
688
) ;
689
689
t . end ( ) ;
690
690
} ) ;
691
+
692
+ test (
693
+ 'py3.6 uses download cache with dockerizePip option' ,
694
+ t => {
695
+ process . chdir ( 'tests/base' ) ;
696
+ const path = npm ( [ 'pack' , '../..' ] ) ;
697
+ npm ( [ 'i' , path ] ) ;
698
+ sls ( [ '--useDownloadCache=true' , '--dockerizePip=true' , 'package' ] ) ;
699
+ const cachepath = getUserCachePath ( ) ;
700
+ t . true (
701
+ pathExistsSync ( `${ cachepath } ${ sep } downloadCacheslspyc${ sep } http` ) ,
702
+ 'cache directoy exists'
703
+ ) ;
704
+ t . end ( ) ;
705
+ } ,
706
+ { skip : ! canUseDocker ( ) }
707
+ ) ;
708
+
709
+ test ( 'py3.6 uses download cache with dockerizePip + cacheLocation option' , t => {
710
+ process . chdir ( 'tests/base' ) ;
711
+ const path = npm ( [ 'pack' , '../..' ] ) ;
712
+ npm ( [ 'i' , path ] ) ;
713
+ sls ( [
714
+ '--useDownloadCache=true' ,
715
+ '--dockerizePip=true' ,
716
+ '--cacheLocation=.requirements-cache' ,
717
+ 'package'
718
+ ] ) ;
719
+ t . true (
720
+ pathExistsSync ( `.requirements-cache${ sep } downloadCacheslspyc${ sep } http` ) ,
721
+ 'cache directoy exists'
722
+ ) ;
723
+ t . end ( ) ;
724
+ } ) ;
725
+
726
+ test ( 'py3.6 uses static and download cache' , t => {
727
+ process . chdir ( 'tests/base' ) ;
728
+ const path = npm ( [ 'pack' , '../..' ] ) ;
729
+ npm ( [ 'i' , path ] ) ;
730
+ sls ( [ '--useDownloadCache=true' , '--useStaticCache=true' , 'package' ] ) ;
731
+ const cachepath = getUserCachePath ( ) ;
732
+ const cacheFolderHash = md5Path ( '.serverless/requirements.txt' ) ;
733
+ t . true (
734
+ pathExistsSync ( `${ cachepath } ${ sep } downloadCacheslspyc${ sep } http` ) ,
735
+ 'http exists in download-cache'
736
+ ) ;
737
+ t . true (
738
+ pathExistsSync ( `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } flask` ) ,
739
+ 'flask exists in static-cache'
740
+ ) ;
741
+ t . end ( ) ;
742
+ } ) ;
743
+
744
+ test (
745
+ 'py3.6 uses static and download cache with dockerizePip option' ,
746
+ t => {
747
+ process . chdir ( 'tests/base' ) ;
748
+ const path = npm ( [ 'pack' , '../..' ] ) ;
749
+ npm ( [ 'i' , path ] ) ;
750
+ sls ( [
751
+ '--useDownloadCache=true' ,
752
+ '--useStaticCache=true' ,
753
+ '--dockerizePip=true' ,
754
+ 'package'
755
+ ] ) ;
756
+ const cachepath = getUserCachePath ( ) ;
757
+ const cacheFolderHash = md5Path ( '.serverless/requirements.txt' ) ;
758
+ t . true (
759
+ pathExistsSync ( `${ cachepath } ${ sep } downloadCacheslspyc${ sep } http` ) ,
760
+ 'http exists in download-cache'
761
+ ) ;
762
+ t . true (
763
+ pathExistsSync ( `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } flask` ) ,
764
+ 'flask exists in static-cache'
765
+ ) ;
766
+ t . end ( ) ;
767
+ } ,
768
+ { skip : ! canUseDocker ( ) }
769
+ ) ;
770
+
771
+ test ( 'py3.6 uses static cache' , t => {
772
+ process . chdir ( 'tests/base' ) ;
773
+ const path = npm ( [ 'pack' , '../..' ] ) ;
774
+ npm ( [ 'i' , path ] ) ;
775
+ sls ( [ '--useStaticCache=true' , 'package' ] ) ;
776
+ const cachepath = getUserCachePath ( ) ;
777
+ const cacheFolderHash = md5Path ( '.serverless/requirements.txt' ) ;
778
+ t . true (
779
+ pathExistsSync ( `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } flask` ) ,
780
+ 'flask exists in static-cache'
781
+ ) ;
782
+ t . true (
783
+ pathExistsSync (
784
+ `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } .completed_requirements`
785
+ ) ,
786
+ '.completed_requirements exists in static-cache'
787
+ ) ;
788
+
789
+ // py3.6 checking that static cache actually pulls from cache (by poisoning it)
790
+ writeFileSync (
791
+ `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } injected_file_is_bad_form` ,
792
+ 'injected new file into static cache folder'
793
+ ) ;
794
+ sls ( [ '--useStaticCache=true' , 'package' ] ) ;
795
+
796
+ const zipfiles = listZipFiles ( '.serverless/sls-py-req-test.zip' ) ;
797
+ t . true (
798
+ zipfiles . includes ( 'injected_file_is_bad_form' ) ,
799
+ "static cache is really used when running 'sls package' again"
800
+ ) ;
801
+
802
+ t . end ( ) ;
803
+ } ) ;
804
+
805
+ test ( 'py3.6 uses static cache with cacheLocation option' , t => {
806
+ process . chdir ( 'tests/base' ) ;
807
+ const path = npm ( [ 'pack' , '../..' ] ) ;
808
+ npm ( [ 'i' , path ] ) ;
809
+ const cachepath = '.requirements-cache' ;
810
+ sls ( [ '--useStaticCache=true' , `--cacheLocation=${ cachepath } ` , 'package' ] ) ;
811
+ const cacheFolderHash = md5Path ( '.serverless/requirements.txt' ) ;
812
+ t . true (
813
+ pathExistsSync ( `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } flask` ) ,
814
+ 'flask exists in static-cache'
815
+ ) ;
816
+ t . true (
817
+ pathExistsSync (
818
+ `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } .completed_requirements`
819
+ ) ,
820
+ '.completed_requirements exists in static-cache'
821
+ ) ;
822
+ t . end ( ) ;
823
+ } ) ;
824
+
825
+ test (
826
+ 'py3.6 uses static cache with dockerizePip & slim option' ,
827
+ t => {
828
+ process . chdir ( 'tests/base' ) ;
829
+ const path = npm ( [ 'pack' , '../..' ] ) ;
830
+ npm ( [ 'i' , path ] ) ;
831
+ sls ( [
832
+ '--useStaticCache=true' ,
833
+ '--dockerizePip=true' ,
834
+ '--slim=true' ,
835
+ 'package'
836
+ ] ) ;
837
+ const cachepath = getUserCachePath ( ) ;
838
+ const cacheFolderHash = md5Path ( '.serverless/requirements.txt' ) ;
839
+ t . true (
840
+ pathExistsSync ( `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } flask` ) ,
841
+ 'flask exists in static-cache'
842
+ ) ;
843
+ t . true (
844
+ pathExistsSync (
845
+ `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } .completed_requirements`
846
+ ) ,
847
+ '.completed_requirements exists in static-cache'
848
+ ) ;
849
+
850
+ // py3.6 checking that static cache actually pulls from cache (by poisoning it)
851
+ writeFileSync (
852
+ `${ cachepath } ${ sep } ${ cacheFolderHash } _slspyc${ sep } injected_file_is_bad_form` ,
853
+ 'injected new file into static cache folder'
854
+ ) ;
855
+ sls ( [
856
+ '--useStaticCache=true' ,
857
+ '--dockerizePip=true' ,
858
+ '--slim=true' ,
859
+ 'package'
860
+ ] ) ;
861
+
862
+ const zipfiles = listZipFiles ( '.serverless/sls-py-req-test.zip' ) ;
863
+ t . true (
864
+ zipfiles . includes ( 'injected_file_is_bad_form' ) ,
865
+ "static cache is really used when running 'sls package' again"
866
+ ) ;
867
+ t . deepEqual (
868
+ zipfiles . filter ( filename => filename . endsWith ( '.pyc' ) ) ,
869
+ [ ] ,
870
+ 'no pyc files are packaged'
871
+ ) ;
872
+
873
+ t . end ( ) ;
874
+ } ,
875
+ { skip : ! canUseDocker ( ) }
876
+ ) ;
877
+
878
+ test (
879
+ 'py3.6 uses download cache with dockerizePip & slim option' ,
880
+ t => {
881
+ process . chdir ( 'tests/base' ) ;
882
+ const path = npm ( [ 'pack' , '../..' ] ) ;
883
+ npm ( [ 'i' , path ] ) ;
884
+ sls ( [
885
+ '--useDownloadCache=true' ,
886
+ '--dockerizePip=true' ,
887
+ '--slim=true' ,
888
+ 'package'
889
+ ] ) ;
890
+ const cachepath = getUserCachePath ( ) ;
891
+ t . true (
892
+ pathExistsSync ( `${ cachepath } ${ sep } downloadCacheslspyc${ sep } http` ) ,
893
+ 'http exists in download-cache'
894
+ ) ;
895
+
896
+ const zipfiles = listZipFiles ( '.serverless/sls-py-req-test.zip' ) ;
897
+ t . true ( zipfiles . includes ( `flask${ sep } __init__.py` ) , 'flask is packaged' ) ;
898
+ t . deepEqual (
899
+ zipfiles . filter ( filename => filename . endsWith ( '.pyc' ) ) ,
900
+ [ ] ,
901
+ 'no pyc files are packaged'
902
+ ) ;
903
+
904
+ t . end ( ) ;
905
+ } ,
906
+ { skip : ! canUseDocker ( ) }
907
+ ) ;
0 commit comments