Skip to content

Commit 828149b

Browse files
committed
1. Added support for embedded FS for reading files
2. Refactoring build and GitHub Actions
1 parent 1072119 commit 828149b

File tree

10 files changed

+147
-48
lines changed

10 files changed

+147
-48
lines changed

.github/workflows/test-matrix.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010

1111
strategy:
1212
matrix:
13-
go: ["1.22.x", "stable"]
13+
go: ["1.22.x","1.23.x", "oldstable", "stable"]
1414
continueOnError: [false]
1515
# include:
1616
# - go: ">=1.23.0-rc.1"
@@ -25,10 +25,27 @@ jobs:
2525
go-version: ${{ matrix.go }}
2626
check-latest: true
2727

28+
- run: make go-dependencies
29+
- run: make go-generate && git diff --exit-code
2830
- run: make go-all-tests
2931

30-
# - name: Test Summary
31-
# uses: test-summary/action@v2
32-
# with:
33-
# paths: "junit-report.xml"
34-
# if: always()
32+
- name: Test Summary
33+
uses: test-summary/action@v2
34+
with:
35+
paths: "junit-report.xml"
36+
if: always()
37+
38+
- name: Upload test results to Codecov
39+
if: ${{ !cancelled() }}
40+
uses: codecov/test-results-action@v1
41+
with:
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
files: "junit-report.xml"
44+
verbose: true
45+
46+
- name: Upload coverage reports to Codecov
47+
uses: codecov/codecov-action@v4
48+
with:
49+
token: ${{ secrets.CODECOV_TOKEN }}
50+
files: ./coverage-unit.out
51+
verbose: true

.github/workflows/test.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ jobs:
2727
check-latest: true
2828

2929
- run: make go-dependencies
30-
3130
- run: make go-generate && git diff --exit-code
32-
3331
- run: make go-all-tests
3432

35-
# - name: Test Summary
36-
# uses: test-summary/action@v2
37-
# with:
38-
# paths: "junit-report.xml"
39-
# if: always()
40-
#
41-
# - name: Upload coverage reports to Codecov
42-
# uses: codecov/codecov-action@v4
43-
# with:
44-
# token: ${{ secrets.CODECOV_TOKEN }}
45-
# files: ./.coverage-ginkgo.out
46-
#
47-
# - name: Upload test results to Codecov
48-
# if: ${{ !cancelled() }}
49-
# uses: codecov/test-results-action@v1
50-
# with:
51-
# token: ${{ secrets.CODECOV_TOKEN }}
52-
# files: "junit-report.xml"
53-
# verbose: true
33+
- name: Test Summary
34+
uses: test-summary/action@v2
35+
with:
36+
paths: "junit-report.xml"
37+
if: always()
38+
39+
- name: Upload test results to Codecov
40+
if: ${{ !cancelled() }}
41+
uses: codecov/test-results-action@v1
42+
with:
43+
token: ${{ secrets.CODECOV_TOKEN }}
44+
files: "junit-report.xml"
45+
verbose: true
46+
47+
- name: Upload coverage reports to Codecov
48+
if: ${{ !cancelled() }}
49+
uses: codecov/codecov-action@v4
50+
with:
51+
token: ${{ secrets.CODECOV_TOKEN }}
52+
files: ./coverage-unit.out
53+
verbose: true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ tmp.*
33

44
# Go
55
bin/
6-
report.xml
6+
coverage-*.*
7+
ginkgo-*.*
8+
junit-report.xml
79

