2013-12-27 17:40:23 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors.
|
2013-12-27 17:40:23 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-01-06 16:44:08 +00:00
|
|
|
"path"
|
2013-12-27 17:40:23 +00:00
|
|
|
"path/filepath"
|
2016-01-06 16:44:08 +00:00
|
|
|
"runtime"
|
2013-12-27 17:40:23 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-25 22:46:07 +00:00
|
|
|
// this test demonstrates why http://camlistore.org/r/2296 was needed for
|
|
|
|
// matching to correctly work when the checker has more than one pattern of the
|
|
|
|
// same kind. Before the fix, this test would fail because the first
|
|
|
|
// isShellPatternMatch closure of the checker would not end up using the
|
|
|
|
// intended "*.jpg" pattern.
|
|
|
|
func TestIgnoreMultiPattern(t *testing.T) {
|
|
|
|
ignoredShellPattern := []string{
|
|
|
|
"*.jpg",
|
|
|
|
"*.png",
|
|
|
|
"*.gif",
|
|
|
|
}
|
|
|
|
ignoreChecker := newIgnoreChecker(ignoredShellPattern)
|
|
|
|
toIgnore := "/home/foo/Downloads/pony.jpg"
|
2016-04-03 07:21:28 +00:00
|
|
|
if !ignoreChecker(toIgnore) {
|
2014-08-25 22:46:07 +00:00
|
|
|
t.Errorf("Failed to ignore %v with %q among multiple shell patterns in ignore list.", toIgnore, ignoredShellPattern[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 17:40:23 +00:00
|
|
|
func TestIsIgnoredFile(t *testing.T) {
|
2014-02-23 17:34:16 +00:00
|
|
|
old := osutilHomeDir
|
|
|
|
defer func() { osutilHomeDir = old }()
|
2016-01-06 16:44:08 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
osutilHomeDir = func() string {
|
|
|
|
return `C:\Fake\Users\Camli`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
osutilHomeDir = func() string {
|
|
|
|
return "/Fake/Home/Camli"
|
|
|
|
}
|
2014-02-23 17:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
home := osutilHomeDir()
|
2013-12-27 17:40:23 +00:00
|
|
|
fullpath := filepath.Join(home, "Downloads", "pony.jpg")
|
|
|
|
var wantIgnored = map[string]bool{
|
|
|
|
filepath.Join(home, filepath.FromSlash("Downloads/pony.jpg")): true,
|
|
|
|
filepath.Join(home, filepath.FromSlash("Downloads/pony.*")): true,
|
|
|
|
filepath.Join(home, filepath.FromSlash("Downloads/*.jpg")): true,
|
|
|
|
filepath.Join(home, filepath.FromSlash("Downloads/*")): true,
|
|
|
|
"*.jpg": true,
|
|
|
|
"pony.*": true,
|
|
|
|
"*.foo": false,
|
|
|
|
"foo.*": false,
|
|
|
|
filepath.Join(home, "Downloads"): true,
|
|
|
|
filepath.Join(home, "Down"): false,
|
|
|
|
filepath.FromSlash("~/Downloads/pony.jpg"): true,
|
|
|
|
filepath.FromSlash("~/Downloads/pony.*"): true,
|
|
|
|
filepath.FromSlash("~/Downloads/*.jpg"): true,
|
|
|
|
filepath.FromSlash("~/Downloads"): true,
|
|
|
|
filepath.FromSlash("~/Down"): false,
|
|
|
|
filepath.FromSlash("~/DownloadsAndMore"): false,
|
|
|
|
home: true,
|
|
|
|
"Downloads": true,
|
|
|
|
"Down": false,
|
|
|
|
}
|
|
|
|
for pattern, want := range wantIgnored {
|
|
|
|
ignoreChecker := newIgnoreChecker([]string{pattern})
|
|
|
|
if ignoreChecker(fullpath) != want {
|
|
|
|
t.Errorf("%v not ignored; did not match %v", fullpath, pattern)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type patternTest struct {
|
|
|
|
name string
|
|
|
|
fn func(string, string) bool
|
|
|
|
pattern string
|
|
|
|
fullpath string
|
|
|
|
want bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIgnoreFns(t *testing.T) {
|
2016-01-06 16:44:08 +00:00
|
|
|
// POSIX tests
|
2013-12-27 17:40:23 +00:00
|
|
|
tests := []patternTest{
|
|
|
|
{
|
|
|
|
name: "isShellPatternMatch",
|
|
|
|
fn: isShellPatternMatch,
|
|
|
|
pattern: "/home/pony/rainbow.*",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "isShellPatternMatch",
|
|
|
|
fn: isShellPatternMatch,
|
|
|
|
pattern: "rainbow.*",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "isShellPatternMatch",
|
|
|
|
fn: isShellPatternMatch,
|
|
|
|
pattern: "*.jpg",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "isShellPatternMatch",
|
|
|
|
fn: isShellPatternMatch,
|
|
|
|
pattern: "pony/rainbow.*",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasDirPrefix",
|
|
|
|
fn: hasDirPrefix,
|
|
|
|
pattern: "/home/pony",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasDirPrefix",
|
|
|
|
fn: hasDirPrefix,
|
|
|
|
pattern: "/home/lizard",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "pony",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "pon",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: false,
|
|
|
|
},
|
2016-01-06 16:44:08 +00:00
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "/home/pony",
|
|
|
|
fullpath: "/home/pony/rainbow.jpg",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "pony",
|
|
|
|
fullpath: "/home/ponytail/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "pony",
|
|
|
|
fullpath: "/home/pony/ponytail/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: "rainbow.jpg",
|
|
|
|
fullpath: "/home/ponytail/pony/rainbow.jpg",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// A path starting with a single slash is relative
|
|
|
|
// path on Windows. Prepend a drive letter to it to
|
|
|
|
// make it absolute.
|
|
|
|
// Also clean paths so that the test work on Windows.
|
|
|
|
const driveSpec = "C:"
|
|
|
|
for i := range tests {
|
|
|
|
v := &tests[i]
|
|
|
|
// Check path.IsAbs, not filepath.IsAbs to see
|
|
|
|
// if v.pattern should be absolute.
|
|
|
|
if path.IsAbs(v.pattern) {
|
|
|
|
v.pattern = driveSpec + filepath.Clean(v.pattern)
|
|
|
|
} else {
|
|
|
|
v.pattern = filepath.Clean(v.pattern)
|
|
|
|
}
|
|
|
|
v.fullpath = driveSpec + filepath.Clean(v.fullpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Windows, a volume name such as a drive letter or UNC volume:
|
|
|
|
// `C:`
|
|
|
|
// `\\server\sharename`
|
|
|
|
// is considered a single path component. Therefore neither of:
|
|
|
|
// `server`
|
|
|
|
// `sharename`
|
|
|
|
// `server\sharename`
|
|
|
|
// should be accepted for fullpath == `\\server\sharename\...`
|
|
|
|
windowsTests := []patternTest{
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `pony`,
|
|
|
|
fullpath: `C:\pony\rainbow.jpg`,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `pony`,
|
|
|
|
fullpath: `\\server\sharename\pony\rainbow.jpg`,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `C:`,
|
|
|
|
fullpath: `C:\windows\system32`,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `server`,
|
|
|
|
fullpath: `\\server\sharename\rainbow.jpg`,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `sharename`,
|
|
|
|
fullpath: `\\server\sharename\rainbow.jpg`,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hasComponent",
|
|
|
|
fn: hasComponent,
|
|
|
|
pattern: `server\sharename`,
|
|
|
|
fullpath: `\\server\sharename\rainbow.jpg`,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tests = append(tests, windowsTests...)
|
2013-12-27 17:40:23 +00:00
|
|
|
}
|
|
|
|
for _, v := range tests {
|
|
|
|
if v.fn(v.pattern, v.fullpath) != v.want {
|
|
|
|
t.Errorf("%v(%q, %q) != %v", v.name, v.pattern, v.fullpath, v.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|