diff --git a/internal/pkgmanager/package_test.go b/internal/pkgmanager/package_test.go new file mode 100644 index 00000000..37e77547 --- /dev/null +++ b/internal/pkgmanager/package_test.go @@ -0,0 +1,77 @@ +package pkgmanager + +import ( + "testing" + + "github.com/ossf/package-analysis/pkg/api/pkgecosystem" +) + +func TestName(t *testing.T) { + expectedName := "a-package" + p := Pkg{name: expectedName} + + actualName := p.Name() + if actualName != expectedName { + t.Errorf("Name() = %v; want %v", actualName, expectedName) + } +} + +func TestVersion(t *testing.T) { + expectedVersion := "1.0.0" + p := Pkg{version: expectedVersion} + + actualVersion := p.Version() + if actualVersion != expectedVersion { + t.Errorf("Version() = %v; want %v", actualVersion, expectedVersion) + } +} + +func TestEcosystem(t *testing.T) { + expectedEcosystem := pkgecosystem.NPM + p := Pkg{manager: &PkgManager{ecosystem: expectedEcosystem}} + + actualEcosystem := p.Ecosystem() + if actualEcosystem != expectedEcosystem { + t.Errorf("Ecosystem() = %v; want %v", actualEcosystem, expectedEcosystem) + } +} + +func TestEcosystemName(t *testing.T) { + expectedEcosystemName := "npm" + p := Pkg{manager: &PkgManager{ecosystem: pkgecosystem.NPM}} + + actualEcosystemName := p.EcosystemName() + if actualEcosystemName != expectedEcosystemName { + t.Errorf("EcosystemName() = %v; want %v", actualEcosystemName, expectedEcosystemName) + } +} + +func TestManager(t *testing.T) { + expectedPkgManager := &PkgManager{ecosystem: pkgecosystem.RubyGems} + p := Pkg{manager: expectedPkgManager} + + actualPkgManager := p.Manager() + if actualPkgManager != expectedPkgManager { + t.Errorf("Manager() = %v; want %v", actualPkgManager, expectedPkgManager) + } +} + +func TestIsLocal(t *testing.T) { + expectedIsLocal := true + p := Pkg{local: "some/local/path"} + + actualIsLocal := p.IsLocal() + if actualIsLocal != expectedIsLocal { + t.Errorf("IsLocal() = %v; want %v", actualIsLocal, expectedIsLocal) + } +} + +func TestLocalPath(t *testing.T) { + expectedLocalPath := "some/local/path" + p := Pkg{local: expectedLocalPath} + + actualLocalPath := p.LocalPath() + if actualLocalPath != expectedLocalPath { + t.Errorf("LocalPath() = %v; want %v", actualLocalPath, expectedLocalPath) + } +} diff --git a/internal/utils/equals_test.go b/internal/utils/equals_test.go new file mode 100644 index 00000000..44f6489d --- /dev/null +++ b/internal/utils/equals_test.go @@ -0,0 +1,108 @@ +package utils + +import ( + "math" + "testing" +) + +func TestFloatEquals(t *testing.T) { + tests := []struct { + name string + x1 float64 + x2 float64 + absTol float64 + expected bool + }{ + {name: "equal", x1: 1.0, x2: 1.0, absTol: 0.0, expected: true}, + {name: "gt tolerance", x1: 1.0, x2: 1.1, absTol: 0.1, expected: false}, + {name: "lt 1 OoM tolerance", x1: 1.0, x2: 1.1, absTol: 0.2, expected: true}, + {name: "lt 2 OoM tolerance", x1: 1.0, x2: 1.01, absTol: 0.02, expected: true}, + {name: "NaN always equal", x1: math.NaN(), x2: math.NaN(), absTol: 0.123456789, expected: true}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual := FloatEquals(test.x1, test.x2, test.absTol) + if actual != test.expected { + t.Errorf( + "FloatEquals(%f, %f, %f) expected to be %t but was %t", + test.x1, + test.x2, + test.absTol, + test.expected, + actual, + ) + } + }) + } +} + +func TestJSONEquals(t *testing.T) { + tests := []struct { + name string + j1 []byte + j2 []byte + expected bool + }{ + { + name: "eq empty", + j1: []byte("{}"), + j2: []byte("{}"), + expected: true, + }, + { + name: "eq one key", + j1: []byte(`{"a": 1}`), + j2: []byte(`{"a": 1}`), + expected: true, + }, + { + name: "eq two keys", + j1: []byte(`{"a": 1, "b": 1}`), + j2: []byte(`{"a": 1, "b": 1}`), + expected: true, + }, + { + name: "eq two keys diff order", + j1: []byte(`{"a": 1, "b": 1}`), + j2: []byte(`{"b": 1, "a": 1}`), + expected: true, + }, + { + name: "deep eq", + j1: []byte(`{"a": {"b": 1}}`), + j2: []byte(`{"a": {"b": 1}}`), + expected: true, + }, + { + name: "deep eq diff order", + j1: []byte(`{"a": {"b": 1, "c": 1}}`), + j2: []byte(`{"a": {"c": 1, "b": 1}}`), + expected: true, + }, + { + name: "neq", + j1: []byte(`{"a": 1}`), + j2: []byte(`{"b": 1}`), + expected: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual, err := JSONEquals(test.j1, test.j2) + + if err != nil { + t.Fatalf("JSONEquals() = %v; want no error", err) + } + + if actual != test.expected { + t.Errorf( + "JSONEquals(%s, %s) expected to be %t but was %t", + test.j1, + test.j2, + test.expected, + actual, + ) + } + }) + } +} diff --git a/internal/utils/file_write_data_utils_test.go b/internal/utils/file_write_data_utils_test.go new file mode 100644 index 00000000..3b413a55 --- /dev/null +++ b/internal/utils/file_write_data_utils_test.go @@ -0,0 +1,74 @@ +package utils + +import ( + "os" + "path/filepath" + "reflect" + "testing" +) + +func fileExists(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + } + return false +} + +func TestCreateAndWriteTempFile(t *testing.T) { + // Cleanup any old test runs + RemoveTempFilesDirectory() + + fileName := "CreateAndWriteTempFile_testfile.txt" + filePath := filepath.Join(writeBufferFolder, fileName) + + defer RemoveTempFilesDirectory() + + CreateAndWriteTempFile(fileName, []byte("This test file is safe to remove.")) + + if !fileExists(filePath) { + t.Errorf("CreateAndWriteTempFile(): did not create file, want %v", fileName) + } +} + +func TestOpenTempFile(t *testing.T) { + fileName := "CreateAndWriteTempFile_testfile.txt" + fileBody := []byte("This test file is safe to remove.") + CreateAndWriteTempFile(fileName, []byte(fileBody)) + + defer RemoveTempFilesDirectory() + + file, err := OpenTempFile(fileName) + if err != nil { + t.Errorf("%s could not be opened for test", fileName) + } + defer file.Close() + + actualName := file.Name() + if actualName != filepath.Join(writeBufferFolder, fileName) { + t.Errorf("Name() = %s; want %s", actualName, fileName) + } + + actualBody := []byte{} + _, err = file.Read(actualBody) + if err != nil { + t.Errorf("%s could not be read for test", fileName) + } + if !reflect.DeepEqual(actualBody, fileBody) { + t.Errorf("Read() = %s; want %s", actualBody, fileBody) + } +} + +func TestRemoveTempFilesDirectory(t *testing.T) { + if err := os.MkdirAll(writeBufferFolder, 0777); err != nil { + t.Errorf("%s could not be created for test", writeBufferFolder) + } + + err := RemoveTempFilesDirectory() + if err != nil { + t.Errorf("Error removing temp folder: %s", err) + } + + if fileExists((writeBufferFolder)) { + t.Errorf("RemoveTempFilesDirectory(): folder exists, want no folder.") + } +}