Skip to content

Commit

Permalink
Merge pull request hashicorp#82 from hashicorp/jbardin/glob-no-match
Browse files Browse the repository at this point in the history
cover the case where a subdir glob doesn't match
  • Loading branch information
jbardin authored Sep 22, 2017
2 parents 56c651a + 5ff8f8b commit a686900
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func SubdirGlob(dst, subDir string) (string, error) {
if err != nil {
return "", err
}

if len(matches) == 0 {
return "", fmt.Errorf("subdir %q not found", subDir)
}

if len(matches) > 1 {
return "", fmt.Errorf("subdir %q matches multiple paths", subDir)
}
Expand Down
55 changes: 55 additions & 0 deletions source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package getter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -41,3 +44,55 @@ func TestSourceDirSubdir(t *testing.T) {
}
}
}

func TestSourceSubdirGlob(t *testing.T) {
td, err := ioutil.TempDir("", "subdir-glob")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)

if err := os.Mkdir(filepath.Join(td, "subdir"), 0755); err != nil {
t.Fatal(err)
}

if err := os.Mkdir(filepath.Join(td, "subdir/one"), 0755); err != nil {
t.Fatal(err)
}

if err := os.Mkdir(filepath.Join(td, "subdir/two"), 0755); err != nil {
t.Fatal(err)
}

subdir := filepath.Join(td, "subdir")

// match the exact directory
res, err := SubdirGlob(td, "subdir")
if err != nil {
t.Fatal(err)
}
if res != subdir {
t.Fatalf(`expected "subdir", got: %q`, subdir)
}

// single match from a wildcard
res, err = SubdirGlob(td, "*")
if err != nil {
t.Fatal(err)
}
if res != subdir {
t.Fatalf(`expected "subdir", got: %q`, subdir)
}

// multiple matches
res, err = SubdirGlob(td, "subdir/*")
if err == nil {
t.Fatalf("expected multiple matches, got %q", res)
}

// non-existent
res, err = SubdirGlob(td, "foo")
if err == nil {
t.Fatalf("expected no matches, got %q", res)
}
}

0 comments on commit a686900

Please sign in to comment.