Skip to content

Commit

Permalink
Split pacfinder(_test).go into versions for darwin/unix/windows
Browse files Browse the repository at this point in the history
The tests currently fail on Windows due to the darwin and unix code not
working on Windows. This change fixes that.
  • Loading branch information
samuong committed May 23, 2021
1 parent 66772fd commit a06ea93
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 62 deletions.
30 changes: 1 addition & 29 deletions pacfinder.go → pacfinder_darwin.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
38 changes: 5 additions & 33 deletions pacfinder_test.go → pacfinder_darwin_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
33 changes: 33 additions & 0 deletions pacfinder_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
56 changes: 56 additions & 0 deletions pacfinder_unix_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions pacfinder_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a06ea93

Please sign in to comment.