810
# JetBrains
911
.idea/*

Makefile

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
go-all: go-update go-generate go-all-tests
2+
go-all-tests: go-lint go-unit-tests
3+
14
go-dependencies:
25
$(eval GOBIN=$(shell go env GOPATH 2>/dev/null)/bin)
36
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) latest
@@ -7,20 +10,16 @@ go-dependencies:
710
go-update: go-dependencies
811
go mod tidy && go get -t -v -u ./...
912

10-
go-generate: go-dependencies
13+
go-generate:
1114
go generate ./...
1215
$(MAKE) go-update
1316

14-
go-lint: go-dependencies
17+
go-lint:
1518
golangci-lint run
1619

17-
go-test: go-lint
18-
#ginkgo -race --cover --coverprofile=.coverage-ginkgo.out --junit-report=junit-report.xml ./...
19-
#go tool cover -func=.coverage-ginkgo.out -o=.coverage.out
20-
#go tool cover -html=.coverage-ginkgo.out -o=coverage.html
21-
#cat .coverage.out
22-
23-
go-all-tests: go-generate go-lint go-test
20+
go-unit-tests:
21+
ginkgo -race --cover --coverprofile="ginkgo-coverage-unit.out" --junit-report="junit-report.xml" ./...
22+
go tool cover -func "ginkgo-coverage-unit.out" -o "coverage-unit.out"
23+
go tool cover -html "ginkgo-coverage-unit.out" -o "coverage-unit.html"
2424

25-
go-all: go-dependencies go-all-tests
26-
go mod tidy || :
25+
cat coverage-unit.out

README.adoc

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,44 @@ go get -u github.com/itbasis/go-test-utils/v4
2020

2121
== File utils
2222

23-
* link:files/files.go[] - checks if the path is a regular file and reads its contents
23+
* link:files/files.go[] - Reads the contents of a file and checks that there was no error while reading.
24+
25+
Real file system:
26+
[source,go]
27+
----
28+
package demo_test
29+
30+
import (
31+
"os"
32+
33+
"github.com/itbasis/go-test-utils/v4/files"
34+
"github.com/onsi/ginkgo/v2"
35+
)
36+
37+
var _ = ginkgo.Describe("Real OS", func(){
38+
content := files.ReadFile(os.ReadFile, "example.txt")
39+
})
40+
----
41+
42+
Embedded File System:
43+
[source,go]
44+
----
45+
package demo_test
46+
47+
import (
48+
"embed"
49+
50+
"github.com/itbasis/go-test-utils/v4/files"
51+
"github.com/onsi/ginkgo/v2"
52+
)
53+
54+
//go:embed example.txt
55+
var testData embed.FS
56+
57+
var _ = ginkgo.Describe("Real OS", func(){
58+
content := files.ReadFile(testData.ReadFile, "example.txt")
59+
})
60+
----
2461

2562
== Database utils
2663

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

3471
[source,go]
3572
----
36-
package demo
73+
package demo_test
3774
3875
import (
3976
"testing"
@@ -50,7 +87,7 @@ Custom slog options
5087

5188
[source,go]
5289
----
53-
package demo
90+
package demo_test
5491
5592
import (
5693
"log/slog"
@@ -59,7 +96,7 @@ import (
5996
"github.com/itbasis/go-test-utils/v4/ginkgo"
6097
)
6198
62-
func TestSuite(t *testing.T) {
99+
func TestDemoSuite(t *testing.T) {
63100
ginkgo.InitGinkgoSuiteWithSlogOptions(t, "Sample Suite", &slog.HandlerOptions{
64101
Level: slog.LevelDebug,
65102
AddSource: false,

files/files.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package files
22

33
import (
44
"log/slog"
5-
"os"
65
"path/filepath"
76

87
. "github.com/onsi/gomega"
98
)
109

11-
func ReadFile(filePath string) []byte {
12-
slog.Info("reading file", slog.String("file", filePath))
10+
type ReadFileFn func(filename string) ([]byte, error)
1311

14-
Expect(filePath).To(BeARegularFile())
12+
func ReadFile(readFileFn ReadFileFn, filePath string) []byte {
13+
slog.Info("reading file", slog.String("file", filePath))
1514

16-
bytes, err := os.ReadFile(filepath.Clean(filePath))
15+
bytes, err := readFileFn(filepath.Clean(filePath))
1716
Expect(err).To(Succeed())
1817

1918
return bytes

files/files_suite_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package files_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/itbasis/go-test-utils/v4/ginkgo"
7+
)
8+
9+
func TestFiles(t *testing.T) {
10+
ginkgo.InitGinkgoSuite(t, "Files Suite")
11+
}

files/files_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package files_test
2+
3+
import (
4+
"embed"
5+
"os"
6+
7+
"github.com/itbasis/go-test-utils/v4/files"
8+
"github.com/onsi/ginkgo/v2"
9+
"github.com/onsi/gomega"
10+
)
11+
12+
//go:embed testdata/*
13+
var testData embed.FS
14+
15+
var _ = ginkgo.DescribeTableSubtree(
16+
"ReadFile", func(fileName, wantContent string) {
17+
ginkgo.It(
18+
"Real OS", func() {
19+
gomega.Expect(fileName).To(gomega.BeARegularFile())
20+
gomega.Expect(files.ReadFile(os.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
21+
},
22+
)
23+
24+
ginkgo.It(
25+
"Embedded FS", func() {
26+
gomega.Expect(files.ReadFile(testData.ReadFile, fileName)).To(gomega.BeEquivalentTo(wantContent))
27+
},
28+
)
29+
},
30+
ginkgo.Entry(nil, "testdata/001.txt", "test 001\n"),
31+
ginkgo.Entry(nil, "testdata/002.txt", "test 002\n"),
32+
)

files/testdata/001.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 001

files/testdata/002.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 002

0 commit comments

Comments
 (0)