diff --git a/check.go b/check.go index c7752b0..4adfe73 100644 --- a/check.go +++ b/check.go @@ -84,16 +84,25 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err if keywords != nil && !inSlice(kv.Keyword(), keywords) { continue } - fh, err := os.Open(pathname) - if err != nil { - return nil, err - } - curKeyVal, err := keywordFunc(pathname, info, fh) - if err != nil { + + var curKeyVal string + if info.Mode().IsRegular() { + fh, err := os.Open(pathname) + if err != nil { + return nil, err + } + curKeyVal, err = keywordFunc(pathname, info, fh) + if err != nil { + fh.Close() + return nil, err + } fh.Close() - return nil, err + } else { + curKeyVal, err = keywordFunc(pathname, info, nil) + if err != nil { + return nil, err + } } - fh.Close() if string(kv) != curKeyVal { failure := Failure{Path: pathname, Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()} result.Failures = append(result.Failures, failure) @@ -174,7 +183,7 @@ func TarCheck(tarDH, dh *DirectoryHierarchy, keywords []string) (*Result, error) } for _, kv := range kvs { - + // TODO: keep track of symlinks if _, ok := KeywordFuncs[kv.Keyword()]; !ok { return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), pathname) } diff --git a/check_test.go b/check_test.go index 035c2b3..5a54e40 100644 --- a/check_test.go +++ b/check_test.go @@ -97,6 +97,24 @@ func ExampleCheck() { } } +// Tests default action for evaluating a symlink, which is just to compare the +// link itself, not to follow it +func TestDefaultBrokenLink(t *testing.T) { + dh, err := Walk("./testdata/dirwithbrokenlink", nil, append(DefaultKeywords, "sha1")) + if err != nil { + t.Fatal(err) + } + res, err := Check("./testdata/dirwithbrokenlink", dh, nil) + if err != nil { + t.Fatal(err) + } + if res != nil && len(res.Failures) > 0 { + for _, f := range res.Failures { + t.Error(f) + } + } +} + // https://github.com/vbatts/go-mtree/issues/8 func TestTimeComparison(t *testing.T) { dir, err := ioutil.TempDir("", "test-time.") diff --git a/testdata/dirwithbrokenlink/ file with spaces b/testdata/dirwithbrokenlink/ file with spaces new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/deepfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/deepfile new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile new file mode 100644 index 0000000..fae9c31 --- /dev/null +++ b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile @@ -0,0 +1 @@ +some spacious text diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile new file mode 100644 index 0000000..b0cb8ea --- /dev/null +++ b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile @@ -0,0 +1 @@ +some nice text diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file b/testdata/dirwithbrokenlink/deepdir/dir3/file new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file6 b/testdata/dirwithbrokenlink/deepdir/dir3/file6 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file7 b/testdata/dirwithbrokenlink/deepdir/dir3/file7 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/this_is_a_file b/testdata/dirwithbrokenlink/deepdir/this_is_a_file new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/dir1/.hiddenfile b/testdata/dirwithbrokenlink/dir1/.hiddenfile new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/dir1/badlink b/testdata/dirwithbrokenlink/dir1/badlink new file mode 120000 index 0000000..7768d5f --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/badlink @@ -0,0 +1 @@ +badfile \ No newline at end of file diff --git a/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink b/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink new file mode 120000 index 0000000..e66f4ae --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink @@ -0,0 +1 @@ +../../goodfile \ No newline at end of file diff --git a/testdata/dirwithbrokenlink/dir1/goodfile b/testdata/dirwithbrokenlink/dir1/goodfile new file mode 100644 index 0000000..9c7b8e1 --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/goodfile @@ -0,0 +1 @@ +link text diff --git a/testdata/dirwithbrokenlink/file1 b/testdata/dirwithbrokenlink/file1 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/file2 b/testdata/dirwithbrokenlink/file2 new file mode 100644 index 0000000..e69de29