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

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

Issue 2981243002: isolate: (refactor) extract blacklisting into common package (Closed)
Patch Set: address review comments 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
« no previous file with comments | « client/internal/common/filesystem_view.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..4fdca5c2573498a1f83dc91540fcf256dd7c41b2
--- /dev/null
+++ b/client/internal/common/filesystem_view_linux_test.go
@@ -0,0 +1,226 @@
+// Copyright 2017 The LUCI Authors.
+//
+// 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,
+ },
+ }
+
+ 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) {
+ 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)
+
+ relPath, err := fsView.RelativePath(tc.absPath)
+ // These test cases contain only valid relative paths.
+ // non-computable relative paths are tested in TestCalculatesRelativePaths.
+ So(err, ShouldBeNil)
+ So(relPath, ShouldEqual, tc.wantRelPath)
+ })
+ }
+}
« no previous file with comments | « client/internal/common/filesystem_view.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698