Skip to content

Commit

Permalink
1. Unifying work with FS via VFS
Browse files Browse the repository at this point in the history
2. Unifying work with FS via VFS
  • Loading branch information
BorzdeG committed Jan 18, 2025
1 parent a2b2d60 commit a7b9753
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 106 deletions.
92 changes: 5 additions & 87 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= test utils
= Test utils
:toc: macro
---

Expand All @@ -14,94 +14,12 @@ toc::[]

== Install module

```
go get -u github.com/github.com/itbasis/go-test-utils/v4
```

== File utils

* link:files/files.go[] - Reads the contents of a file and checks that there was no error while reading.

Real file system:
[source,go]
----
package demo_test
import (
"os"
"github.com/itbasis/go-test-utils/v4/files"
"github.com/onsi/ginkgo/v2"
)
var _ = ginkgo.Describe("Real OS", func(){
content := files.ReadFile(os.ReadFile, "example.txt")
})
----

Embedded File System:
[source,go]
----
package demo_test
import (
"embed"
"github.com/itbasis/go-test-utils/v4/files"
"github.com/onsi/ginkgo/v2"
)
//go:embed example.txt
var testData embed.FS
var _ = ginkgo.Describe("Embedded FS", func(){
content := files.ReadFile(testData.ReadFile, "example.txt")
})
go get -u github.com/github.com/itbasis/go-test-utils/v5
----

== Database utils
include::files/README.adoc[]

* link:db/sql.go[] - Getting a DB mock and SqlMock without SqlMock options (example option: link:https://github.com/DATA-DOG/go-sqlmock?tab=readme-ov-file#customize-sql-query-matching[QueryMatcherOptions])
* link:db/gorm.go[] - Getting a GORM and SqlMock instances
include::db/README.adoc[]

== Ginkgo utils

* link:ginkgo/ginkgo.go[] - Adds slog support to tests and code under test

[source,go]
----
package demo_test
import (
"testing"
"github.com/itbasis/go-test-utils/v4/ginkgo"
)
func TestSuite(t *testing.T) {
ginkgo.InitGinkgoSuite(t, "Sample Suite")
}
----

Custom slog options

[source,go]
----
package demo_test
import (
"log/slog"
"testing"
"github.com/itbasis/go-test-utils/v4/ginkgo"
)
func TestDemoSuite(t *testing.T) {
ginkgo.InitGinkgoSuiteWithSlogOptions(t, "Sample Suite", &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: false,
})
}
----
include::ginkgo/README.adoc[]
4 changes: 4 additions & 0 deletions db/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
== Database utils

