@@ -352,8 +352,13 @@ starlark_module! { python_packaging_policy_module =>
352352#[ cfg( test) ]
353353mod tests {
354354 use {
355- super :: super :: python_distribution:: PythonDistributionValue , super :: super :: testutil:: * ,
356- super :: * , anyhow:: Result ,
355+ super :: super :: testutil:: * ,
356+ super :: super :: {
357+ python_distribution:: PythonDistributionValue , python_executable:: PythonExecutableValue ,
358+ } ,
359+ super :: * ,
360+ anyhow:: Result ,
361+ indoc:: indoc,
357362 } ;
358363
359364 #[ test]
@@ -632,4 +637,149 @@ mod tests {
632637
633638 Ok ( ( ) )
634639 }
640+
641+ #[ test]
642+ fn test_stdlib_extension_module_enable ( ) -> Result < ( ) > {
643+ let mut env = test_evaluation_context_builder ( ) ?. into_context ( ) ?;
644+
645+ let exe_value = env. eval ( indoc ! { r#"
646+ dist = default_python_distribution()
647+ policy = dist.make_python_packaging_policy()
648+ policy.extension_module_filter = "minimal"
649+ policy.resources_location_fallback = "filesystem-relative:lib"
650+
651+ def cb(policy, resource):
652+ if type(resource) == "PythonExtensionModule":
653+ if resource.name == "_ssl":
654+ resource.add_include = True
655+
656+ policy.register_resource_callback(cb)
657+
658+ exe = dist.to_python_executable(
659+ name = "myapp",
660+ packaging_policy = policy
661+ )
662+
663+ exe
664+ "# } ) ?;
665+
666+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
667+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
668+
669+ assert_eq ! (
670+ inner
671+ . iter_resources( )
672+ . filter( |( _, r) | { r. is_extension_module && r. name == "_ssl" } )
673+ . count( ) ,
674+ // TODO arguably should be 1.
675+ 0 ,
676+ ) ;
677+
678+ Ok ( ( ) )
679+ }
680+
681+ #[ test]
682+ fn test_stdlib_extension_module_disable ( ) -> Result < ( ) > {
683+ let mut env = test_evaluation_context_builder ( ) ?. into_context ( ) ?;
684+
685+ let exe_value = env. eval ( indoc ! { r#"
686+ dist = default_python_distribution()
687+ policy = dist.make_python_packaging_policy()
688+ policy.resources_location_fallback = "filesystem-relative:lib"
689+
690+ def cb(policy, resource):
691+ if type(resource) == "PythonExtensionModule":
692+ if resource.name == "_ssl":
693+ resource.add_include = False
694+
695+ policy.register_resource_callback(cb)
696+
697+ exe = dist.to_python_executable(
698+ name = "myapp",
699+ packaging_policy = policy
700+ )
701+
702+ exe
703+ "# } ) ?;
704+
705+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
706+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
707+
708+ assert_eq ! (
709+ inner
710+ . iter_resources( )
711+ . filter( |( _, r) | { r. is_extension_module && r. name == "_ssl" } )
712+ . count( ) ,
713+ 0 ,
714+ ) ;
715+
716+ Ok ( ( ) )
717+ }
718+
719+ #[ test]
720+ fn test_ignore_non_stdlib_extension_module ( ) -> Result < ( ) > {
721+ let mut env = test_evaluation_context_builder ( ) ?. into_context ( ) ?;
722+
723+ let exe_value = env. eval ( indoc ! { r#"
724+ dist = default_python_distribution()
725+ policy = dist.make_python_packaging_policy()
726+ policy.resources_location_fallback = "filesystem-relative:lib"
727+
728+ exe = dist.to_python_executable(
729+ name = "myapp",
730+ packaging_policy = policy
731+ )
732+
733+ exe.add_python_resources(exe.pip_install(["zstandard==0.16.0"]))
734+
735+ exe
736+ "# } ) ?;
737+
738+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
739+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
740+
741+ assert_eq ! (
742+ inner
743+ . iter_resources( )
744+ . filter( |( _, r) | { r. is_extension_module && r. name == "zstandard.backend_c" } )
745+ . count( ) ,
746+ 1
747+ ) ;
748+
749+ let exe_value = env. eval ( indoc ! { r#"
750+ dist = default_python_distribution()
751+ policy = dist.make_python_packaging_policy()
752+ policy.resources_location_fallback = "filesystem-relative:lib"
753+
754+ def cb(policy, resource):
755+ if type(resource) == "PythonExtensionModule":
756+ if resource.name == "zstandard.backend_c":
757+ resource.add_include = False
758+
759+ policy.register_resource_callback(cb)
760+
761+ exe = dist.to_python_executable(
762+ name = "myapp",
763+ packaging_policy = policy,
764+ )
765+
766+ exe.add_python_resources(exe.pip_install(["zstandard==0.16.0"]))
767+
768+ exe
769+ "# } ) ?;
770+
771+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
772+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
773+
774+ assert_eq ! (
775+ inner
776+ . iter_resources( )
777+ . filter( |( _, r) | { r. is_extension_module && r. name == "zstandard.backend_c" } )
778+ . count( ) ,
779+ // TODO should be 0.
780+ 1
781+ ) ;
782+
783+ Ok ( ( ) )
784+ }
635785}
0 commit comments