Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: setup issue osx failing #140

Merged
merged 8 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ start:

tools:
@(go install golang.org/x/lint/golint)
# @(go get github.com/golang/lint)

fmt: tools
@(echo "${OK_COLOR}Running fmt ...${NO_COLOR}")
Expand All @@ -66,7 +65,7 @@ vet: tools
go vet ./...

lint: tools
@(echo "${OK_COLOR}Running lint ...${NO_COLOR}")
@(echo "Running lint ...")
@(export PATH=$$PATH:$$GOPATH/bin && [ $$(golint ./... | wc -l) != 0 ] && \
echo "${WARN_COLOR}Lint says the following files are not ok:${NO_COLOR}" && \
echo "${ERROR_COLOR}" && golint ./... && \
Expand Down
10 changes: 6 additions & 4 deletions commands/setup/linux_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ type LinuxConfigurator struct{}

// SetUp is responsible for setting up the ergo as proxy
func (c *LinuxConfigurator) SetUp(proxyURL string) error {
err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'auto'")
_, err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'auto'")

if err != nil {
return err
}

return RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", `'`+proxyURL+`'`)
_, err = RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", `'`+proxyURL+`'`)
return err
}

// SetDown is responsible for remove the ergo as proxy
func (c *LinuxConfigurator) SetDown() error {
err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'none'")
_, err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'none'")

if err != nil {
return err
}

return RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", "''")
_, err = RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", "''")
return err
}
60 changes: 58 additions & 2 deletions commands/setup/osx_configurator.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
package setup

import (
"fmt"
"strconv"
"strings"
)

// OSXConfigurator implements Configurator for windows
type OSXConfigurator struct{}

const osxVersionThatSupportsSetupCommand = 10 // up to Catalina

func checkSupportedVersion() error {
cmd := `sw_vers -productVersion`
output, err := RunnerDefault.Run("/bin/sh", "-c", cmd)
if err != nil {
fmt.Println(err)
return fmt.Errorf("checking the current osx version failed")
}

var majorVersionNumber int
outputString := string(output)
if outputString == "" {
return fmt.Errorf("checking the current osx version failed")
}

if strings.Contains(outputString, ".") {
majorVersion := strings.Split(string(output), ".")[0]
majorVersionNumber, err = strconv.Atoi(majorVersion)
} else {
majorVersionNumber, err = strconv.Atoi(outputString)
}

if err != nil {
fmt.Println(err)
return fmt.Errorf("checking the current osx version failed")
}

if majorVersionNumber > osxVersionThatSupportsSetupCommand {
fmt.Println("The ergo setup is not supported for the current osx version.")
fmt.Println("Supported versions Catalina or below.")
fmt.Println("Please, consider setting up ergo as proxy manually.")
return fmt.Errorf("unsupported osx version")
}

return nil
}

// SetUp is responsible for setting up the ergo as proxy
func (c *OSXConfigurator) SetUp(proxyURL string) error {
return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`)
err := checkSupportedVersion()
if err != nil {
return err
}

_, err = RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`)
return err
}