* link:sql.go[] - Getting a DB mock and SqlMock without SqlMock options (example option: link:https://github.com/DATA-DOG/go-sqlmock?tab=readme-ov-file#customize-sql-query-matching[QueryMatcherOptions])
* link:gorm.go[] - Getting a GORM and SqlMock instances
21 changes: 21 additions & 0 deletions files/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
== File utils

* link:files.go[] - Reads the contents of a file and checks that there was no error while reading.
OS file system (link:files_test.go[]):

[source,go]
----
include::files_test.go[tag=snippet]
include::files_test.go[tag=snippet_os]
include::files_test.go[tag=snippet_reader_os]
----

For any `fs.FS` implementation, for example - Embedded FS (link:files_test.go[]):

[source,go]
----
include::files_test.go[tag=snippet]
include::files_test.go[tag=snippet_embedded]
include::files_test.go[tag=snippet_reader_embedded]
----
29 changes: 25 additions & 4 deletions files/files.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
package files

import (
"io"
"log/slog"

. "github.com/onsi/gomega"
"golang.org/x/tools/godoc/vfs"
)

type ReadFileFn func(filename string) ([]byte, error)
func Close(closer io.Closer) {
if closer == nil {
slog.Warn("closer is nil")

func ReadFile(readFileFn ReadFileFn, filePath string) []byte {
slog.Info("reading file", slog.String("file", filePath))
return
}

Expect(closer.Close()).To(Succeed())
}

bytes, err := readFileFn(filePath)
func ReadFile(fs vfs.Opener, filePath string) []byte {
fileReader := FileReader(fs, filePath)

defer Close(fileReader)

bytes, err := io.ReadAll(fileReader)
Expect(err).To(Succeed())

return bytes
}

func FileReader(fs vfs.Opener, filePath string) vfs.ReadSeekCloser {
slog.Info("reading file", slog.String("file", filePath))

rsc, err := fs.Open(filePath)
Expect(err).To(Succeed())

return rsc
}
2 changes: 1 addition & 1 deletion files/files_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package files_test
import (
"testing"

"github.com/itbasis/go-test-utils/v4/ginkgo"
"github.com/itbasis/go-test-utils/v5/ginkgo"
)

func TestFiles(t *testing.T) {
Expand Down
97 changes: 86 additions & 11 deletions files/files_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,108 @@
package files_test

// tag::snippet[]
import (
"embed"
"os"
"io"
"io/fs"

"github.com/itbasis/go-test-utils/v4/files"
"github.com/itbasis/go-test-utils/v5/files"
"golang.org/x/tools/godoc/vfs"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

var _entries = []ginkgo.TableEntry{
ginkgo.Entry(nil, "testdata/001.txt", "test 001\n"),
ginkgo.Entry(nil, "testdata/002.txt", "test 002\n"),
}

// end::snippet[]

// tag::snippet_os[]
var _ = ginkgo.DescribeTable(
"ReadFile :: OS", func(fileName, wantContent string) {
gomega.Expect(fileName).To(gomega.BeARegularFile())

gomega.Expect(files.ReadFile(vfs.OS("."), fileName)).To(gomega.BeEquivalentTo(wantContent))
},
_entries,
)

// end::snippet_os[]

// tag::snippet_reader_os[]
var _ = ginkgo.DescribeTable(
"FileReader :: OS", func(fileName, wantContent string) {
var reader = files.FileReader(vfs.OS("."), fileName)

defer files.Close(reader)

gomega.Expect(io.ReadAll(reader)).To(gomega.BeEquivalentTo(wantContent))
},
_entries,
)

// end::snippet_reader_os[]

// tag::snippet_embedded[]

//go:embed testdata/*
var testData embed.FS

var _ = ginkgo.DescribeTableSubtree(
"ReadFile", func(fileName, wantContent string) {
var _ = ginkgo.DescribeTable(
"ReadFile :: Embedded FS", func(fileName, wantContent string) {
gomega.Expect(files.ReadFile(vfs.FromFS(testData), fileName)).To(gomega.BeEquivalentTo(wantContent))
},
_entries,
)

// end::snippet_embedded[]

// tag::snippet_reader_embedded[]
var _ = ginkgo.DescribeTable(
"FileReader :: Embedded FS", func(fileName, wantContent string) {
var reader = files.FileReader(vfs.FromFS(testData), fileName)

defer files.Close(reader)

gomega.Expect(io.ReadAll(reader)).To(gomega.BeEquivalentTo(wantContent))
},
_entries,
)

// end::snippet_reader_embedded[]

var _ = ginkgo.Describe(
"Close", func() {
defer ginkgo.GinkgoRecover()

ginkgo.It(
"Real OS", func() {
gomega.Expect(fileName).To(gomega.BeARegularFile())
gomega.Expect(files.ReadFile(os.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
"Close nil", func() {
gomega.Expect(
gomega.InterceptGomegaFailure(
func() {
files.Close(io.Closer(nil))
},
),
).To(gomega.Succeed())
},
)

ginkgo.It(
"Embedded FS", func() {
gomega.Expect(files.ReadFile(testData.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
"Fail close", func() {
var reader = files.FileReader(vfs.OS("."), "testdata/001.txt")

gomega.Expect(reader.Close()).To(gomega.Succeed())
gomega.Expect(
gomega.InterceptGomegaFailure(
func() {
files.Close(reader)
},
),
).Error().To(gomega.MatchError(gomega.ContainSubstring(fs.ErrClosed.Error())))
},
)
},
ginkgo.Entry(nil, "testdata/001.txt", "test 001\n"),
ginkgo.Entry(nil, "testdata/002.txt", "test 002\n"),
)
39 changes: 39 additions & 0 deletions ginkgo/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
== Ginkgo utils

* link:ginkgo/ginkgo.go[] - Adds slog support to tests and code under test
[source,go]
----
package demo_test
import (
"testing"
"github.com/itbasis/go-test-utils/v5/ginkgo"
)
func TestSuite(t *testing.T) {
ginkgo.InitGinkgoSuite(t, "Sample Suite")
}
----

Custom slog options

[source,go]
----
package demo_test
import (
"log/slog"
"testing"
"github.com/itbasis/go-test-utils/v5/ginkgo"
)
func TestDemoSuite(t *testing.T) {
ginkgo.InitGinkgoSuiteWithSlogOptions(t, "Sample Suite", &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: false,
})
}
----
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module github.com/itbasis/go-test-utils/v4
module github.com/itbasis/go-test-utils/v5

go 1.22.10
go 1.22.11

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/dusted-go/logging v1.3.0
github.com/onsi/ginkgo/v2 v2.22.2
github.com/onsi/gomega v1.36.2
github.com/orandin/slog-gorm v1.4.0
golang.org/x/tools v0.29.0
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.25.12
)
Expand All @@ -30,6 +31,5 @@ require (
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.29.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

0 comments on commit a7b9753

Please sign in to comment.