@@ -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,150 @@ 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+ // TODO this seems buggy.
714+ if cfg!( windows) { 1 } else { 0 }
715+ ) ;
716+
717+ Ok ( ( ) )
718+ }
719+
720+ #[ test]
721+ fn test_ignore_non_stdlib_extension_module ( ) -> Result < ( ) > {
722+ let mut env = test_evaluation_context_builder ( ) ?. into_context ( ) ?;
723+
724+ let exe_value = env. eval ( indoc ! { r#"
725+ dist = default_python_distribution()
726+ policy = dist.make_python_packaging_policy()
727+ policy.resources_location_fallback = "filesystem-relative:lib"
728+
729+ exe = dist.to_python_executable(
730+ name = "myapp",
731+ packaging_policy = policy
732+ )
733+
734+ exe.add_python_resources(exe.pip_install(["zstandard==0.16.0"]))
735+
736+ exe
737+ "# } ) ?;
738+
739+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
740+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
741+
742+ assert_eq ! (
743+ inner
744+ . iter_resources( )
745+ . filter( |( _, r) | { r. is_extension_module && r. name == "zstandard.backend_c" } )
746+ . count( ) ,
747+ 1
748+ ) ;
749+
750+ let exe_value = env. eval ( indoc ! { r#"
751+ dist = default_python_distribution()
752+ policy = dist.make_python_packaging_policy()
753+ policy.resources_location_fallback = "filesystem-relative:lib"
754+
755+ def cb(policy, resource):
756+ if type(resource) == "PythonExtensionModule":
757+ if resource.name == "zstandard.backend_c":
758+ resource.add_include = False
759+
760+ policy.register_resource_callback(cb)
761+
762+ exe = dist.to_python_executable(
763+ name = "myapp",
764+ packaging_policy = policy,
765+ )
766+
767+ exe.add_python_resources(exe.pip_install(["zstandard==0.16.0"]))
768+
769+ exe
770+ "# } ) ?;
771+
772+ let exe = exe_value. downcast_ref :: < PythonExecutableValue > ( ) . unwrap ( ) ;
773+ let inner = exe. inner ( "ignored" ) . unwrap ( ) ;
774+
775+ assert_eq ! (
776+ inner
777+ . iter_resources( )
778+ . filter( |( _, r) | { r. is_extension_module && r. name == "zstandard.backend_c" } )
779+ . count( ) ,
780+ // TODO should be 0.
781+ 1
782+ ) ;
783+
784+ Ok ( ( ) )
785+ }
635786}
0 commit comments