-
Notifications
You must be signed in to change notification settings - Fork 1
/
appimport_test.go
72 lines (60 loc) · 1.86 KB
/
appimport_test.go
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
package appimport_test
import (
"path/filepath"
"testing"
"strings"
"os"
"github.com/drud/ddev/pkg/appimport"
"github.com/drud/ddev/pkg/util"
asrt "github.com/stretchr/testify/assert"
)
// TestValidateAsset tests validation of asset paths.
func TestValidateAsset(t *testing.T) {
assert := asrt.New(t)
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd: %s", err)
}
testdata := filepath.Join(cwd, "testdata")
// test tilde expansion
userDir, err := os.UserHomeDir()
testDirName := "tmp.ddev.testpath-" + util.RandString(4)
testDir := filepath.Join(userDir, testDirName)
assert.NoError(err)
err = os.Mkdir(testDir, 0755)
assert.NoError(err)
testPath, isArchive, err := appimport.ValidateAsset("~/"+testDirName, "files")
assert.NoError(err)
assert.Contains(testPath, userDir)
assert.False(strings.Contains(testPath, "~"))
assert.False(isArchive)
err = os.Remove(testDir)
assert.NoError(err)
// test a relative path
deepDir := filepath.Join(testdata, "dirlevel1", "dirlevel2")
err = os.Chdir(deepDir)
assert.NoError(err)
testPath, _, err = appimport.ValidateAsset("../../dirlevel1", "files")
assert.NoError(err)
assert.Contains(testPath, "dirlevel1")
// archive
testArchivePath := filepath.Join(testdata, "somedb.sql.gz")
_, isArchive, err = appimport.ValidateAsset(testArchivePath, "db")
assert.NoError(err)
assert.True(isArchive)
// db no sql
gofilePath := filepath.Join(testdata, "junk.go")
_, isArchive, err = appimport.ValidateAsset(gofilePath, "db")
if err != nil {
assert.Contains(err.Error(), "provided path is not a .sql file or archive")
}
assert.Error(err)
assert.False(isArchive)
// files not a directory
_, isArchive, err = appimport.ValidateAsset(gofilePath, "files")
assert.False(isArchive)
assert.Error(err)
if err != nil {
assert.Contains(err.Error(), "provided path is not a directory or archive")
}
}