@@ -304,22 +304,25 @@ pub async fn find_gateway_container(docker: &Docker, port: Option<u16>) -> Resul
304304
305305 let matches: Vec < String > = containers
306306 . iter ( )
307- . filter ( |c| is_gateway_image ( c) && port. map_or ( true , |p| has_port ( c, p) ) )
307+ . filter ( |c| is_gateway_image ( c) && port. is_none_or ( |p| has_port ( c, p) ) )
308308 . filter_map ( container_name)
309309 . collect ( ) ;
310310
311311 match matches. len ( ) {
312312 0 => {
313- let hint = if let Some ( p ) = port {
314- format ! (
315- "No openshell gateway container found listening on port {p} .\n \
313+ let hint = port. map_or_else (
314+ || {
315+ "No openshell gateway container found.\n \
316316 Is the gateway running? Check with: docker ps"
317- )
318- } else {
319- "No openshell gateway container found.\n \
320- Is the gateway running? Check with: docker ps"
321- . to_string ( )
322- } ;
317+ . to_string ( )
318+ } ,
319+ |p| {
320+ format ! (
321+ "No openshell gateway container found listening on port {p}.\n \
322+ Is the gateway running? Check with: docker ps"
323+ )
324+ } ,
325+ ) ;
323326 Err ( miette:: miette!( "{hint}" ) )
324327 }
325328 1 => Ok ( matches. into_iter ( ) . next ( ) . unwrap ( ) ) ,
@@ -748,22 +751,22 @@ pub async fn check_port_conflicts(
748751
749752 let ports = container. ports . as_deref ( ) . unwrap_or_default ( ) ;
750753 for port in ports {
751- if let Some ( public) = port. public_port {
752- if needed_ports. contains ( & public) {
753- let cname = names
754- . first ( )
755- . map ( |n| n . trim_start_matches ( '/' ) . to_string ( ) )
756- . unwrap_or_else ( || {
757- container
758- . id
759- . clone ( )
760- . unwrap_or_else ( || "<unknown>" . to_string ( ) )
761- } ) ;
762- conflicts . push ( PortConflict {
763- container_name : cname ,
764- host_port : public ,
765- } ) ;
766- }
754+ if let Some ( public) = port. public_port
755+ && needed_ports. contains ( & public)
756+ {
757+ let cname = names . first ( ) . map_or_else (
758+ || {
759+ container
760+ . id
761+ . clone ( )
762+ . unwrap_or_else ( || "<unknown>" . to_string ( ) )
763+ } ,
764+ |n| n . trim_start_matches ( '/' ) . to_string ( ) ,
765+ ) ;
766+ conflicts . push ( PortConflict {
767+ container_name : cname ,
768+ host_port : public ,
769+ } ) ;
767770 }
768771 }
769772 }
@@ -1091,6 +1094,7 @@ fn is_port_conflict(err: &BollardError) -> bool {
10911094#[ cfg( test) ]
10921095mod tests {
10931096 use super :: * ;
1097+ use temp_env:: with_var;
10941098
10951099 #[ test]
10961100 fn normalize_arch_x86_64 ( ) {
@@ -1152,36 +1156,22 @@ mod tests {
11521156 #[ test]
11531157 fn docker_not_reachable_error_with_docker_host ( ) {
11541158 // Simulate: DOCKER_HOST is set but daemon unresponsive.
1155- // We set the env var temporarily (this is test-only).
1156- let prev_docker_host = std:: env:: var ( "DOCKER_HOST" ) . ok ( ) ;
1157- // SAFETY: test-only, single-threaded test runner for this test
1158- unsafe {
1159- std:: env:: set_var ( "DOCKER_HOST" , "unix:///tmp/fake-docker.sock" ) ;
1160- }
1161-
1162- let err = docker_not_reachable_error (
1163- "daemon not responding" ,
1164- "Docker socket exists but the daemon is not responding" ,
1165- ) ;
1166- let msg = format ! ( "{err:?}" ) ;
1167-
1168- // Restore env
1169- // SAFETY: test-only, restoring previous state
1170- unsafe {
1171- match prev_docker_host {
1172- Some ( val) => std:: env:: set_var ( "DOCKER_HOST" , val) ,
1173- None => std:: env:: remove_var ( "DOCKER_HOST" ) ,
1174- }
1175- }
1159+ with_var ( "DOCKER_HOST" , Some ( "unix:///tmp/fake-docker.sock" ) , || {
1160+ let err = docker_not_reachable_error (
1161+ "daemon not responding" ,
1162+ "Docker socket exists but the daemon is not responding" ,
1163+ ) ;
1164+ let msg = format ! ( "{err:?}" ) ;
11761165
1177- assert ! (
1178- msg. contains( "DOCKER_HOST" ) ,
1179- "should mention DOCKER_HOST when it is set"
1180- ) ;
1181- assert ! (
1182- msg. contains( "unix:///tmp/fake-docker.sock" ) ,
1183- "should show the current DOCKER_HOST value"
1184- ) ;
1166+ assert ! (
1167+ msg. contains( "DOCKER_HOST" ) ,
1168+ "should mention DOCKER_HOST when it is set"
1169+ ) ;
1170+ assert ! (
1171+ msg. contains( "unix:///tmp/fake-docker.sock" ) ,
1172+ "should show the current DOCKER_HOST value"
1173+ ) ;
1174+ } ) ;
11851175 }
11861176
11871177 #[ test]
0 commit comments