// SetDown is responsible for remove the ergo as proxy
func (c *OSXConfigurator) SetDown() error {
return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`)
err := checkSupportedVersion()
if err != nil {
return err
}

_, err = RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`)
return err
}
6 changes: 3 additions & 3 deletions commands/setup/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (

// Runner is a process runner
type Runner interface {
Run(string, ...string) error
Run(string, ...string) ([]byte, error)
}

// DefaultRunner implements the default runner for ergo
type DefaultRunner struct{}

// Run a given command script
func (r DefaultRunner) Run(command string, args ...string) error {
func (r DefaultRunner) Run(command string, args ...string) ([]byte, error) {
out, err := exec.Command(command, args...).Output()
if len(out) != 0 {
fmt.Printf("Command: %s\n", out)
}
return err
return out, err
}

// RunnerDefault run given commands
Expand Down
4 changes: 2 additions & 2 deletions commands/setup/windows_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type WindowsConfigurator struct{}

// SetUp is responsible for setting up the ergo as proxy
func (c *WindowsConfigurator) SetUp(proxyURL string) error {
err := RunnerDefault.Run(
_, err := RunnerDefault.Run(
`reg`, ` add HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings /v AutoConfigURL /t REG_SZ /d `+proxyURL+` /f`)

InetRefresh()
Expand All @@ -15,7 +15,7 @@ func (c *WindowsConfigurator) SetUp(proxyURL string) error {

// SetDown is responsible for remove the ergo as proxy
func (c *WindowsConfigurator) SetDown() error {
err := RunnerDefault.Run(
_, err := RunnerDefault.Run(
`reg`, ` delete HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings /v AutoConfigURL /f`)

InetRefresh()
Expand Down
73 changes: 61 additions & 12 deletions commands/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type TestRunner struct {
History string
}

func (r *TestRunner) Run(command string, args ...string) error {
func (r *TestRunner) Run(command string, args ...string) ([]byte, error) {
if r.ExpectToInclude == "" {
fmt.Println("No expectation")
return nil
return []byte{}, nil
}

r.History = r.History + " > " + command + " " + strings.Join(args, " ")
Expand All @@ -44,7 +44,31 @@ func (r *TestRunner) Run(command string, args ...string) error {
)
}

return nil
return []byte{}, nil
}

type TestRunnerWithOutput struct {
Test *testing.T
History string
MockedOutput map[string][]byte
}

func (r *TestRunnerWithOutput) Mock(command string, output []byte) {
// parse command to a unique key
key := strings.Join(strings.Split(command, " "), "_")
r.MockedOutput[key] = output
}

func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) {
commandWithArgs := command + " " + strings.Join(args, " ")
key := strings.Join(strings.Split(commandWithArgs, " "), "_")
mockedOutput, ok := r.MockedOutput[key]
if !ok {
r.Test.Fatalf("No more expectation for command %s", commandWithArgs)
return []byte{}, nil
}

return mockedOutput, nil
}

func TestSetupLinuxGnome(t *testing.T) {
Expand Down Expand Up @@ -99,7 +123,6 @@ func TestSetupLinuxGnome(t *testing.T) {
}

testRunner := &TestRunner{}

for _, c := range cases {
t.Run(c.Title, func(tt *testing.T) {
testRunner.Test = tt
Expand Down Expand Up @@ -128,19 +151,24 @@ func TestSetupOSX(t *testing.T) {
}{
{
Title: "expect to set networking proxy pac url",
CommandExpectToInclude: `-setautoproxyurl "Wi-Fi" "` + config.GetProxyPacURL() + `"`,
CommandExpectToInclude: `networksetup -setautoproxyurl "Wi-Fi" "` + config.GetProxyPacURL() + `"`,
},
}

for _, c := range cases {
t.Run(c.Title, func(tt *testing.T) {
setup.RunnerDefault = &TestRunner{
Test: tt,
ExpectToInclude: c.CommandExpectToInclude,
mockedRunner := &TestRunnerWithOutput{
Test: tt,
MockedOutput: map[string][]byte{},
}

mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6"))
mockedRunner.Mock(c.CommandExpectToInclude, []byte{})

setup.RunnerDefault = mockedRunner
command := SetupCommand{System: "osx", Remove: false}
_, err := command.Execute(config)

if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -155,17 +183,21 @@ func TestSetupOSX(t *testing.T) {
}{
{
Title: "expect to set networking wi-fi to none",
CommandExpectToInclude: `-setautoproxyurl "Wi-Fi" ""`,
CommandExpectToInclude: `networksetup -setautoproxyurl "Wi-Fi" ""`,
},
}

for _, c := range cases {
t.Run(c.Title, func(tt *testing.T) {
setup.RunnerDefault = &TestRunner{
Test: tt,
ExpectToInclude: c.CommandExpectToInclude,
mockedRunner := &TestRunnerWithOutput{
Test: tt,
MockedOutput: map[string][]byte{},
}

mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6"))
mockedRunner.Mock(c.CommandExpectToInclude, []byte{})
setup.RunnerDefault = mockedRunner

command := SetupCommand{System: "osx", Remove: true}
_, err := command.Execute(config)
if err != nil {
Expand All @@ -174,6 +206,23 @@ func TestSetupOSX(t *testing.T) {
})
}
})

t.Run("it does not work after Catalina version * > 10", func(tt *testing.T) {
mockedRunner := &TestRunnerWithOutput{
Test: tt,
MockedOutput: map[string][]byte{},
}

mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("11.11.6"))
setup.RunnerDefault = mockedRunner

command := SetupCommand{System: "osx", Remove: true}
_, err := command.Execute(config)
// check if the error message contains Setup failed
if !strings.Contains(err.Error(), "Setup failed cause unsupported osx version") {
t.Fatalf(err.Error())
}
})
}

func TestSetupWindows(t *testing.T) {
Expand Down
37 changes: 35 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestShowUsage(t *testing.T) {

type TestRunner struct{}

func (r *TestRunner) Run(command string, args ...string) error {
return nil
func (r *TestRunner) Run(command string, args ...string) ([]byte, error) {
return []byte(""), nil
}

func TestListCommand(t *testing.T) {
Expand Down Expand Up @@ -152,6 +152,31 @@ func TestListNamesCommand(t *testing.T) {
})
}

type TestRunnerWithOutput struct {
OriginalRunner setup.Runner
Test *testing.T
History string
MockedOutput map[string][]byte
}

func (r *TestRunnerWithOutput) Mock(command string, output []byte) {
// parse command to a unique key
key := strings.Join(strings.Split(command, " "), "_")
r.MockedOutput[key] = output
}

func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) {
commandWithArgs := command + " " + strings.Join(args, " ")
key := strings.Join(strings.Split(commandWithArgs, " "), "_")

mockedOutput, ok := r.MockedOutput[key]
if !ok {
return r.OriginalRunner.Run(command, args...)
}

return mockedOutput, nil
}

func TestSetupCommand(t *testing.T) {
setup.RunnerDefault = &TestRunner{}

Expand Down Expand Up @@ -200,6 +225,14 @@ func TestSetupCommand(t *testing.T) {
t.Fatal("Expected result to not be nil")
}

mockedRunner := &TestRunnerWithOutput{
OriginalRunner: setup.RunnerDefault,
Test: tt,
MockedOutput: map[string][]byte{},
}
mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6"))
setup.RunnerDefault = mockedRunner

setup := command.(commands.SetupCommand)

output, err := setup.Execute(config)
Expand Down