Skip to content

Commit

Permalink
Unit-tests oh joy! Adding some much needed tests. These will get bett…
Browse files Browse the repository at this point in the history
…er and more robust
  • Loading branch information
deckarep committed Jan 6, 2024
1 parent 6002e37 commit 64d1d59
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/remote_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -147,7 +148,7 @@ func ExecuteClusterRemoteCmd(ctx context.Context, w io.Writer, hosts []RemoteCmd
}

func executeRemoteCmd(ctx context.Context, idx int, host string, alias string, remoteCmd string, outputChan chan<- hostLine) error {
binPath, err := utils.SelectBinaryPath(binarySearchPathCandidates)
binPath, err := utils.SelectBinaryPath(runtime.GOOS, binarySearchPathCandidates)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/tailscale_cli/tailscale_cli_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package tailscale_cli

import (
"os/exec"
"runtime"
"strings"

"github.com/deckarep/tips/pkg/utils"
Expand Down Expand Up @@ -58,7 +59,7 @@ type DeviceInfo struct {
}

func GetVersion() (string, error) {
confirmedPath, err := utils.SelectBinaryPath(binarySearchPathCandidates)
confirmedPath, err := utils.SelectBinaryPath(runtime.GOOS, binarySearchPathCandidates)
if err != nil {
return "", err
}
Expand All @@ -73,7 +74,7 @@ func GetVersion() (string, error) {
}

func GetDevicesState() (map[string]DeviceInfo, error) {
confirmedPath, err := utils.SelectBinaryPath(binarySearchPathCandidates)
confirmedPath, err := utils.SelectBinaryPath(runtime.GOOS, binarySearchPathCandidates)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
"runtime"
)

func SelectBinaryPath(candidates map[string][]string) (string, error) {
func SelectBinaryPath(platform string, candidates map[string][]string) (string, error) {
osSelected := runtime.GOOS
if paths, exists := candidates[runtime.GOOS]; exists {
if paths, exists := candidates[platform]; exists {
for _, p := range paths {
if confirmedPath, err := exec.LookPath(p); err == nil {
return confirmedPath, nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
)

func TestSelectBinaryPath(t *testing.T) {
// TODO: this will fail on other platforms.
const (
tsPathBogus = "/Applications/Something/That/Does/Not/Exist"
tsPathDarwin = "/Applications/Tailscale.app/Contents/MacOS/Tailscale"
)

c := map[string][]string{
"darwin": {
tsPathBogus,
tsPathDarwin,
},
}

result, err := SelectBinaryPath(c)
result, err := SelectBinaryPath("darwin", c)
if err != nil {
t.Errorf("expected nil err, got: %s", err.Error())
}
Expand Down

0 comments on commit 64d1d59

Please sign in to comment.