Skip to content

Commit

Permalink
1. Added support for embedded FS for reading files
Browse files Browse the repository at this point in the history
2. Refactoring build and GitHub Actions
  • Loading branch information
BorzdeG committed Aug 14, 2024
1 parent 1072119 commit 828149b
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 48 deletions.
29 changes: 23 additions & 6 deletions .github/workflows/test-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
go: ["1.22.x", "stable"]
go: ["1.22.x","1.23.x", "oldstable", "stable"]
continueOnError: [false]
# include:
# - go: ">=1.23.0-rc.1"
Expand All @@ -25,10 +25,27 @@ jobs:
go-version: ${{ matrix.go }}
check-latest: true

- run: make go-dependencies
- run: make go-generate && git diff --exit-code
- run: make go-all-tests

# - name: Test Summary
# uses: test-summary/action@v2
# with:
# paths: "junit-report.xml"
# if: always()
- name: Test Summary
uses: test-summary/action@v2
with:
paths: "junit-report.xml"
if: always()

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: "junit-report.xml"
verbose: true

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage-unit.out
verbose: true
42 changes: 21 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ jobs:
check-latest: true

- run: make go-dependencies

- run: make go-generate && git diff --exit-code

- run: make go-all-tests

# - name: Test Summary
# uses: test-summary/action@v2
# with:
# paths: "junit-report.xml"
# if: always()
#
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v4
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# files: ./.coverage-ginkgo.out
#
# - name: Upload test results to Codecov
# if: ${{ !cancelled() }}
# uses: codecov/test-results-action@v1
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# files: "junit-report.xml"
# verbose: true
- name: Test Summary
uses: test-summary/action@v2
with:
paths: "junit-report.xml"
if: always()

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: "junit-report.xml"
verbose: true

- name: Upload coverage reports to Codecov
if: ${{ !cancelled() }}
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage-unit.out
verbose: true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ tmp.*

# Go
bin/
report.xml
coverage-*.*
ginkgo-*.*
junit-report.xml

# JetBrains
.idea/*
Expand Down
21 changes: 10 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
go-all: go-update go-generate go-all-tests
go-all-tests: go-lint go-unit-tests

go-dependencies:
$(eval GOBIN=$(shell go env GOPATH 2>/dev/null)/bin)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) latest
Expand All @@ -7,20 +10,16 @@ go-dependencies:
go-update: go-dependencies
go mod tidy && go get -t -v -u ./...

go-generate: go-dependencies
go-generate:
go generate ./...
$(MAKE) go-update

go-lint: go-dependencies
go-lint:
golangci-lint run

go-test: go-lint
#ginkgo -race --cover --coverprofile=.coverage-ginkgo.out --junit-report=junit-report.xml ./...
#go tool cover -func=.coverage-ginkgo.out -o=.coverage.out
#go tool cover -html=.coverage-ginkgo.out -o=coverage.html
#cat .coverage.out

go-all-tests: go-generate go-lint go-test
go-unit-tests:
ginkgo -race --cover --coverprofile="ginkgo-coverage-unit.out" --junit-report="junit-report.xml" ./...
go tool cover -func "ginkgo-coverage-unit.out" -o "coverage-unit.out"
go tool cover -html "ginkgo-coverage-unit.out" -o "coverage-unit.html"

go-all: go-dependencies go-all-tests
go mod tidy || :
cat coverage-unit.out
45 changes: 41 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,44 @@ go get -u github.com/itbasis/go-test-utils/v4

== File utils

* link:files/files.go[] - checks if the path is a regular file and reads its contents
* 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("Real OS", func(){
content := files.ReadFile(testData.ReadFile, "example.txt")
})
----

== Database utils

Expand All @@ -33,7 +70,7 @@ go get -u github.com/itbasis/go-test-utils/v4

[source,go]
----
package demo
package demo_test
import (
"testing"
Expand All @@ -50,7 +87,7 @@ Custom slog options

[source,go]
----
package demo
package demo_test
import (
"log/slog"
Expand All @@ -59,7 +96,7 @@ import (
"github.com/itbasis/go-test-utils/v4/ginkgo"
)
func TestSuite(t *testing.T) {
func TestDemoSuite(t *testing.T) {
ginkgo.InitGinkgoSuiteWithSlogOptions(t, "Sample Suite", &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: false,
Expand Down
9 changes: 4 additions & 5 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package files

import (
"log/slog"
"os"
"path/filepath"

. "github.com/onsi/gomega"
)

func ReadFile(filePath string) []byte {
slog.Info("reading file", slog.String("file", filePath))
type ReadFileFn func(filename string) ([]byte, error)

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

bytes, err := os.ReadFile(filepath.Clean(filePath))
bytes, err := readFileFn(filepath.Clean(filePath))
Expect(err).To(Succeed())

return bytes
Expand Down
11 changes: 11 additions & 0 deletions files/files_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package files_test

import (
"testing"

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

func TestFiles(t *testing.T) {
ginkgo.InitGinkgoSuite(t, "Files Suite")
}
32 changes: 32 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package files_test

import (
"embed"
"os"

"github.com/itbasis/go-test-utils/v4/files"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

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

var _ = ginkgo.DescribeTableSubtree(
"ReadFile", func(fileName, wantContent string) {
ginkgo.It(
"Real OS", func() {
gomega.Expect(fileName).To(gomega.BeARegularFile())
gomega.Expect(files.ReadFile(os.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
},
)

ginkgo.It(
"Embedded FS", func() {
gomega.Expect(files.ReadFile(testData.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
},
)
},
ginkgo.Entry(nil, "testdata/001.txt", "test 001\n"),
ginkgo.Entry(nil, "testdata/002.txt", "test 002\n"),
)
1 change: 1 addition & 0 deletions files/testdata/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test 001
1 change: 1 addition & 0 deletions files/testdata/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test 002

0 comments on commit 828149b

Please sign in to comment.