Skip to content

Commit 9aef707

Browse files
qnqatopandrey
andauthored
Fix test path handling (#51)
* Update README, add test with one entity * delete empty rows and use So(err, ShouldBeNil) * Fix test path handling * Update GitHub Actions to use newer versions * Update test generators to use temp dirs --------- Co-authored-by: andrey <[email protected]>
1 parent 4e94722 commit 9aef707

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

.github/workflows/go.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ jobs:
2525
--health-retries 5
2626
steps:
2727
- name: Set up Go
28-
uses: actions/setup-go@v2
28+
uses: actions/setup-go@v5
2929
with:
30-
go-version: '1.22.x'
30+
go-version: '1.24.x'
3131

32-
- uses: actions/checkout@v2
32+
- uses: actions/checkout@v3
3333

3434
- name: Prepare test db
3535
run: psql -d postgresql://postgres@localhost/newsportal < schema.sql
@@ -43,12 +43,12 @@ jobs:
4343
build:
4444
runs-on: ubuntu-latest
4545
steps:
46-
- uses: actions/checkout@v2
46+
- uses: actions/checkout@v3
4747

4848
- name: Set up Go
49-
uses: actions/setup-go@v2
49+
uses: actions/setup-go@v5
5050
with:
51-
go-version: '1.22.x'
51+
go-version: '1.24.x'
5252

5353
- name: Build
5454
run: go build -v ./...

.github/workflows/goreleaser.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ jobs:
1919
fetch-depth: 0
2020
-
2121
name: Set up Go
22-
uses: actions/setup-go@v3
22+
uses: actions/setup-go@v5
2323
with:
24-
go-version: '1.22.x'
24+
go-version: '1.24.x'
2525
-
2626
name: Run GoReleaser
27-
uses: goreleaser/goreleaser-action@v4
27+
uses: goreleaser/goreleaser-action@v6
2828
with:
2929
distribution: goreleaser
3030
version: latest

generators/xml-lang/generator_test.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,34 @@ import (
1313
// todo: add generator.options.Output for generate to actual directory. Now it generate in expected dir where located .mfd
1414
// todo: not generate if *.vt.xml not exists, but err == nil
1515
func TestGenerator_Generate(t *testing.T) {
16-
err := prepareFiles()
16+
actualDir := t.TempDir()
17+
err := prepareFiles(actualDir)
1718
if err != nil {
1819
t.Fatal(err)
1920
}
21+
mfdPathInActual := filepath.Join(actualDir, filepath.Base(testdata.PathExpectedMFD))
2022

2123
Convey("TestGenerator_Generate", t, func() {
2224
Convey("Generate with Entity flag", func() {
2325
generator := New()
24-
generator.options.MFDPath = filepath.Join(testdata.PathActualMFD)
26+
generator.options.MFDPath = mfdPathInActual
2527
generator.options.Entities = []string{"category"}
2628

2729
t.Log("Generate only entity news xml-vt")
2830
err = generator.Generate()
2931
So(err, ShouldBeNil)
3032

3133
t.Logf("Check %s file", "en-one-entity.xml")
32-
content, err := os.ReadFile(filepath.Join(testdata.PathActual, "en.xml"))
34+
content, err := os.ReadFile(filepath.Join(actualDir, "en.xml"))
3335
So(err, ShouldBeNil)
3436
expectedContent, err := os.ReadFile(filepath.Join(testdata.PathExpected, "en-one-entity.xml"))
3537
So(err, ShouldBeNil)
3638
So(content, ShouldResemble, expectedContent)
37-
3839
})
3940

4041
Convey("Check correct generate", func() {
4142
generator := New()
42-
generator.options.MFDPath = testdata.PathActualMFD
43+
generator.options.MFDPath = mfdPathInActual
4344

4445
t.Log("Generate xml-vt")
4546
err = generator.Generate()
@@ -53,7 +54,7 @@ func TestGenerator_Generate(t *testing.T) {
5354

5455
for f := range expectedFilenames {
5556
t.Logf("Check %s file", f)
56-
content, err := os.ReadFile(filepath.Join(testdata.PathActual, f))
57+
content, err := os.ReadFile(filepath.Join(actualDir, f))
5758
So(err, ShouldBeNil)
5859
expectedContent, err := os.ReadFile(filepath.Join(testdata.PathExpected, f))
5960
So(err, ShouldBeNil)
@@ -63,29 +64,18 @@ func TestGenerator_Generate(t *testing.T) {
6364
})
6465
}
6566

66-
func prepareFiles() error {
67-
// clearing actual test data
68-
err := os.RemoveAll(testdata.PathActual)
69-
if err != nil {
70-
return err
71-
}
72-
73-
err = os.MkdirAll(testdata.PathActual, 0775)
74-
if err != nil {
75-
return err
76-
}
77-
78-
err = os.Link(testdata.PathExpectedMFD, testdata.PathActualMFD)
67+
func prepareFiles(actualPath string) error {
68+
err := os.Link(testdata.PathExpectedMFD, filepath.Join(actualPath, filepath.Base(testdata.PathExpectedMFD)))
7969
if err != nil && !os.IsExist(err) {
8070
return err
8171
}
8272

83-
err = os.Link(filepath.Join(testdata.PathExpected, testdata.FilenameXML), filepath.Join(testdata.PathActual, testdata.FilenameXML))
73+
err = os.Link(filepath.Join(testdata.PathExpected, testdata.FilenameXML), filepath.Join(actualPath, testdata.FilenameXML))
8474
if err != nil && !os.IsExist(err) {
8575
return err
8676
}
8777

88-
err = os.Link(filepath.Join(testdata.PathExpected, testdata.FilenameVTXML), filepath.Join(testdata.PathActual, testdata.FilenameVTXML))
78+
err = os.Link(filepath.Join(testdata.PathExpected, testdata.FilenameVTXML), filepath.Join(actualPath, testdata.FilenameVTXML))
8979
if err != nil && !os.IsExist(err) {
9080
return err
9181
}

generators/xml/generator_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
// todo: panic if ../testdata/actual/*.mfd exists and xml not exist
1414
func TestGenerator_Generate(t *testing.T) {
1515
// Store the PATH environment variable in a variable
16+
actualDir := t.TempDir()
17+
mfdPathInActual := filepath.Join(actualDir, filepath.Base(testdata.PathExpectedMFD))
18+
1619
dbdsn, exists := os.LookupEnv("DB_DSN")
1720
if !exists {
1821
dbdsn = "postgres://postgres:postgres@localhost:5432/newsportal?sslmode=disable"
@@ -24,7 +27,7 @@ func TestGenerator_Generate(t *testing.T) {
2427

2528
generator.options.Def()
2629
generator.options.URL = dbdsn
27-
generator.options.Output = testdata.PathActualMFD
30+
generator.options.Output = mfdPathInActual
2831
generator.options.Packages = parseNamespacesFlag("portal:news,categories,tags")
2932

3033
t.Log("Generate xml")
@@ -39,7 +42,7 @@ func TestGenerator_Generate(t *testing.T) {
3942

4043
for f := range expectedFilenames {
4144
t.Logf("Check %s file", f)
42-
content, err := os.ReadFile(filepath.Join(testdata.PathActual, f))
45+
content, err := os.ReadFile(filepath.Join(actualDir, f))
4346
if err != nil {
4447
t.Fatal(err)
4548
}

0 commit comments

Comments
 (0)