From a06ea93dd313d85163ac9045db71664cf94bdace Mon Sep 17 00:00:00 2001 From: Sam Uong Date: Tue, 4 May 2021 22:15:39 +1000 Subject: [PATCH] Split pacfinder(_test).go into versions for darwin/unix/windows The tests currently fail on Windows due to the darwin and unix code not working on Windows. This change fixes that. --- pacfinder.go => pacfinder_darwin.go | 30 +--------- pacfinder_test.go => pacfinder_darwin_test.go | 38 ++----------- pacfinder_unix.go | 33 +++++++++++ pacfinder_unix_test.go | 56 +++++++++++++++++++ pacfinder_windows.go | 20 +++++++ 5 files changed, 115 insertions(+), 62 deletions(-) rename pacfinder.go => pacfinder_darwin.go (77%) rename pacfinder_test.go => pacfinder_darwin_test.go (62%) create mode 100644 pacfinder_unix.go create mode 100644 pacfinder_unix_test.go create mode 100644 pacfinder_windows.go diff --git a/pacfinder.go b/pacfinder_darwin.go similarity index 77% rename from pacfinder.go rename to pacfinder_darwin.go index e8ab09a..d0a1e01 100644 --- a/pacfinder.go +++ b/pacfinder_darwin.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Alpaca Authors +// Copyright 2019, 2021 The Alpaca Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,24 +20,10 @@ import ( "io" "log" "os/exec" - "runtime" "strings" ) func findPACURL() (string, error) { - switch runtime.GOOS { - case "darwin": - return findPACURLForDarwin() - case "windows": - return findPACURLForWindows() - default: - // Hopefully Linux, FreeBSD, Solaris, etc. will have GNOME 3 installed... - // TODO: Figure out how to do this for KDE. - return findPACURLForGNOME() - } -} - -func findPACURLForDarwin() (string, error) { cmd := exec.Command("networksetup", "-listallnetworkservices") stdout, err := cmd.StdoutPipe() if err != nil { @@ -101,17 +87,3 @@ func getAutoProxyURL(networkService string) (string, error) { } return "", fmt.Errorf("No auto-proxy URL for network service %v", networkService) } - -func findPACURLForWindows() (string, error) { - // TODO: Implement this. - return "", nil -} - -func findPACURLForGNOME() (string, error) { - cmd := exec.Command("gsettings", "get", "org.gnome.system.proxy", "autoconfig-url") - out, err := cmd.Output() - if err != nil { - return "", err - } - return strings.Trim(string(out), "'\n"), nil -} diff --git a/pacfinder_test.go b/pacfinder_darwin_test.go similarity index 62% rename from pacfinder_test.go rename to pacfinder_darwin_test.go index bc8fe15..5d2b69d 100644 --- a/pacfinder_test.go +++ b/pacfinder_darwin_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Alpaca Authors +// Copyright 2019, 2021 The Alpaca Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestFindPACURLForDarwin(t *testing.T) { +func TestFindPACURL(t *testing.T) { dir, err := ioutil.TempDir("", "alpaca") require.NoError(t, err) defer os.RemoveAll(dir) @@ -73,46 +73,18 @@ fi exit 0` require.NoError(t, ioutil.WriteFile(tmpfn, []byte(mockcmd), 0700)) - pacURL, err := findPACURLForDarwin() + pacURL, err := findPACURL() require.NoError(t, err) assert.Equal(t, "http://internal.anz.com/proxy.pac", pacURL) } -func TestFindPACURLForDarwinWhenNetworkSetupIsntAvailable(t *testing.T) { +func TestFindPACURLWhenNetworkSetupIsntAvailable(t *testing.T) { dir, err := ioutil.TempDir("", "alpaca") require.NoError(t, err) defer os.RemoveAll(dir) oldpath := os.Getenv("PATH") defer require.NoError(t, os.Setenv("PATH", oldpath)) require.NoError(t, os.Setenv("PATH", dir)) - _, err = findPACURLForDarwin() - require.NotNil(t, err) -} - -func TestFindPACURLForGNOME(t *testing.T) { - dir, err := ioutil.TempDir("", "alpaca") - require.NoError(t, err) - defer os.RemoveAll(dir) - oldpath := os.Getenv("PATH") - defer require.NoError(t, os.Setenv("PATH", oldpath)) - - require.NoError(t, os.Setenv("PATH", dir)) - tmpfn := filepath.Join(dir, "gsettings") - mockcmd := "#!/bin/sh\necho \\'http://internal.anz.com/proxy.pac\\'\n" - require.NoError(t, ioutil.WriteFile(tmpfn, []byte(mockcmd), 0700)) - - pacURL, err := findPACURLForGNOME() - require.NoError(t, err) - assert.Equal(t, "http://internal.anz.com/proxy.pac", pacURL) -} - -func TestFindPACURLForGNOMEWhenGsettingsIsntAvailable(t *testing.T) { - dir, err := ioutil.TempDir("", "alpaca") - require.NoError(t, err) - defer os.RemoveAll(dir) - oldpath := os.Getenv("PATH") - defer require.NoError(t, os.Setenv("PATH", oldpath)) - require.NoError(t, os.Setenv("PATH", dir)) - _, err = findPACURLForGNOME() + _, err = findPACURL() require.NotNil(t, err) } diff --git a/pacfinder_unix.go b/pacfinder_unix.go new file mode 100644 index 0000000..552e63b --- /dev/null +++ b/pacfinder_unix.go @@ -0,0 +1,33 @@ +// Copyright 2019, 2021 The Alpaca Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build aix dragonfly freebsd linux netbsd openbsd solaris + +package main + +import ( + "os/exec" + "strings" +) + +func findPACURL() (string, error) { + // Hopefully Linux, FreeBSD, Solaris, etc. will have GNOME 3 installed... + // TODO: Figure out how to do this for KDE. + cmd := exec.Command("gsettings", "get", "org.gnome.system.proxy", "autoconfig-url") + out, err := cmd.Output() + if err != nil { + return "", err + } + return strings.Trim(string(out), "'\n"), nil +} diff --git a/pacfinder_unix_test.go b/pacfinder_unix_test.go new file mode 100644 index 0000000..81d7051 --- /dev/null +++ b/pacfinder_unix_test.go @@ -0,0 +1,56 @@ +// Copyright 2019, 2021 The Alpaca Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build aix dragonfly freebsd linux netbsd openbsd solaris + +package main + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + + +func TestFindPACURL(t *testing.T) { + dir, err := ioutil.TempDir("", "alpaca") + require.NoError(t, err) + defer os.RemoveAll(dir) + oldpath := os.Getenv("PATH") + defer require.NoError(t, os.Setenv("PATH", oldpath)) + + require.NoError(t, os.Setenv("PATH", dir)) + tmpfn := filepath.Join(dir, "gsettings") + mockcmd := "#!/bin/sh\necho \\'http://internal.anz.com/proxy.pac\\'\n" + require.NoError(t, ioutil.WriteFile(tmpfn, []byte(mockcmd), 0700)) + + pacURL, err := findPACURL() + require.NoError(t, err) + assert.Equal(t, "http://internal.anz.com/proxy.pac", pacURL) +} + +func TestFindPACURLWhenGsettingsIsntAvailable(t *testing.T) { + dir, err := ioutil.TempDir("", "alpaca") + require.NoError(t, err) + defer os.RemoveAll(dir) + oldpath := os.Getenv("PATH") + defer require.NoError(t, os.Setenv("PATH", oldpath)) + require.NoError(t, os.Setenv("PATH", dir)) + _, err = findPACURL() + require.NotNil(t, err) +} diff --git a/pacfinder_windows.go b/pacfinder_windows.go new file mode 100644 index 0000000..597f8ab --- /dev/null +++ b/pacfinder_windows.go @@ -0,0 +1,20 @@ +// Copyright 2019, 2021 The Alpaca Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +func findPACURL() (string, error) { + // TODO: Implement this. + return "", nil +}