Skip to content

Commit

Permalink
Rename configured?/1 to network_configured?/1
Browse files Browse the repository at this point in the history
This also removes the call to get the configuration to make the helper
more explicitly pure.

This helper is useful for differentiating whether a WiFi interface is
configured for scanning WiFi networks only or whether it has a network
configuration on it.
  • Loading branch information
Frank Hunleth authored and fhunleth committed Mar 31, 2024
1 parent a7bbb73 commit 53cc4f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 19 additions & 10 deletions lib/vintage_net_wifi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -997,15 +997,24 @@ defmodule VintageNetWiFi do
defp trim_orphan_backslash(s), do: s

@doc """
Check if a wlan network interface is configured.
"""
@spec configured?(map | binary) :: boolean
def configured?(wlan_ifname) when is_binary(wlan_ifname) do
wlan_ifname |> VintageNet.get_configuration() |> configured?()
end
Helper for checking whether a WiFi configuration has a network configured
This is useful for checking whether a WiFi configuration is just good for
scanning for WiFi networks or whether it actually could connect to another
computer.
Returns `false` if the configuration isn't a VintageNetWiFi one or if no
networks were specified.
def configured?(wlan_config) when wlan_config == %{type: VintageNetWiFi}, do: false
def configured?(%{vintage_net_wifi: %{networks: []}}), do: false
def configured?(%{vintage_net_wifi: %{networks: [_ | _]}}), do: true
def configured?(_), do: false
To test an `ifname` has a network configured, run:
```
VintageNet.get_configuration() |> network_configured?()
```
"""
@spec network_configured?(map()) :: boolean()
def network_configured?(wlan_config) when wlan_config == %{type: VintageNetWiFi}, do: false
def network_configured?(%{vintage_net_wifi: %{networks: []}}), do: false
def network_configured?(%{vintage_net_wifi: %{networks: [_ | _]}}), do: true
def network_configured?(_), do: false
end
8 changes: 4 additions & 4 deletions test/vintage_net_wifi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2688,9 +2688,9 @@ defmodule VintageNetWiFiTest do
ipv4: %{method: :disabled}
}

assert VintageNetWiFi.configured?(configured)
refute VintageNetWiFi.configured?(empty1)
refute VintageNetWiFi.configured?(empty2)
refute VintageNetWiFi.configured?(%{})
assert VintageNetWiFi.network_configured?(configured)
refute VintageNetWiFi.network_configured?(empty1)
refute VintageNetWiFi.network_configured?(empty2)
refute VintageNetWiFi.network_configured?(%{})
end
end

0 comments on commit 53cc4f2

Please sign in to comment.