refactored so there would be one IsNonEmptyDir check instead of an IsDir check and an IsEmptyDir check

This commit is contained in:
James Smith 2017-02-10 15:42:02 -05:00 коммит произвёл Edward Muller
Родитель 95b114ff2c
Коммит 4ad9f4ec24
3 изменённых файлов: 15 добавлений и 16 удалений

Просмотреть файл

@ -223,17 +223,11 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
// check if vendor exists, because if the locks are the same but
// vendor does not exist we should write vendor
var writeV bool
path := filepath.Join(sw.Root, "vendor")
vendorIsDir, err := dep.IsDir(path)
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(sw.Root, "vendor"))
if err != nil {
return errors.Wrap(err, "ensure vendor is a directory")
}
vendorEmpty, _ := dep.IsEmptyDir(path)
vendorExists := vendorIsDir && !vendorEmpty
if !vendorExists && solution != nil {
writeV = true
}
writeV := !vendorExists && solution != nil
return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
}

9
fs.go
Просмотреть файл

@ -45,12 +45,17 @@ func IsDir(name string) (bool, error) {
return true, nil
}
func IsEmptyDir(name string) (bool, error) {
func IsNonEmptyDir(name string) (bool, error) {
isDir, err := IsDir(name)
if !isDir || err != nil {
return isDir, err
}
files, err := ioutil.ReadDir(name)
if err != nil {
return false, err
}
return len(files) == 0, nil
return len(files) != 0, nil
}
func writeFile(path string, in json.Marshaler) error {

Просмотреть файл

@ -192,15 +192,15 @@ func TestIsEmpty(t *testing.T) {
h := test.NewHelper(t)
h.TempDir("empty")
tests := map[string]string{
wd: "false",
"_testdata": "false",
filepath.Join(wd, "main.go"): "err",
filepath.Join(wd, "this_file_does_not_exist.thing"): "err",
h.Path("empty"): "true",
wd: "true",
"_testdata": "true",
filepath.Join(wd, "fs.go"): "err",
filepath.Join(wd, "this_file_does_not_exist.thing"): "false",
h.Path("empty"): "false",
}
for f, want := range tests {
empty, err := IsEmptyDir(f)
empty, err := IsNonEmptyDir(f)
if want == "err" {
if err == nil {
t.Fatalf("Wanted an error for %v, but it was nil", f)