feat(ignore): support ignore list (#86)

fix #85
This commit is contained in:
Bo-Yi Wu
2019-01-18 15:12:36 +08:00
committed by GitHub
parent 4f7d31bd86
commit d921289474
2 changed files with 111 additions and 14 deletions
+79 -8
View File
@@ -80,7 +80,9 @@ func TestTrimElement(t *testing.T) {
func TestSCPFileFromPublicKey(t *testing.T) {
if os.Getenv("SSH_AUTH_SOCK") != "" {
exec.Command("eval", "`ssh-agent -k`").Run()
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
t.Fatalf("exec: %v", err)
}
}
u, err := user.Lookup("drone-scp")
@@ -128,7 +130,9 @@ func TestSCPFileFromPublicKey(t *testing.T) {
func TestSCPWildcardFileList(t *testing.T) {
if os.Getenv("SSH_AUTH_SOCK") != "" {
exec.Command("eval", "`ssh-agent -k`").Run()
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
t.Fatalf("exec: %v", err)
}
}
u, err := user.Lookup("drone-scp")
@@ -202,7 +206,9 @@ func TestSCPFromProxySetting(t *testing.T) {
func TestStripComponentsFlag(t *testing.T) {
if os.Getenv("SSH_AUTH_SOCK") != "" {
exec.Command("eval", "`ssh-agent -k`").Run()
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
t.Fatalf("exec: %v", err)
}
}
u, err := user.Lookup("drone-scp")
@@ -237,6 +243,45 @@ func TestStripComponentsFlag(t *testing.T) {
}
}
func TestIgnoreList(t *testing.T) {
if os.Getenv("SSH_AUTH_SOCK") != "" {
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
t.Fatalf("exec: %v", err)
}
}
u, err := user.Lookup("drone-scp")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
plugin := Plugin{
Config: Config{
Host: []string{"localhost"},
Username: "drone-scp",
Port: "22",
KeyPath: "tests/.ssh/id_rsa",
Source: []string{"tests/global/*", "!tests/global/c.txt"},
StripComponents: 2,
Target: []string{filepath.Join(u.HomeDir, "123")},
CommandTimeout: 60,
TarExec: "tar",
},
}
err = plugin.Exec()
assert.Nil(t, err)
// check file exist
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/c.txt")); os.IsExist(err) {
t.Fatalf("SCP-error: %v", err)
}
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/d.txt")); os.IsNotExist(err) {
t.Fatalf("SCP-error: %v", err)
}
}
// func TestSCPFileFromSSHAgent(t *testing.T) {
// if os.Getenv("SSH_AUTH_SOCK") == "" {
// exec.Command("eval", "`ssh-agent -s`").Run()
@@ -333,24 +378,50 @@ func TestGlobList(t *testing.T) {
// wrong patern
paterns := []string{"[]a]", "tests/?.txt"}
expects := []string{"tests/a.txt", "tests/b.txt"}
assert.Equal(t, expects, globList(paterns))
assert.Equal(t, expects, globList(paterns).Source)
paterns = []string{"tests/*.txt", "tests/.ssh/*", "abc*"}
expects = []string{"tests/a.txt", "tests/b.txt", "tests/.ssh/id_rsa", "tests/.ssh/id_rsa.pub"}
assert.Equal(t, expects, globList(paterns))
assert.Equal(t, expects, globList(paterns).Source)
paterns = []string{"tests/?.txt"}
expects = []string{"tests/a.txt", "tests/b.txt"}
assert.Equal(t, expects, globList(paterns))
assert.Equal(t, expects, globList(paterns).Source)
// remove item which file not found.
paterns = []string{"tests/aa.txt", "tests/b.txt"}
expects = []string{"tests/b.txt"}
assert.Equal(t, expects, globList(paterns))
assert.Equal(t, expects, globList(paterns).Source)
paterns = []string{"./tests/b.txt"}
expects = []string{"./tests/b.txt"}
assert.Equal(t, expects, globList(paterns))
assert.Equal(t, expects, globList(paterns).Source)
paterns = []string{"./tests/*.txt", "!./tests/b.txt"}
expectSources := []string{"tests/a.txt", "tests/b.txt"}
expectIgnores := []string{"./tests/b.txt"}
result := globList(paterns)
assert.Equal(t, expectSources, result.Source)
assert.Equal(t, expectIgnores, result.Ignore)
}
func TestBuildArgs(t *testing.T) {
list := fileList{
Source: []string{"tests/a.txt", "tests/b.txt"},
Ignore: []string{"tests/a.txt"},
}
result := buildArgs("test.tar.gz", list)
expects := []string{"--exclude", "tests/a.txt", "-cf", "test.tar.gz", "tests/a.txt", "tests/b.txt"}
assert.Equal(t, expects, result)
list = fileList{
Source: []string{"tests/a.txt", "tests/b.txt"},
}
result = buildArgs("test.tar.gz", list)
expects = []string{"-cf", "test.tar.gz", "tests/a.txt", "tests/b.txt"}
assert.Equal(t, expects, result)
}
func TestRemoveDestFile(t *testing.T) {