Skip to content

Commit

Permalink
Introduce package testutils (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp authored Sep 4, 2024
1 parent 7cac376 commit 5e39de9
Show file tree
Hide file tree
Showing 19 changed files with 718 additions and 748 deletions.
175 changes: 88 additions & 87 deletions pkg/specter/artifactproc_fileartifact_test.go

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions pkg/specter/artifactproc_registry_inmemory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@
package specter_test

import (
. "github.com/morebec/specter/pkg/specter"
"github.com/morebec/specter/pkg/specter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestInMemoryArtifactRegistry_InterfaceCompliance(t *testing.T) {
// InMemoryArtifactRegistry
assertArtifactRegistryCompliance(t, "InMemoryArtifactRegistry", func() *InMemoryArtifactRegistry {
return &InMemoryArtifactRegistry{}
assertArtifactRegistryCompliance(t, "InMemoryArtifactRegistry", func() *specter.InMemoryArtifactRegistry {
return &specter.InMemoryArtifactRegistry{}
})
}

func TestInMemoryArtifactRegistry_Add(t *testing.T) {
type given struct {
EntriesMap map[string][]ArtifactRegistryEntry
EntriesMap map[string][]specter.ArtifactRegistryEntry
}
type when struct {
processorName string
entry ArtifactRegistryEntry
entry specter.ArtifactRegistryEntry
}
type then struct {
expectedError assert.ErrorAssertionFunc
expectedEntries []ArtifactRegistryEntry
expectedEntries []specter.ArtifactRegistryEntry
}
tests := []struct {
name string
Expand All @@ -49,16 +49,16 @@ func TestInMemoryArtifactRegistry_Add(t *testing.T) {
{
name: "Given non nil entry map When an entry is added Then the entry should be in the registry",
given: given{
EntriesMap: map[string][]ArtifactRegistryEntry{},
EntriesMap: map[string][]specter.ArtifactRegistryEntry{},
},
when: when{
processorName: "processor1",
entry: ArtifactRegistryEntry{
entry: specter.ArtifactRegistryEntry{
ArtifactID: "an_artifact",
},
},
then: then{
expectedEntries: []ArtifactRegistryEntry{
expectedEntries: []specter.ArtifactRegistryEntry{
{
ArtifactID: "an_artifact",
},
Expand All @@ -73,12 +73,12 @@ func TestInMemoryArtifactRegistry_Add(t *testing.T) {
},
when: when{
processorName: "processor1",
entry: ArtifactRegistryEntry{
entry: specter.ArtifactRegistryEntry{
ArtifactID: "an_artifact",
},
},
then: then{
expectedEntries: []ArtifactRegistryEntry{
expectedEntries: []specter.ArtifactRegistryEntry{
{
ArtifactID: "an_artifact",
},
Expand All @@ -89,7 +89,7 @@ func TestInMemoryArtifactRegistry_Add(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &InMemoryArtifactRegistry{
r := &specter.InMemoryArtifactRegistry{
EntriesMap: tt.given.EntriesMap,
}
err := r.Add(tt.when.processorName, tt.when.entry)
Expand All @@ -105,13 +105,13 @@ func TestInMemoryArtifactRegistry_Add(t *testing.T) {

func TestInMemoryArtifactRegistry_FindAll(t *testing.T) {
type given struct {
EntriesMap map[string][]ArtifactRegistryEntry
EntriesMap map[string][]specter.ArtifactRegistryEntry
}
type when struct {
processorName string
}
type then struct {
expectedArtifacts []ArtifactRegistryEntry
expectedArtifacts []specter.ArtifactRegistryEntry
expectedError assert.ErrorAssertionFunc
}
tests := []struct {
Expand All @@ -136,7 +136,7 @@ func TestInMemoryArtifactRegistry_FindAll(t *testing.T) {
{
name: "Given non nil empty entry map Then return nil",
given: given{
EntriesMap: map[string][]ArtifactRegistryEntry{},
EntriesMap: map[string][]specter.ArtifactRegistryEntry{},
},
when: when{
processorName: "unit_tester",
Expand All @@ -149,7 +149,7 @@ func TestInMemoryArtifactRegistry_FindAll(t *testing.T) {
{
name: "Given nil slice for processor name Then return nil",
given: given{
EntriesMap: map[string][]ArtifactRegistryEntry{
EntriesMap: map[string][]specter.ArtifactRegistryEntry{
"unit_tester": nil,
},
},
Expand All @@ -164,7 +164,7 @@ func TestInMemoryArtifactRegistry_FindAll(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &InMemoryArtifactRegistry{
r := &specter.InMemoryArtifactRegistry{
EntriesMap: tt.given.EntriesMap,
}
artifacts, err := r.FindAll(tt.when.processorName)
Expand All @@ -177,25 +177,25 @@ func TestInMemoryArtifactRegistry_FindAll(t *testing.T) {
}

func TestInMemoryArtifactRegistry_Load(t *testing.T) {
r := &InMemoryArtifactRegistry{}
r := &specter.InMemoryArtifactRegistry{}
require.Nil(t, r.Load())
}

func TestInMemoryArtifactRegistry_Save(t *testing.T) {
r := &InMemoryArtifactRegistry{}
r := &specter.InMemoryArtifactRegistry{}
require.Nil(t, r.Save())
}

func TestInMemoryArtifactRegistry_Remove(t *testing.T) {
type given struct {
EntriesMap map[string][]ArtifactRegistryEntry
EntriesMap map[string][]specter.ArtifactRegistryEntry
}
type when struct {
processorName string
artifactID ArtifactID
artifactID specter.ArtifactID
}
type then struct {
expectedEntries []ArtifactRegistryEntry
expectedEntries []specter.ArtifactRegistryEntry
expectedError assert.ErrorAssertionFunc
}
tests := []struct {
Expand All @@ -221,7 +221,7 @@ func TestInMemoryArtifactRegistry_Remove(t *testing.T) {
{
name: "Given empty entry map Then return nil",
given: given{
EntriesMap: map[string][]ArtifactRegistryEntry{},
EntriesMap: map[string][]specter.ArtifactRegistryEntry{},
},
when: when{
processorName: "unit_tester",
Expand All @@ -235,7 +235,7 @@ func TestInMemoryArtifactRegistry_Remove(t *testing.T) {
{
name: "Given nil slice for processor Then return nil",
given: given{
EntriesMap: map[string][]ArtifactRegistryEntry{
EntriesMap: map[string][]specter.ArtifactRegistryEntry{
"unit_tester": nil,
},
},
Expand All @@ -251,7 +251,7 @@ func TestInMemoryArtifactRegistry_Remove(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &InMemoryArtifactRegistry{
r := &specter.InMemoryArtifactRegistry{
EntriesMap: tt.given.EntriesMap,
}

Expand Down
51 changes: 26 additions & 25 deletions pkg/specter/artifactproc_registry_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package specter_test

import (
. "github.com/morebec/specter/pkg/specter"
"github.com/morebec/specter/pkg/specter"
"github.com/morebec/specter/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
Expand All @@ -25,19 +26,19 @@ import (

func TestJSONArtifactRegistry_InterfaceCompliance(t *testing.T) {
// JSON Artifact Registry
assertArtifactRegistryCompliance(t, "JSONArtifactRegistry", func() *JSONArtifactRegistry {
return NewJSONArtifactRegistry(DefaultJSONArtifactRegistryFileName, &mockFileSystem{})
assertArtifactRegistryCompliance(t, "JSONArtifactRegistry", func() *specter.JSONArtifactRegistry {
return specter.NewJSONArtifactRegistry(specter.DefaultJSONArtifactRegistryFileName, &testutils.MockFileSystem{})
})
}

func TestJSONArtifactRegistry_Load(t *testing.T) {
type given struct {
jsonFileContent string
fileSystem FileSystem
fileSystem specter.FileSystem
}
type then struct {
expectedError require.ErrorAssertionFunc
expectedEntries map[string][]ArtifactRegistryEntry
expectedEntries map[string][]specter.ArtifactRegistryEntry
}
tests := []struct {
name string
Expand All @@ -47,7 +48,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
{
name: "Given entries in json file Then entries should be loaded Successfully",
given: given{
fileSystem: &mockFileSystem{},
fileSystem: &testutils.MockFileSystem{},
jsonFileContent: `{
"generatedAt" : "2024-01-01T00:00:00Z",
"entries" : {
Expand All @@ -60,7 +61,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
},
then: then{
expectedError: nil,
expectedEntries: map[string][]ArtifactRegistryEntry{
expectedEntries: map[string][]specter.ArtifactRegistryEntry{
"processor1": {
{
ArtifactID: "file1.txt",
Expand All @@ -72,7 +73,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
{
name: "Given file content is empty Then no entries should be loaded",
given: given{
fileSystem: &mockFileSystem{},
fileSystem: &testutils.MockFileSystem{},
jsonFileContent: "",
},
then: then{
Expand All @@ -83,7 +84,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
{
name: "Given JSON is malformed Then no entries should be loaded and an error should be returned",
given: given{
fileSystem: &mockFileSystem{},
fileSystem: &testutils.MockFileSystem{},
jsonFileContent: `{"entries":{`,
},
then: then{
Expand All @@ -95,7 +96,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
name: "Given json contains invalid entries Then entries should be loaded Successfully",
// Invalid entries being processors without a name or artifacts without an ID
given: given{
fileSystem: &mockFileSystem{},
fileSystem: &testutils.MockFileSystem{},
jsonFileContent: `{
"generatedAt" : "2024-01-01T00:00:00Z",
"entries" : {
Expand All @@ -113,8 +114,8 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
{
name: "Given file does not exist Then return no error",
given: given{
fileSystem: &mockFileSystem{
readFileErr: os.ErrNotExist,
fileSystem: &testutils.MockFileSystem{
ReadFileErr: os.ErrNotExist,
},
jsonFileContent: "",
},
Expand All @@ -126,8 +127,8 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
{
name: "Given filesystem returns an error when reading files Then return an error",
given: given{
fileSystem: &mockFileSystem{
readFileErr: assert.AnError,
fileSystem: &testutils.MockFileSystem{
ReadFileErr: assert.AnError,
},
jsonFileContent: "",
},
Expand All @@ -146,11 +147,11 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {

err := fs.WriteFile(filePath, []byte(tt.given.jsonFileContent), os.ModePerm)
require.NoError(t, err)
defer func(fs FileSystem, path string) {
defer func(fs specter.FileSystem, path string) {
require.NoError(t, fs.Remove(path))
}(fs, filePath)

registry := NewJSONArtifactRegistry(filePath, fs)
registry := specter.NewJSONArtifactRegistry(filePath, fs)
registry.TimeProvider = staticTimeProvider(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC))

err = registry.Load()
Expand All @@ -168,8 +169,8 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {

func TestJSONArtifactRegistry_Save(t *testing.T) {
type given struct {
entries map[string][]ArtifactRegistryEntry
fileSystem FileSystem
entries map[string][]specter.ArtifactRegistryEntry
fileSystem specter.FileSystem
}

type then struct {
Expand All @@ -185,8 +186,8 @@ func TestJSONArtifactRegistry_Save(t *testing.T) {
{
name: "GIVEN a set of entries THEN registry should be saved as JSON",
given: given{
fileSystem: &mockFileSystem{},
entries: map[string][]ArtifactRegistryEntry{
fileSystem: &testutils.MockFileSystem{},
entries: map[string][]specter.ArtifactRegistryEntry{
"processor1": {
{ArtifactID: "file1.txt"},
},
Expand All @@ -208,7 +209,7 @@ func TestJSONArtifactRegistry_Save(t *testing.T) {
{
name: "GIVEN no entries THEN registry should be saved as JSON successfully",
given: given{
fileSystem: &mockFileSystem{},
fileSystem: &testutils.MockFileSystem{},
entries: nil,
},
then: then{
Expand All @@ -222,8 +223,8 @@ func TestJSONArtifactRegistry_Save(t *testing.T) {
{
name: "GIVEN filesystem has errors writing files THEN an error should be returned",
given: given{
fileSystem: &mockFileSystem{
writeFileErr: assert.AnError,
fileSystem: &testutils.MockFileSystem{
WriteFileErr: assert.AnError,
},
entries: nil,
},
Expand All @@ -238,9 +239,9 @@ func TestJSONArtifactRegistry_Save(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
fs := tt.given.fileSystem

registryFilePath := DefaultJSONArtifactRegistryFileName
registryFilePath := specter.DefaultJSONArtifactRegistryFileName

registry := NewJSONArtifactRegistry(registryFilePath, fs)
registry := specter.NewJSONArtifactRegistry(registryFilePath, fs)
registry.TimeProvider = staticTimeProvider(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC))

// Add initial entries
Expand Down
Loading

0 comments on commit 5e39de9

Please sign in to comment.