@@ -414,7 +414,7 @@ mod tests {
414
414
PathBuf :: from ( crate_dir) . join ( "tests" )
415
415
}
416
416
417
- const TPLS_IN_THIS : usize = 8 ;
417
+ const TPLS_IN_THIS : usize = 9 ;
418
418
419
419
#[ tokio:: test]
420
420
async fn can_install_into_new_directory ( ) {
@@ -631,10 +631,12 @@ mod tests {
631
631
. into_iter ( )
632
632
. collect ( ) ;
633
633
let options = RunOptions {
634
+ variant : crate :: template:: TemplateVariantKind :: NewApplication ,
634
635
output_path : output_dir. clone ( ) ,
635
636
name : "my project" . to_owned ( ) ,
636
637
values,
637
638
accept_defaults : false ,
639
+ existing_manifest : None ,
638
640
} ;
639
641
640
642
template
@@ -669,10 +671,12 @@ mod tests {
669
671
let output_dir = dest_temp_dir. path ( ) . join ( "myproj" ) ;
670
672
let values = HashMap :: new ( ) ;
671
673
let options = RunOptions {
674
+ variant : crate :: template:: TemplateVariantKind :: NewApplication ,
672
675
output_path : output_dir. clone ( ) ,
673
676
name : "my project" . to_owned ( ) ,
674
677
values,
675
678
accept_defaults : true ,
679
+ existing_manifest : None ,
676
680
} ;
677
681
678
682
template
@@ -716,10 +720,12 @@ mod tests {
716
720
. into_iter ( )
717
721
. collect ( ) ;
718
722
let options = RunOptions {
723
+ variant : crate :: template:: TemplateVariantKind :: NewApplication ,
719
724
output_path : output_dir. clone ( ) ,
720
725
name : "custom-filter-test" . to_owned ( ) ,
721
726
values,
722
727
accept_defaults : false ,
728
+ existing_manifest : None ,
723
729
} ;
724
730
725
731
template
@@ -737,4 +743,206 @@ mod tests {
737
743
assert ! ( message. contains( "p2/studly = nOmNoMnOm" ) ) ;
738
744
assert ! ( message. contains( "p1/clappy = b👏i👏s👏c👏u👏i👏t👏s" ) ) ;
739
745
}
746
+
747
+ #[ tokio:: test]
748
+ async fn can_add_component_from_template ( ) {
749
+ let temp_dir = tempdir ( ) . unwrap ( ) ;
750
+ let store = TemplateStore :: new ( temp_dir. path ( ) ) ;
751
+ let manager = TemplateManager { store } ;
752
+ let source = TemplateSource :: File ( project_root ( ) ) ;
753
+
754
+ manager
755
+ . install ( & source, & InstallOptions :: default ( ) , & DiscardingReporter )
756
+ . await
757
+ . unwrap ( ) ;
758
+
759
+ let dest_temp_dir = tempdir ( ) . unwrap ( ) ;
760
+ let application_dir = dest_temp_dir. path ( ) . join ( "multi" ) ;
761
+
762
+ // Set up the containing app
763
+ {
764
+ let template = manager. get ( "http-empty" ) . unwrap ( ) . unwrap ( ) ;
765
+
766
+ let values = [
767
+ ( "project-description" . to_owned ( ) , "my desc" . to_owned ( ) ) ,
768
+ ( "http-base" . to_owned ( ) , "/" . to_owned ( ) ) ,
769
+ ]
770
+ . into_iter ( )
771
+ . collect ( ) ;
772
+ let options = RunOptions {
773
+ variant : crate :: template:: TemplateVariantKind :: NewApplication ,
774
+ output_path : application_dir. clone ( ) ,
775
+ name : "my multi project" . to_owned ( ) ,
776
+ values,
777
+ accept_defaults : false ,
778
+ existing_manifest : None ,
779
+ } ;
780
+
781
+ template
782
+ . run ( options)
783
+ . silent ( )
784
+ . await
785
+ . execute ( )
786
+ . await
787
+ . unwrap ( ) ;
788
+ }
789
+
790
+ let spin_toml_path = application_dir. join ( "spin.toml" ) ;
791
+ assert ! ( spin_toml_path. exists( ) , "expected spin.toml to be created" ) ;
792
+
793
+ // Now add a component
794
+ {
795
+ let template = manager. get ( "http-rust" ) . unwrap ( ) . unwrap ( ) ;
796
+
797
+ let output_dir = "hello" ;
798
+ let values = [
799
+ ( "project-description" . to_owned ( ) , "hello" . to_owned ( ) ) ,
800
+ ( "http-path" . to_owned ( ) , "/hello" . to_owned ( ) ) ,
801
+ ]
802
+ . into_iter ( )
803
+ . collect ( ) ;
804
+ let options = RunOptions {
805
+ variant : crate :: template:: TemplateVariantKind :: AddComponent ,
806
+ output_path : PathBuf :: from ( output_dir) ,
807
+ name : "hello" . to_owned ( ) ,
808
+ values,
809
+ accept_defaults : false ,
810
+ existing_manifest : Some ( spin_toml_path. clone ( ) ) ,
811
+ } ;
812
+
813
+ template
814
+ . run ( options)
815
+ . silent ( )
816
+ . await
817
+ . execute ( )
818
+ . await
819
+ . unwrap ( ) ;
820
+ }
821
+
822
+ // And another
823
+ {
824
+ let template = manager. get ( "http-rust" ) . unwrap ( ) . unwrap ( ) ;
825
+
826
+ let output_dir = "encore" ;
827
+ let values = [
828
+ ( "project-description" . to_owned ( ) , "hello 2" . to_owned ( ) ) ,
829
+ ( "http-path" . to_owned ( ) , "/hello-2" . to_owned ( ) ) ,
830
+ ]
831
+ . into_iter ( )
832
+ . collect ( ) ;
833
+ let options = RunOptions {
834
+ variant : crate :: template:: TemplateVariantKind :: AddComponent ,
835
+ output_path : PathBuf :: from ( output_dir) ,
836
+ name : "hello 2" . to_owned ( ) ,
837
+ values,
838
+ accept_defaults : false ,
839
+ existing_manifest : Some ( spin_toml_path. clone ( ) ) ,
840
+ } ;
841
+
842
+ template
843
+ . run ( options)
844
+ . silent ( )
845
+ . await
846
+ . execute ( )
847
+ . await
848
+ . unwrap ( ) ;
849
+ }
850
+
851
+ let cargo1 = tokio:: fs:: read_to_string ( application_dir. join ( "hello/Cargo.toml" ) )
852
+ . await
853
+ . unwrap ( ) ;
854
+ assert ! ( cargo1. contains( "name = \" hello\" " ) ) ;
855
+
856
+ let cargo2 = tokio:: fs:: read_to_string ( application_dir. join ( "encore/Cargo.toml" ) )
857
+ . await
858
+ . unwrap ( ) ;
859
+ assert ! ( cargo2. contains( "name = \" hello-2\" " ) ) ;
860
+
861
+ let spin_toml = tokio:: fs:: read_to_string ( & spin_toml_path) . await . unwrap ( ) ;
862
+ assert ! ( spin_toml. contains( "source = \" hello/target/wasm32-wasi/release/hello.wasm\" " ) ) ;
863
+ assert ! ( spin_toml. contains( "source = \" encore/target/wasm32-wasi/release/hello_2.wasm\" " ) ) ;
864
+ }
865
+
866
+ #[ tokio:: test]
867
+ async fn cannot_add_component_that_does_not_match_trigger ( ) {
868
+ let temp_dir = tempdir ( ) . unwrap ( ) ;
869
+ let store = TemplateStore :: new ( temp_dir. path ( ) ) ;
870
+ let manager = TemplateManager { store } ;
871
+ let source = TemplateSource :: File ( project_root ( ) ) ;
872
+
873
+ manager
874
+ . install ( & source, & InstallOptions :: default ( ) , & DiscardingReporter )
875
+ . await
876
+ . unwrap ( ) ;
877
+
878
+ let dest_temp_dir = tempdir ( ) . unwrap ( ) ;
879
+ let application_dir = dest_temp_dir. path ( ) . join ( "multi" ) ;
880
+
881
+ // Set up the containing app
882
+ {
883
+ let template = manager. get ( "redis-rust" ) . unwrap ( ) . unwrap ( ) ;
884
+
885
+ let values = [
886
+ ( "project-description" . to_owned ( ) , "my desc" . to_owned ( ) ) ,
887
+ (
888
+ "redis-address" . to_owned ( ) ,
889
+ "redis://localhost:6379" . to_owned ( ) ,
890
+ ) ,
891
+ (
892
+ "redis-channel" . to_owned ( ) ,
893
+ "the-horrible-knuckles" . to_owned ( ) ,
894
+ ) ,
895
+ ]
896
+ . into_iter ( )
897
+ . collect ( ) ;
898
+ let options = RunOptions {
899
+ variant : crate :: template:: TemplateVariantKind :: NewApplication ,
900
+ output_path : application_dir. clone ( ) ,
901
+ name : "my multi project" . to_owned ( ) ,
902
+ values,
903
+ accept_defaults : false ,
904
+ existing_manifest : None ,
905
+ } ;
906
+
907
+ template
908
+ . run ( options)
909
+ . silent ( )
910
+ . await
911
+ . execute ( )
912
+ . await
913
+ . unwrap ( ) ;
914
+ }
915
+
916
+ let spin_toml_path = application_dir. join ( "spin.toml" ) ;
917
+ assert ! ( spin_toml_path. exists( ) , "expected spin.toml to be created" ) ;
918
+
919
+ // Now add a component
920
+ {
921
+ let template = manager. get ( "http-rust" ) . unwrap ( ) . unwrap ( ) ;
922
+
923
+ let output_dir = "hello" ;
924
+ let values = [
925
+ ( "project-description" . to_owned ( ) , "hello" . to_owned ( ) ) ,
926
+ ( "http-path" . to_owned ( ) , "/hello" . to_owned ( ) ) ,
927
+ ]
928
+ . into_iter ( )
929
+ . collect ( ) ;
930
+ let options = RunOptions {
931
+ variant : crate :: template:: TemplateVariantKind :: AddComponent ,
932
+ output_path : PathBuf :: from ( output_dir) ,
933
+ name : "hello" . to_owned ( ) ,
934
+ values,
935
+ accept_defaults : false ,
936
+ existing_manifest : Some ( spin_toml_path. clone ( ) ) ,
937
+ } ;
938
+
939
+ template
940
+ . run ( options)
941
+ . silent ( )
942
+ . await
943
+ . execute ( )
944
+ . await
945
+ . expect_err ( "Expected to fail to add component, but it succeeded" ) ;
946
+ }
947
+ }
740
948
}
0 commit comments