Skip to content

Commit 408c6bc

Browse files
committed
Update homebrew pkg
1 parent 9b4b124 commit 408c6bc

File tree

2 files changed

+94
-124
lines changed

2 files changed

+94
-124
lines changed

internal/homebrew/homebrew.go

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package homebrew
1616

1717
import (
1818
"bytes"
19+
"fmt"
1920
"os"
2021
"os/exec"
2122
"path/filepath"
@@ -29,9 +30,24 @@ import (
2930

3031
//go:generate mockgen -destination=../mocks/mock_brew_store.go -package=mocks github.com/mongodb/mongocli/internal/homebrew PathStore
3132

32-
const atlasFormulaName = "mongodb-atlas"
33+
const (
34+
atlasFormulaName = "mongodb-atlas"
35+
brewFileSubPath = "/brew.yaml"
36+
)
37+
38+
type Checker struct {
39+
path string
40+
fs afero.Fs
41+
}
42+
43+
func NewChecker(fileSystem afero.Fs) (*Checker, error) {
44+
filePath, err := config.Path(brewFileSubPath)
45+
if err != nil {
46+
return nil, err
47+
}
48+
return &Checker{fs: fileSystem, path: filePath}, nil
49+
}
3350

34-
// formulaName get homebrew suitable command for a given tool.
3551
func formulaName(tool string) string {
3652
if strings.Contains(tool, "atlas") {
3753
return atlasFormulaName
@@ -41,36 +57,38 @@ func formulaName(tool string) string {
4157

4258
// IsHomebrew checks if the cli was installed with homebrew.
4359
func (s Checker) IsHomebrew() bool {
44-
h, err := s.Load()
45-
// If one of the values was not found previously it is still a valid case - rely on the file.
46-
if (h.ExecutablePath != "" || h.FormulaPath != "") && err == nil {
60+
// Load from cache
61+
h, err := s.load()
62+
if h != nil && h.ExecutablePath != "" && h.FormulaPath != "" && err == nil {
4763
return strings.HasPrefix(h.ExecutablePath, h.FormulaPath)
4864
}
65+
4966
formula := formulaName(config.BinName())
50-
buf := new(bytes.Buffer)
51-
cmd := exec.Command("brew", "--prefix", "--installed", formula)
67+
cmdResult := new(bytes.Buffer)
68+
cmd := exec.Command("brew", "--prefix", formula)
5269

53-
cmd.Stdout = buf
54-
if err := cmd.Start(); err != nil {
70+
if err = cmd.Start(); err != nil {
5571
return false
5672
}
5773

58-
executablePath, err := executableCurrentPath()
74+
h = new(homebrew)
75+
h.ExecutablePath, err = executableCurrentPath()
5976
if err != nil {
6077
return false
6178
}
62-
h.ExecutablePath = executablePath
6379

64-
if err := cmd.Wait(); err != nil {
80+
if err = cmd.Wait(); err != nil {
6581
return false
6682
}
67-
brewFormulaPath, err := filepath.EvalSymlinks(strings.TrimSpace(buf.String()))
83+
84+
h.FormulaPath, err = filepath.EvalSymlinks(strings.TrimSpace(cmdResult.String()))
6885
if err != nil {
86+
fmt.Println("formula")
87+
fmt.Println(err)
6988
return false
7089
}
71-
h.FormulaPath = brewFormulaPath
72-
_ = s.Save(h)
73-
return strings.HasPrefix(executablePath, brewFormulaPath)
90+
_ = s.save(h)
91+
return strings.HasPrefix(h.ExecutablePath, h.FormulaPath)
7492
}
7593

7694
func executableCurrentPath() (string, error) {
@@ -87,32 +105,7 @@ type homebrew struct {
87105
FormulaPath string `yaml:"formula_path"`
88106
}
89107

90-
type Loader interface {
91-
Load() (*homebrew, error)
92-
}
93-
94-
type LoaderSaver interface {
95-
Loader
96-
Save(*homebrew) error
97-
}
98-
99-
func NewChecker(fileSystem afero.Fs) (*Checker, error) {
100-
filePath, err := config.Path(brewFileSubPath)
101-
if err != nil {
102-
return nil, err
103-
}
104-
return &Checker{fs: fileSystem, path: filePath}, nil
105-
}
106-
107-
type Checker struct {
108-
path string
109-
fs afero.Fs
110-
}
111-
112-
const brewFileSubPath = "/brew.yaml"
113-
114-
// Load will load the latest calculated brew path.
115-
func (s *Checker) Load() (*homebrew, error) {
108+
func (s *Checker) load() (*homebrew, error) {
116109
path := new(homebrew)
117110
if err := file.Load(s.fs, s.path, path); err != nil {
118111
return nil, err
@@ -124,7 +117,7 @@ func (s *Checker) Load() (*homebrew, error) {
124117
return nil, nil
125118
}
126119

127-
// Save will save the latest calculated brew path.
128-
func (s *Checker) Save(h *homebrew) error {
120+
func (s *Checker) save(h *homebrew) error {
121+
h.CheckedAt = time.Now()
129122
return file.Save(s.fs, s.path, h)
130123
}

internal/homebrew/homebrew_test.go

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,75 @@ package homebrew
1919

2020
import (
2121
"fmt"
22-
"github.com/golang/mock/gomock"
23-
"github.com/mongodb/mongocli/internal/mocks"
2422
"testing"
23+
"time"
2524

26-
"github.com/mongodb/mongocli/internal/config"
2725
"github.com/spf13/afero"
2826
)
2927

30-
func TestFile(t *testing.T) {
31-
t.Run("save brewpath mcli", func(t *testing.T) {
32-
appFS := afero.NewMemMapFs()
33-
s := NewLoaderSaver(appFS, "mongocli")
34-
35-
err := s.SaveBrewPath("a/b/c", "d/e/f")
36-
if err != nil {
37-
t.Errorf("LoadLatestVersion() unexpected error: %v", err)
38-
}
39-
})
40-
t.Run("load brewpath mcli", func(t *testing.T) {
41-
appFS := afero.NewMemMapFs()
42-
s := NewLoaderSaver(appFS, "mongocli")
43-
44-
path, _ := config.Path(brewFileSubPath)
45-
_ = afero.WriteFile(appFS, path, []byte(""), 0600)
46-
47-
p1, p2, err := s.LoadBrewPath()
48-
if err != nil || p1 != "" || p2 != "" {
49-
t.Errorf("LoadLatestVersion() unexpected error: %v", err)
50-
}
51-
})
52-
t.Run("load brewpath mcli is empty", func(t *testing.T) {
53-
appFS := afero.NewMemMapFs()
54-
s := NewLoaderSaver(appFS, "mongocli")
55-
56-
_, _, err := s.LoadBrewPath()
57-
if err == nil {
58-
t.Errorf("LoadLatestVersion() expected error: file not found")
59-
}
60-
})
61-
t.Run("save brewpath atlascli", func(t *testing.T) {
62-
appFS := afero.NewMemMapFs()
63-
s := NewLoaderSaver(appFS, "atlascli")
64-
65-
err := s.SaveBrewPath("a/b/c", "d/e/f")
66-
if err != nil {
67-
t.Errorf("LoadLatestVersion() unexpected error: %v", err)
68-
}
69-
})
70-
t.Run("load brewpath atlascli", func(t *testing.T) {
71-
appFS := afero.NewMemMapFs()
72-
s := NewLoaderSaver(appFS, "atlascli")
73-
74-
path, _ := config.Path(brewFileSubPath)
75-
_ = afero.WriteFile(appFS, path, []byte(""), 0600)
76-
77-
p1, p2, err := s.LoadBrewPath()
78-
if err != nil || p1 != "" || p2 != "" {
79-
t.Errorf("LoadLatestVersion() unexpected error: %v", err)
80-
}
81-
})
82-
t.Run("load brewpath atlascli is empty", func(t *testing.T) {
83-
appFS := afero.NewMemMapFs()
84-
s := NewLoaderSaver(appFS, "atlascli")
85-
86-
_, _, err := s.LoadBrewPath()
87-
if err == nil {
88-
t.Errorf("LoadLatestVersion() expected error: file not found")
89-
}
90-
})
91-
}
92-
93-
func TestOutputOpts_testIsHomebrew(t *testing.T) {
28+
func TestChecker_IsHomebrew(t *testing.T) {
9429
tests := []struct {
95-
tool string
96-
isHb bool
30+
paths *homebrew
31+
isHomebrew bool
9732
}{
98-
{"atlascli", false},
99-
{"mongocli", false},
33+
{
34+
paths: &homebrew{
35+
CheckedAt: time.Now(),
36+
ExecutablePath: "/workplace/mongocli/bin/mongocli",
37+
FormulaPath: "/opt/homebrew/Cellar/mongocli/1.22.0",
38+
},
39+
isHomebrew: false,
40+
},
41+
{
42+
paths: &homebrew{
43+
CheckedAt: time.Now(),
44+
ExecutablePath: "",
45+
FormulaPath: "/opt/homebrew/Cellar/mongocli/1.22.0",
46+
},
47+
isHomebrew: false,
48+
},
49+
{
50+
paths: &homebrew{
51+
CheckedAt: time.Now(),
52+
ExecutablePath: "/workplace/mongocli/bin/mongocli",
53+
FormulaPath: "",
54+
},
55+
isHomebrew: false,
56+
},
57+
{
58+
paths: &homebrew{
59+
CheckedAt: time.Now(),
60+
ExecutablePath: "/workplace/mongocli/bin/mongocli",
61+
FormulaPath: ".",
62+
},
63+
isHomebrew: false,
64+
},
65+
{
66+
paths: &homebrew{
67+
CheckedAt: time.Now(),
68+
ExecutablePath: "/opt/homebrew/Cellar/mongocli/1.22.0/bin",
69+
FormulaPath: "/opt/homebrew/Cellar/mongocli/1.22.0",
70+
},
71+
isHomebrew: true,
72+
},
10073
}
10174

10275
for _, tt := range tests {
103-
t.Run(fmt.Sprintf("%v_ishomebrew_%v", tt.tool, tt.isHb), func(t *testing.T) {
104-
ctrl := gomock.NewController(t)
105-
mockStore := mocks.NewMockPathStore(ctrl)
106-
defer ctrl.Finish()
76+
t.Run(fmt.Sprintf("path:%v/formula:%v", tt.paths.ExecutablePath, tt.paths.FormulaPath), func(t *testing.T) {
77+
appFS := afero.NewMemMapFs()
78+
c, err := NewChecker(appFS)
79+
if err != nil {
80+
t.Errorf("NewChecker() unexpected error: %v", err)
81+
}
10782

108-
mockStore.EXPECT().LoadBrewPath().Return("", "", nil)
109-
mockStore.EXPECT().SaveBrewPath(gomock.Any(), gomock.Any()).Return(nil)
83+
err = c.save(tt.paths)
84+
if err != nil {
85+
t.Errorf("save() unexpected error: %v", err)
86+
}
11087

111-
result := IsHomebrew(mockStore)
112-
if result != tt.isHb {
113-
t.Errorf("got = %v, want %v", result, tt.isHb)
88+
result := c.IsHomebrew()
89+
if result != tt.isHomebrew {
90+
t.Errorf("got = %v, want %v", result, tt.isHomebrew)
11491
}
11592
})
11693
}

0 commit comments

Comments
 (0)