-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Unifying work with FS via VFS
- Loading branch information
Showing
8 changed files
with
184 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters