Skip to content

Commit cf39fbe

Browse files
author
Massimiliano Pippi
authored
tentatively optimize searchConfigTree (#515)
1 parent e164137 commit cf39fbe

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Diff for: cli/cli.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24-
"path"
2524
"path/filepath"
2625
"strings"
2726

@@ -124,15 +123,19 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
124123
func searchConfigTree(cwd string) string {
125124
// go back up to root and search for the config file
126125
for {
127-
if _, err := os.Stat(path.Join(cwd, "arduino-cli.yaml")); os.IsNotExist(err) {
126+
if _, err := os.Stat(filepath.Join(cwd, "arduino-cli.yaml")); err == nil {
127+
// config file found
128+
return cwd
129+
} else if os.IsNotExist(err) {
128130
// no config file found
129-
next := path.Join(cwd, "..")
130-
if filepath.Clean(next) == filepath.Clean(cwd) {
131+
next := filepath.Dir(cwd)
132+
if next == cwd {
131133
return ""
132134
}
133135
cwd = next
134136
} else {
135-
return cwd
137+
// some error we can't handle happened
138+
return ""
136139
}
137140
}
138141
}

Diff for: cli/cli_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,18 @@ func TestSearchConfigTreeInParent(t *testing.T) {
513513
require.Nil(t, err)
514514
require.Equal(t, searchConfigTree(target), tmp)
515515
}
516+
517+
var result string
518+
519+
func BenchmarkSearchConfigTree(b *testing.B) {
520+
tmp := tmpDirOrDie()
521+
defer os.RemoveAll(tmp)
522+
target := filepath.Join(tmp, "foo", "bar", "baz")
523+
os.MkdirAll(target, os.ModePerm)
524+
525+
var s string
526+
for n := 0; n < b.N; n++ {
527+
s = searchConfigTree(target)
528+
}
529+
result = s
530+
}

0 commit comments

Comments
 (0)