Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Unified Diff: client/internal/common/filesystem_view_linux_test.go

Issue 2981243002: isolate: (refactor) extract blacklisting into common package (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: client/internal/common/filesystem_view_linux_test.go
diff --git a/client/internal/common/filesystem_view_linux_test.go b/client/internal/common/filesystem_view_linux_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..e9f90c2481e5519ad78dfa6cc3b43169dea05805
--- /dev/null
+++ b/client/internal/common/filesystem_view_linux_test.go
@@ -0,0 +1,227 @@
+// Copyright 2017 The LUCI Authors.
mithro 2017/07/19 04:48:41 Why filesystem_view_linux_test.go ? Is there a wi
mcgreevy 2017/07/19 05:11:04 The presence of "_linux" before "_test" will cause
mithro 2017/07/19 05:46:18 Are blacklist patterns OS dependent? I feel like
mcgreevy 2017/07/21 04:15:50 In practice, blacklists look to be pretty simple,
+//
+// 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 common
+
+import (
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestValidatesBlacklist(t *testing.T) {
+ type testCase struct {
+ desc string
+ blacklist []string
+ wantErr bool
+ }
+
+ testCases := []testCase{
+ {
+ desc: "no patterns",
+ blacklist: []string{},
+ wantErr: false,
+ },
+ {
+ desc: "good pattern",
+ blacklist: []string{"*"},
+ wantErr: false,
+ },
+ {
+ desc: "good patterns",
+ blacklist: []string{"a*", "b*"},
+ wantErr: false,
+ },
+ {
+ desc: "bad pattern",
+ blacklist: []string{"["},
+ wantErr: true,
+ },
+ {
+ desc: "bad first pattern",
+ blacklist: []string{"[", "*"},
+ wantErr: true,
+ },
+ {
+ desc: "bad last pattern",
+ blacklist: []string{"*", "["},
+ wantErr: true,
mithro 2017/07/19 04:48:41 bad middle pattern?
mcgreevy 2017/07/19 05:11:04 I don't think that's a case worth testing.
mithro 2017/07/19 05:46:19 If you don't really think it is worth testing, I'm
mcgreevy 2017/07/21 04:15:50 The main failure mode I'm trying to cover here is
+ },
+ }
+
+ for _, tc := range testCases {
+ tc := tc
+ Convey(tc.desc, t, func() {
+ _, err := NewFilesystemView("/root", tc.blacklist)
+ if tc.wantErr {
+ So(err, ShouldNotBeNil)
+ } else {
+ So(err, ShouldBeNil)
+ }
+ })
+ }
+
+}
+
+func TestCalculatesRelativePaths(t *testing.T) {
+ type testCase struct {
+ desc string
+ root string
+ absPath string
+ wantRelPath string
+ wantErr bool
+ }
+
+ testCases := []testCase{
+ {
+ desc: "no trailing slash",
+ root: "/a",
+ absPath: "/a/b",
+ wantRelPath: "b",
+ wantErr: false,
+ },
+ {
+ desc: "trailing slash",
+ root: "/a",
+ absPath: "/a/b/",
+ wantRelPath: "b",
+ wantErr: false,
+ },
+ {
+ desc: "no common root",
+ root: "/a",
+ absPath: "/x/y",
+ wantRelPath: "../x/y",
+ wantErr: false,
+ },
+ {
+ desc: "no computable relative path",
+ root: "/a",
+ absPath: "./x/y",
+ wantRelPath: "",
+ wantErr: true,
+ },
+ {
+ desc: "root",
+ root: "/a",
+ absPath: "/a",
+ wantRelPath: ".",
+ wantErr: false,
+ },
+ }
+
+ for _, tc := range testCases {
+ tc := tc
+ Convey(tc.desc, t, func() {
+ fsView, err := NewFilesystemView(tc.root, nil)
+
+ So(err, ShouldBeNil)
+
+ relPath, err := fsView.RelativePath(tc.absPath)
+ So(relPath, ShouldEqual, tc.wantRelPath)
+ if tc.wantErr {
+ So(err, ShouldNotBeNil)
+ } else {
+ So(err, ShouldBeNil)
+ }
+ })
+ }
+}
+
+func TestAppliesBlacklist(t *testing.T) {
mithro 2017/07/19 05:46:19 BTW Does blacklisting (via filepath.Match) support
mcgreevy 2017/07/21 04:15:50 As mentioned in a comment above, this feature is a
+ type testCase struct {
+ desc string
+ root string
+ blacklist []string
+ absPath string
+ wantRelPath string
+ }
+
+ testCases := []testCase{
+ {
+ desc: "no blacklist",
+ root: "/a",
+ blacklist: []string{},
+ absPath: "/a/x/y",
+ wantRelPath: "x/y",
+ },
+ {
+ desc: "blacklist matches relative path",
+ root: "/a",
+ blacklist: []string{"?/z"},
+ absPath: "/a/x/z",
+ wantRelPath: "",
+ },
+ {
+ desc: "blacklist doesn't match relative path",
+ root: "/a",
+ blacklist: []string{"?/z"},
+ absPath: "/a/x/y",
+ wantRelPath: "x/y",
+ },
+ {
+ desc: "blacklist matches basename",
+ root: "/a",
+ blacklist: []string{"z"},
+ absPath: "/a/x/z",
+ wantRelPath: "",
+ },
+ {
+ desc: "blacklist doesn't match basename",
+ root: "/a",
+ blacklist: []string{"z"},
+ absPath: "/a/z/y",
+ wantRelPath: "z/y",
+ },
+ {
+ desc: "root never matches blacklist",
+ root: "/a",
+ blacklist: []string{"?"},
+ absPath: "/a",
+ wantRelPath: ".",
+ },
+ {
+ desc: "only one blacklist need match path (1)",
+ root: "/a",
+ blacklist: []string{"z", "abc"},
+ absPath: "/a/x/z",
+ wantRelPath: "",
+ },
+ {
+ desc: "only one blacklist need match path (2)",
+ root: "/a",
+ blacklist: []string{"abc", "z"},
+ absPath: "/a/x/z",
+ wantRelPath: "",
+ },
+ }
+
+ for _, tc := range testCases {
+ tc := tc
+ Convey(tc.desc, t, func() {
+ fsView, err := NewFilesystemView(tc.root, tc.blacklist)
+
+ // These test cases contain only valid blacklists.
+ // Invalid blacklists are tested in TestValidatesBlacklist.
+ So(err, ShouldBeNil)
mithro 2017/07/19 05:46:19 My mind keeps reading this as "Should Be Neil" :-P
mcgreevy 2017/07/21 04:15:50 Acknowledged.
+
+ relPath, err := fsView.RelativePath(tc.absPath)
+ So(relPath, ShouldEqual, tc.wantRelPath)
mithro 2017/07/19 05:46:19 It "feels" like you be checking err value first an
mcgreevy 2017/07/21 04:15:50 Done.
+
+ // These test cases contain only valid relative paths.
+ // non-computable relative paths are tested in TestCalculatesRelativePaths.
+ So(err, ShouldBeNil)
+ })
+ }
+}
« client/internal/common/filesystem_view.go ('K') | « client/internal/common/filesystem_view.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698