File: source_test.go

package info (click to toggle)
golang-github-hashicorp-go-getter 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 936 kB
  • sloc: makefile: 5
file content (106 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package getter

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

func TestSourceDirSubdir(t *testing.T) {
	cases := []struct {
		Input    string
		Dir, Sub string
	}{
		{
			"hashicorp.com",
			"hashicorp.com", "",
		},
		{
			"hashicorp.com//foo",
			"hashicorp.com", "foo",
		},
		{
			"hashicorp.com//foo?bar=baz",
			"hashicorp.com?bar=baz", "foo",
		},
		{
			"https://hashicorp.com/path//*?archive=foo",
			"https://hashicorp.com/path?archive=foo", "*",
		},
		{
			"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256",
			"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "",
		},
		{
			"https://hashicorp.com/path//*?checksum=file:http://url.com/....iso.sha256",
			"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "*",
		},
		{
			"file://foo//bar",
			"file://foo", "bar",
		},
	}

	for i, tc := range cases {
		adir, asub := SourceDirSubdir(tc.Input)
		if adir != tc.Dir {
			t.Fatalf("%d: bad dir: %#v", i, adir)
		}
		if asub != tc.Sub {
			t.Fatalf("%d: bad sub: %#v", i, asub)
		}
	}
}

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)
	}
}