Skip to content

Commit dd54fc3

Browse files
committed
Fix HTTP getter subdir paths to work properly
The tests were actually turning `foo//bar` into `foo/bar` so we have _never_ tested subdir access with HTTP. Turns out, it never worked! There was a bug that caused it to never work. This fixes that bug, adds more tests, and verifies globbing works, too.
1 parent ed2b33b commit dd54fc3

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

client.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,11 @@ func (c *Client) Get() error {
305305
return err
306306
}
307307

308-
// Subdir matching supports globs. This allows matching unknown
309-
// subdirectory structures but where we know the general path.
310-
// For example, for archives that have a root folder containing
311-
// all the items, you can download it with the getter URL of:
312-
// 'path//*'.
313-
matches, err := filepath.Glob(filepath.Join(dst, subDir))
308+
// Process any globs
309+
subDir, err := SubdirGlob(dst, subDir)
314310
if err != nil {
315311
return err
316312
}
317-
if len(matches) > 1 {
318-
return fmt.Errorf("subdir %q matches multiple paths", subDir)
319-
}
320-
subDir = matches[0]
321313

322314
return copyDir(realDst, subDir, false)
323315
}

get_http.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
9898
}
9999

100100
func (g *HttpGetter) GetFile(dst string, u *url.URL) error {
101-
102101
if g.Netrc {
103102
// Add auth from netrc if we can
104103
if err := addAuthFromNetrc(u); err != nil {
@@ -140,13 +139,22 @@ func (g *HttpGetter) getSubdir(dst, source, subDir string) error {
140139
}
141140
defer os.RemoveAll(td)
142141

142+
// We have to create a subdirectory that doesn't exist for the file
143+
// getter to work.
144+
td = filepath.Join(td, "data")
145+
143146
// Download that into the given directory
144147
if err := Get(td, source); err != nil {
145148
return err
146149
}
147150

151+
// Process any globbing
152+
sourcePath, err := SubdirGlob(td, subDir)
153+
if err != nil {
154+
return err
155+
}
156+
148157
// Make sure the subdir path actually exists
149-
sourcePath := filepath.Join(td, subDir)
150158
if _, err := os.Stat(sourcePath); err != nil {
151159
return fmt.Errorf(
152160
"Error downloading %s: %s", source, err)

get_http_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ func TestHttpGetter_metaSubdir(t *testing.T) {
8686
}
8787
}
8888

89+
func TestHttpGetter_metaSubdirGlob(t *testing.T) {
90+
ln := testHttpServer(t)
91+
defer ln.Close()
92+
93+
g := new(HttpGetter)
94+
dst := tempDir(t)
95+
96+
var u url.URL
97+
u.Scheme = "http"
98+
u.Host = ln.Addr().String()
99+
u.Path = "/meta-subdir-glob"
100+
101+
// Get it!
102+
if err := g.Get(dst, &u); err != nil {
103+
t.Fatalf("err: %s", err)
104+
}
105+
106+
// Verify the main file exists
107+
mainPath := filepath.Join(dst, "sub.tf")
108+
if _, err := os.Stat(mainPath); err != nil {
109+
t.Fatalf("err: %s", err)
110+
}
111+
}
112+
89113
func TestHttpGetter_none(t *testing.T) {
90114
ln := testHttpServer(t)
91115
defer ln.Close()
@@ -194,6 +218,7 @@ func testHttpServer(t *testing.T) net.Listener {
194218
mux.HandleFunc("/meta", testHttpHandlerMeta)
195219
mux.HandleFunc("/meta-auth", testHttpHandlerMetaAuth)
196220
mux.HandleFunc("/meta-subdir", testHttpHandlerMetaSubdir)
221+
mux.HandleFunc("/meta-subdir-glob", testHttpHandlerMetaSubdirGlob)
197222

198223
var server http.Server
199224
server.Handler = mux
@@ -234,6 +259,10 @@ func testHttpHandlerMetaSubdir(w http.ResponseWriter, r *http.Request) {
234259
w.Write([]byte(fmt.Sprintf(testHttpMetaStr, testModuleURL("basic//subdir").String())))
235260
}
236261

262+
func testHttpHandlerMetaSubdirGlob(w http.ResponseWriter, r *http.Request) {
263+
w.Write([]byte(fmt.Sprintf(testHttpMetaStr, testModuleURL("basic//sub*").String())))
264+
}
265+
237266
func testHttpHandlerNone(w http.ResponseWriter, r *http.Request) {
238267
w.Write([]byte(testHttpNoneStr))
239268
}

module_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ func testModule(n string) string {
4040
}
4141

4242
func testModuleURL(n string) *url.URL {
43+
n, subDir := SourceDirSubdir(n)
4344
u, err := urlhelper.Parse(testModule(n))
4445
if err != nil {
4546
panic(err)
4647
}
48+
if subDir != "" {
49+
u.Path += "//" + subDir
50+
u.RawPath = u.Path
51+
}
4752

4853
return u
4954
}

source.go

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package getter
22

33
import (
4+
"fmt"
5+
"path/filepath"
46
"strings"
57
)
68

@@ -34,3 +36,22 @@ func SourceDirSubdir(src string) (string, string) {
3436

3537
return src, subdir
3638
}
39+
40+
// SubdirGlob returns the actual subdir with globbing processed.
41+
//
42+
// dst should be a destination directory that is already populated (the
43+
// download is complete) and subDir should be the set subDir. If subDir
44+
// is an empty string, this returns an empty string.
45+
//
46+
// The returned path is the full absolute path.
47+
func SubdirGlob(dst, subDir string) (string, error) {
48+
matches, err := filepath.Glob(filepath.Join(dst, subDir))
49+
if err != nil {
50+
return "", err
51+
}
52+
if len(matches) > 1 {
53+
return "", fmt.Errorf("subdir %q matches multiple paths", subDir)
54+
}
55+
56+
return matches[0], nil
57+
}

0 commit comments

Comments
 (0)