aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Winslow <swinslow@gmail.com>2018-10-22 19:00:58 -0400
committerSteve Winslow <swinslow@gmail.com>2018-10-22 19:00:58 -0400
commita87dc863c79ee03a4e5f0390a615671cccce308f (patch)
treec5ecdc9c7c7cc658acde7d5938ba1ca895806fab
parentdf7fc966c5d9389f7df995be565a64590062a97f (diff)
downloadspdx-tools-a87dc863c79ee03a4e5f0390a615671cccce308f.tar.gz
Extracted filesystem tests into utils package
Signed-off-by: Steve Winslow <swinslow@gmail.com>
-rw-r--r--README.md1
-rw-r--r--v0/builder/builder2v1/build_file.go3
-rw-r--r--v0/builder/builder2v1/build_package.go3
-rw-r--r--v0/utils/filesystem.go (renamed from v0/builder/builder2v1/filesystem.go)21
-rw-r--r--v0/utils/filesystem_test.go (renamed from v0/builder/builder2v1/filesystem_test.go)70
5 files changed, 55 insertions, 43 deletions
diff --git a/README.md b/README.md
index ea88c27..0382fae 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ spdx-go provides the following packages:
* *v0/tvsaver* - tag-value file saver
* *v0/builder* - builds "empty" SPDX document (with hashes) for directory contents
* *v0/idsearcher* - searches for [SPDX short-form IDs](https://spdx.org/ids/) and builds SPDX document
+* *v0/utils* - various utility functions that support the other spdx-go packages
## What it doesn't do
diff --git a/v0/builder/builder2v1/build_file.go b/v0/builder/builder2v1/build_file.go
index 5c9e0c5..4d3e2ce 100644
--- a/v0/builder/builder2v1/build_file.go
+++ b/v0/builder/builder2v1/build_file.go
@@ -7,6 +7,7 @@ import (
"path/filepath"
"github.com/swinslow/spdx-go/v0/spdx"
+ "github.com/swinslow/spdx-go/v0/utils"
)
// BuildFileSection2_1 creates an SPDX File (version 2.1), returning that
@@ -19,7 +20,7 @@ func BuildFileSection2_1(filePath string, prefix string, fileNumber int) (*spdx.
p := filepath.Join(prefix, filePath)
// make sure we can get the file and its hashes
- ssha1, ssha256, smd5, err := getHashesForFilePath(p)
+ ssha1, ssha256, smd5, err := utils.GetHashesForFilePath(p)
if err != nil {
return nil, err
}
diff --git a/v0/builder/builder2v1/build_package.go b/v0/builder/builder2v1/build_package.go
index 3457b76..2b10836 100644
--- a/v0/builder/builder2v1/build_package.go
+++ b/v0/builder/builder2v1/build_package.go
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/swinslow/spdx-go/v0/spdx"
+ "github.com/swinslow/spdx-go/v0/utils"
)
// BuildPackageSection2_1 creates an SPDX Package (version 2.1), returning
@@ -16,7 +17,7 @@ import (
func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []string) (*spdx.Package2_1, error) {
// build the file section first, so we'll have it available
// for calculating the package verification code
- filepaths, err := getAllFilePaths(dirRoot, pathsIgnore)
+ filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore)
if err != nil {
return nil, err
}
diff --git a/v0/builder/builder2v1/filesystem.go b/v0/utils/filesystem.go
index 392f229..82e95a9 100644
--- a/v0/builder/builder2v1/filesystem.go
+++ b/v0/utils/filesystem.go
@@ -1,6 +1,7 @@
+// Package utils contains various utility functions to support the
+// main spdx-go packages.
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
-
-package builder2v1
+package utils
import (
"crypto/md5"
@@ -13,7 +14,10 @@ import (
"strings"
)
-func getAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
+// GetAllFilePaths takes a path to a directory (including an optional slice of
+// path patterns to ignore), and returns a slice of relative paths to all files
+// in that directory and its subdirectories (excluding those that are ignored).
+func GetAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
// paths is a _pointer_ to a slice -- not just a slice.
// this is so that it can be appropriately modified by append
// in the sub-function.
@@ -36,7 +40,7 @@ func getAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
shortPath := strings.TrimPrefix(path, prefix)
// don't include path if it should be ignored
- if pathsIgnored != nil && shouldIgnore(shortPath, pathsIgnored) {
+ if pathsIgnored != nil && ShouldIgnore(shortPath, pathsIgnored) {
return nil
}
@@ -48,7 +52,9 @@ func getAllFilePaths(dirRoot string, pathsIgnored []string) ([]string, error) {
return *paths, err
}
-func getHashesForFilePath(p string) (string, string, string, error) {
+// GetHashesForFilePath takes a path to a file on disk, and returns
+// SHA1, SHA256 and MD5 hashes for that file as strings.
+func GetHashesForFilePath(p string) (string, string, string, error) {
f, err := os.Open(p)
if err != nil {
return "", "", "", err
@@ -72,7 +78,10 @@ func getHashesForFilePath(p string) (string, string, string, error) {
return ssha1, ssha256, smd5, nil
}
-func shouldIgnore(fileName string, pathsIgnored []string) bool {
+// ShouldIgnore compares a file path to a slice of file path patterns,
+// and determines whether that file should be ignored because it matches
+// any of those patterns.
+func ShouldIgnore(fileName string, pathsIgnored []string) bool {
fDirs, fFile := filepath.Split(fileName)
for _, pattern := range pathsIgnored {
diff --git a/v0/builder/builder2v1/filesystem_test.go b/v0/utils/filesystem_test.go
index 51a73cf..70e1291 100644
--- a/v0/builder/builder2v1/filesystem_test.go
+++ b/v0/utils/filesystem_test.go
@@ -1,16 +1,16 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
-package builder2v1
+package utils
import (
"testing"
)
// ===== Filesystem and hash functionality tests =====
-func TestBuilder2_1CanGetSliceOfFolderContents(t *testing.T) {
- dirRoot := "../../testdata/project1/"
+func TestFilesystemCanGetSliceOfFolderContents(t *testing.T) {
+ dirRoot := "../testdata/project1/"
- filePaths, err := getAllFilePaths(dirRoot, nil)
+ filePaths, err := GetAllFilePaths(dirRoot, nil)
if err != nil {
t.Fatalf("expected filePaths, got error: %v", err)
}
@@ -41,17 +41,17 @@ func TestBuilder2_1CanGetSliceOfFolderContents(t *testing.T) {
}
}
-func TestBuilder2_1GetAllFilePathsFailsForNonExistentDirectory(t *testing.T) {
+func TestFilesystemGetAllFilePathsFailsForNonExistentDirectory(t *testing.T) {
dirRoot := "./does/not/exist/"
- _, err := getAllFilePaths(dirRoot, nil)
+ _, err := GetAllFilePaths(dirRoot, nil)
if err == nil {
t.Errorf("expected non-nil error, got nil")
}
}
-func TestBuilder2_1CanIgnoreFilesWhenGettingFilePaths(t *testing.T) {
- dirRoot := "../../testdata/project3/"
+func TestFilesystemCanIgnoreFilesWhenGettingFilePaths(t *testing.T) {
+ dirRoot := "../testdata/project3/"
pathsIgnored := []string{
"**/ignoredir/",
"/excludedir/",
@@ -59,7 +59,7 @@ func TestBuilder2_1CanIgnoreFilesWhenGettingFilePaths(t *testing.T) {
"/alsoEXCLUDEthis.txt",
}
- filePaths, err := getAllFilePaths(dirRoot, pathsIgnored)
+ filePaths, err := GetAllFilePaths(dirRoot, pathsIgnored)
if err != nil {
t.Fatalf("expected filePaths, got error: %v", err)
}
@@ -94,10 +94,10 @@ func TestBuilder2_1CanIgnoreFilesWhenGettingFilePaths(t *testing.T) {
// FIXME add test to make sure we get an error for a directory without
// FIXME appropriate permissions to read its (sub)contents
-func TestBuilder2_1GetsHashesForFilePath(t *testing.T) {
- f := "../../testdata/project1/file1.testdata.txt"
+func TestFilesystemGetsHashesForFilePath(t *testing.T) {
+ f := "../testdata/project1/file1.testdata.txt"
- ssha1, ssha256, smd5, err := getHashesForFilePath(f)
+ ssha1, ssha256, smd5, err := GetHashesForFilePath(f)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
@@ -112,10 +112,10 @@ func TestBuilder2_1GetsHashesForFilePath(t *testing.T) {
}
}
-func TestBuilder2_1GetsErrorWhenRequestingHashesForInvalidFilePath(t *testing.T) {
+func TestFilesystemGetsErrorWhenRequestingHashesForInvalidFilePath(t *testing.T) {
f := "./does/not/exist"
- _, _, _, err := getHashesForFilePath(f)
+ _, _, _, err := GetHashesForFilePath(f)
if err == nil {
t.Errorf("expected non-nil error, got nil")
}
@@ -124,102 +124,102 @@ func TestBuilder2_1GetsErrorWhenRequestingHashesForInvalidFilePath(t *testing.T)
// FIXME add test to make sure we get an error for hashes for a file without
// FIXME appropriate permissions to read its contents
-func TestBuilder2_1ExcludesForIgnoredPaths(t *testing.T) {
+func TestFilesystemExcludesForIgnoredPaths(t *testing.T) {
// one specific file
pathsIgnored := []string{"/file.txt"}
fileName := "/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/fileNope.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
// two specific files
pathsIgnored = []string{"/file.txt", "/file2.txt"}
fileName = "/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/fileNope.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/file2.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
// one specific file in specific subdirectory
pathsIgnored = []string{"/subdir/file.txt"}
fileName = "/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/file.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/something/subdir/file.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
// one specific file in any directory
pathsIgnored = []string{"**/file.txt"}
fileName = "/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/something/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/something/fileNope.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
// all files in one specific subdirectory (and its subdirectories)
pathsIgnored = []string{"/subdir/"}
fileName = "/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/file.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/subdir/sub2/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/nope/subdir/file.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
// all files in subdirectory with this name, wherever it appears
pathsIgnored = []string{"**/subdir/"}
fileName = "/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/file.txt"
- if shouldIgnore(fileName, pathsIgnored) {
+ if ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/subdir/sub2/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}
fileName = "/nope/subdir/file.txt"
- if !shouldIgnore(fileName, pathsIgnored) {
+ if !ShouldIgnore(fileName, pathsIgnored) {
t.Errorf("incorrect for %v, ignoring %v", fileName, pathsIgnored)
}