From 00b7b606d82c36461d448a2a7b59475235199acf Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Fri, 24 Feb 2017 09:40:37 -0600 Subject: [PATCH] Handle systems where `hostname` is unavailable (#20765) * Handle systems where `hostname` is unavailable Fixes #20758 * Comment on hostname regex * Fix grammar --- test/libgit2.jl | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/libgit2.jl b/test/libgit2.jl index 4ee3480687840..81df89d8b1ff1 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -1020,18 +1020,25 @@ mktempdir() do dir end # Find a hostname that maps to the loopback address - hostname = replace(readchomp(`hostname`), r"\..*$", "") - loopback = ip"127.0.0.1" + hostnames = ["localhost"] - for host in (hostname, "localhost") + # In minimal environments a hostname might not be available (issue #20758) + try + # In some environments, namely Macs, the hostname "macbook.local" is bound + # to the external address while "macbook" is bound to the loopback address. + unshift!(hostnames, replace(gethostname(), r"\..*$", "")) + end + + loopback = ip"127.0.0.1" + for hostname in hostnames try - addr = getaddrinfo(host) + addr = getaddrinfo(hostname) catch continue end if addr == loopback - common_name = host + common_name = hostname break end end