@@ -705,6 +705,109 @@ def test_bake_stream_logs(monkeypatch):
705705 assert output [- 1 ].startswith ("#" )
706706
707707
708+ @pytest .mark .usefixtures ("with_docker_driver" )
709+ @pytest .mark .usefixtures ("change_cwd" )
710+ @pytest .mark .parametrize ("only_print" , [True , False ])
711+ def test_bake_metadata_file_output (only_print , tmp_path ):
712+ metadata_path = tmp_path / "metadata.json"
713+
714+ config = docker .buildx .bake (
715+ files = [bake_file ], metadata_file = metadata_path , print = only_print
716+ )
717+ assert config == {
718+ "group" : {"default" : {"targets" : ["my_out1" , "my_out2" ]}},
719+ "target" : {
720+ "my_out1" : {
721+ "context" : "." ,
722+ "dockerfile" : "Dockerfile" ,
723+ "tags" : ["pretty_image1:1.0.0" ],
724+ "target" : "out1" ,
725+ },
726+ "my_out2" : {
727+ "context" : "." ,
728+ "dockerfile" : "Dockerfile" ,
729+ "tags" : ["pretty_image2:1.0.0" ],
730+ "target" : "out2" ,
731+ },
732+ },
733+ }
734+ if not only_print :
735+ assert metadata_path .is_file ()
736+ with open (metadata_path , "r" ) as f :
737+ metadata = json .load (f )
738+ assert len (metadata .keys ()) == len (
739+ config ["target" ]
740+ ) # two target builds expected
741+ for target_name , target_metadata in metadata .items ():
742+ assert target_name in config ["target" ]
743+ assert (
744+ "docker.io/library/{}" .format (config ["target" ][target_name ]["tags" ][0 ])
745+ in target_metadata ["image.name" ]
746+ )
747+
748+
749+ @pytest .mark .usefixtures ("with_docker_driver" )
750+ @pytest .mark .usefixtures ("change_cwd" )
751+ @pytest .mark .parametrize ("only_print" , [True , False ])
752+ def test_bake_metadata_file_option (only_print , monkeypatch , tmp_path ):
753+ recorded = {}
754+
755+ def fake_run (cmd , capture_stderr = True , env = {}):
756+ recorded ["cmd" ] = list (cmd )
757+ return "{}"
758+
759+ monkeypatch .setattr (python_on_whales .components .buildx .cli_wrapper , "run" , fake_run )
760+
761+ metadata_path = tmp_path / "metadata.json"
762+
763+ docker .buildx .bake (files = [bake_file ], print = only_print , metadata_file = metadata_path )
764+
765+ cmd = recorded .get ("cmd" , [])
766+ assert "--metadata-file" in cmd
767+ assert metadata_path in cmd or str (metadata_path ) in map (str , cmd )
768+
769+
770+ @pytest .mark .usefixtures ("with_docker_driver" )
771+ @pytest .mark .usefixtures ("change_cwd" )
772+ @pytest .mark .parametrize ("only_print" , [True , False ])
773+ def test_bake_without_metadata_file_option (only_print , monkeypatch , tmp_path ):
774+ recorded = {}
775+
776+ def fake_run (cmd , capture_stderr = True , env = {}):
777+ recorded ["cmd" ] = list (cmd )
778+ return "{}"
779+
780+ monkeypatch .setattr (python_on_whales .components .buildx .cli_wrapper , "run" , fake_run )
781+
782+ docker .buildx .bake (files = [bake_file ], print = only_print )
783+
784+ cmd = recorded .get ("cmd" , [])
785+ assert "--metadata-file" not in cmd
786+
787+
788+ @pytest .mark .usefixtures ("with_docker_driver" )
789+ @pytest .mark .usefixtures ("change_cwd" )
790+ @pytest .mark .parametrize ("only_print" , [True , False ])
791+ def test_bake_metadata_file_str_path_option (only_print , monkeypatch , tmp_path ):
792+ recorded = {}
793+
794+ def fake_run (cmd , capture_stderr = True , env = {}):
795+ recorded ["cmd" ] = list (cmd )
796+ return "{}"
797+
798+ monkeypatch .setattr (python_on_whales .components .buildx .cli_wrapper , "run" , fake_run )
799+
800+ metadata_path = tmp_path / "metadata.json"
801+
802+ docker .buildx .bake (
803+ files = [bake_file ], print = only_print , metadata_file = str (metadata_path )
804+ )
805+
806+ cmd = recorded .get ("cmd" , [])
807+ assert "--metadata-file" in cmd
808+ assert metadata_path in cmd or str (metadata_path ) in map (str , cmd )
809+
810+
708811@pytest .mark .usefixtures ("with_docker_driver" )
709812@pytest .mark .usefixtures ("prune_all" )
710813def test_prune_all_empty ():
0 commit comments