-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring
- Loading branch information
Showing
7 changed files
with
354 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dirlist | ||
|
||
var ( | ||
dList List | ||
) | ||
|
||
func init() { | ||
dList = New() | ||
} | ||
|
||
func Reset() { dList.Reset() } | ||
|
||
func Contains(p string) bool { | ||
return dList.Contains(quoteAndClean(p)) | ||
} | ||
|
||
func Load(s string) { | ||
dList.Load(s) | ||
} | ||
|
||
func LoadEnv(s string) { | ||
dList.LoadEnv(s) | ||
} | ||
|
||
func Prepend(p string) { | ||
dList.Prepend(p) | ||
} | ||
|
||
func Append(p string) { | ||
dList.Append(p) | ||
} | ||
|
||
func Drop(p string) { | ||
dList.Drop(p) | ||
} | ||
|
||
func Slice() []string { | ||
return dList.Slice() | ||
} | ||
|
||
func String() string { | ||
return dList.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package dirlist | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAppend(t *testing.T) { | ||
Reset() | ||
require.Equal(t, "", String()) | ||
for _, p := range []string{"/var", "/var", "/bin", "/bin/", "/bin///"} { | ||
Append(p) | ||
} | ||
|
||
require.Equal(t, "/var:/bin", String()) | ||
Prepend("/bin///") | ||
require.Equal(t, "/var:/bin", String()) | ||
} | ||
|
||
func TestContains(t *testing.T) { | ||
Reset() | ||
Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var:/bin") | ||
require.False(t, Contains("/ur/local/sbin")) | ||
require.False(t, Contains("/ur/local////sbin/")) | ||
require.True(t, Contains("/sbin")) | ||
require.True(t, Contains("///sbin//")) | ||
|
||
} | ||
|
||
func TestDrop(t *testing.T) { | ||
Reset() | ||
Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var:/bin") | ||
require.Equal(t, Slice(), []string{"/opt/local/bin", "/usr/local/bin", "/sbin", "/bin", "/var"}) | ||
Drop("/opt/local/bin") | ||
Drop("/opt/local/bin") | ||
Drop("/opt/local/bin") | ||
Drop("/usr/local/bin") | ||
Drop("/var") | ||
require.NotEqual(t, "", String()) | ||
Drop("/sbin") | ||
Drop("/bin") | ||
require.False(t, Contains("/bin")) | ||
require.Equal(t, "", String()) | ||
|
||
Reset() | ||
require.NotPanics(t, func() { Drop("") }) | ||
} | ||
|
||
func TestLoadEnv(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
val string | ||
want string | ||
}{ | ||
{"empty", "", ""}, | ||
} | ||
for i, tt := range tests { | ||
tt2 := tt | ||
t.Run(tt2.name, func(t *testing.T) { | ||
envvar := fmt.Sprintf("%s_%d_VAR", t.Name(), i) | ||
Reset() | ||
t.Setenv(envvar, tt.val) | ||
LoadEnv(envvar) | ||
require.Equal(t, tt2.want, String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestPrepend(t *testing.T) { | ||
Reset() | ||
require.Equal(t, "", String()) | ||
for _, p := range []string{"/var", "/var", "/bin", "/bin/", "/bin///"} { | ||
Append(p) | ||
} | ||
|
||
require.Equal(t, "/var:/bin", String()) | ||
Prepend("/bin///") | ||
require.Equal(t, "/var:/bin", String()) | ||
} | ||
|
||
func TestSlice(t *testing.T) { | ||
Reset() | ||
require.Equal(t, 0, len(Slice())) | ||
Prepend("/usr/bin") | ||
Append("/bin") | ||
require.Equal(t, []string{"/usr/bin", "/bin"}, Slice()) | ||
} | ||
|
||
func TestString(t *testing.T) { | ||
Reset() | ||
require.Equal(t, "", String()) | ||
Prepend("/usr/bin") | ||
Append("/bin") | ||
require.Equal(t, "/usr/bin:/bin", String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.