Skip to content

Commit

Permalink
refactor(tests): remove TmpFile.Error to centralize error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Dec 1, 2023
1 parent 891d0fb commit 8c7b89c
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -80,7 +80,7 @@ linters-settings:
stylecheck:
# Whietlist dot imports for test packages.
dot-import-whitelist:
- "github.com/onsi/ginkgo"
- "github.com/onsi/ginkgo/v2"
- "github.com/onsi/gomega"
- "github.com/0xERR0R/blocky/config/migration"
- "github.com/0xERR0R/blocky/helpertest"
Expand Down
3 changes: 0 additions & 3 deletions cmd/root_test.go
Expand Up @@ -33,8 +33,6 @@ var _ = Describe("root command", func() {
configPath = defaultConfigPath

tmpDir = NewTmpFolder("RootCommand")
Expect(tmpDir.Error).Should(Succeed())

tmpFile = tmpDir.CreateStringFile("config",
"upstreams:",
" groups:",
Expand All @@ -49,7 +47,6 @@ var _ = Describe("root command", func() {
" - ads",
"port: 5333",
)
Expect(tmpFile.Error).Should(Succeed())
})

It("should accept old env var", func() {
Expand Down
8 changes: 5 additions & 3 deletions cmd/serve_test.go
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("Serve command", func() {
BeforeEach(func() {
port = helpertest.GetStringPort(basePort)
tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())

configPath = defaultConfigPath
})

Expand All @@ -38,9 +38,10 @@ var _ = Describe("Serve command", func() {
" - 1.1.1.1",
"ports:",
" dns: "+port)
Expect(cfgFile.Error).Should(Succeed())

os.Setenv(configFileEnvVar, cfgFile.Path)
DeferCleanup(func() { os.Unsetenv(configFileEnvVar) })

initConfig()
})

Expand Down Expand Up @@ -84,9 +85,10 @@ var _ = Describe("Serve command", func() {
" - 1.1.1.1",
"ports:",
" dns: "+port)
Expect(cfgFile.Error).Should(Succeed())

os.Setenv(configFileEnvVar, cfgFile.Path)
DeferCleanup(func() { os.Unsetenv(configFileEnvVar) })

initConfig()
})

Expand Down
25 changes: 6 additions & 19 deletions config/config_test.go
Expand Up @@ -30,7 +30,6 @@ var _ = Describe("Config", func() {

BeforeEach(func() {
tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())
})

Describe("Deprecated parameters are converted", func() {
Expand Down Expand Up @@ -147,14 +146,9 @@ var _ = Describe("Config", func() {

Describe("Creation of Config", func() {
When("Test config file will be parsed", func() {
var confFile *helpertest.TmpFile

BeforeEach(func() {
confFile = writeConfigYml(tmpDir)
Expect(confFile.Error).Should(Succeed())
})

It("should return a valid config struct", func() {
confFile := writeConfigYml(tmpDir)

c, err = LoadConfig(confFile.Path, true)
Expect(err).Should(Succeed())

Expand All @@ -169,8 +163,7 @@ var _ = Describe("Config", func() {
})
When("Multiple config files are used", func() {
BeforeEach(func() {
err = writeConfigDir(tmpDir)
Expect(err).Should(Succeed())
writeConfigDir(tmpDir)
})

It("should return a valid config struct", func() {
Expand Down Expand Up @@ -204,7 +197,6 @@ var _ = Describe("Config", func() {
When("config file is malformed", func() {
It("should return error", func() {
cfgFile := tmpDir.CreateStringFile("config.yml", "malformed_config")
Expect(cfgFile.Error).Should(Succeed())

c, err = LoadConfig(cfgFile.Path, true)
Expect(err).Should(HaveOccurred())
Expand Down Expand Up @@ -865,8 +857,8 @@ func writeConfigYml(tmpDir *helpertest.TmpFolder) *helpertest.TmpFile {
)
}

func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
f1 := tmpDir.CreateStringFile("config1.yaml",
func writeConfigDir(tmpDir *helpertest.TmpFolder) {
tmpDir.CreateStringFile("config1.yaml",
"upstreams:",
" startVerify: false",
" userAgent: testBlocky",
Expand All @@ -888,11 +880,8 @@ func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
" - AAAA",
" - A",
)
if f1.Error != nil {
return f1.Error
}

f2 := tmpDir.CreateStringFile("config2.yaml",
tmpDir.CreateStringFile("config2.yaml",
"blocking:",
" blackLists:",
" ads:",
Expand Down Expand Up @@ -930,8 +919,6 @@ func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
"logLevel: debug",
"minTlsServeVersion: 1.3",
)

return f2.Error
}

// Tiny helper to get a new pointer with a value.
Expand Down
6 changes: 0 additions & 6 deletions e2e/containers.go
Expand Up @@ -79,9 +79,6 @@ func createHTTPServerContainer(ctx context.Context, alias string, tmpDir *helper
f1 := tmpDir.CreateStringFile(filename,
lines...,
)
if f1.Error != nil {
return nil, f1.Error
}

const modeOwner = 700

Expand Down Expand Up @@ -160,9 +157,6 @@ func createBlockyContainer(ctx context.Context, tmpDir *helpertest.TmpFolder,
f1 := tmpDir.CreateStringFile("config1.yaml",
lines...,
)
if f1.Error != nil {
return nil, f1.Error
}

cfg, err := config.LoadConfig(f1.Path, true)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion e2e/e2e_suite_test.go
Expand Up @@ -53,7 +53,6 @@ var _ = BeforeSuite(func(ctx context.Context) {
})

tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())

SetDefaultEventuallyTimeout(5 * time.Second)
})
99 changes: 31 additions & 68 deletions helpertest/tmpdata.go
Expand Up @@ -6,18 +6,17 @@ import (
"os"
"path/filepath"

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

type TmpFolder struct {
Path string
Error error
prefix string
}

type TmpFile struct {
Path string
Error error
Folder *TmpFolder
}

Expand All @@ -29,14 +28,14 @@ func NewTmpFolder(prefix string) *TmpFolder {
}

path, err := os.MkdirTemp("", ipref)
Expect(err).Should(Succeed())

res := &TmpFolder{
Path: path,
Error: err,
prefix: ipref,
}

ginkgo.DeferCleanup(res.Clean)
DeferCleanup(res.Clean)

return res
}
Expand All @@ -61,110 +60,74 @@ func (tf *TmpFolder) CreateSubFolder(name string) *TmpFolder {
path, err = os.MkdirTemp(tf.Path, tf.prefix)
}

Expect(err).Should(Succeed())

res := &TmpFolder{
Path: path,
Error: err,
prefix: tf.prefix,
}

return res
}

func (tf *TmpFolder) CreateEmptyFile(name string) *TmpFile {
f, err := tf.createFile(name)
if err != nil {
return tf.newErrorTmpFile(err)
}
f, res := tf.createFile(name)
defer f.Close()

return tf.checkState(f, err)
return res
}

func (tf *TmpFolder) CreateStringFile(name string, lines ...string) *TmpFile {
f, err := tf.createFile(name)
if err != nil {
return tf.newErrorTmpFile(err)
}
f, res := tf.createFile(name)
defer f.Close()

first := true

w := bufio.NewWriter(f)

for _, l := range lines {
if first {
first = false
} else {
_, err = w.WriteString("\n")
}

if err != nil {
break
_, err := w.WriteString("\n")
Expect(err).Should(Succeed())
}

_, err = w.WriteString(l)
if err != nil {
break
}
_, err := w.WriteString(l)
Expect(err).Should(Succeed())
}

w.Flush()

return tf.checkState(f, err)
return res
}

func (tf *TmpFolder) JoinPath(name string) string {
return filepath.Join(tf.Path, name)
}

func (tf *TmpFolder) CountFiles() (int, error) {
func (tf *TmpFolder) ReadDir() []fs.DirEntry {
files, err := os.ReadDir(tf.Path)
if err != nil {
return 0, err
}
Expect(err).Should(Succeed())

return len(files), nil
return files
}

func (tf *TmpFolder) createFile(name string) (*os.File, error) {
if len(name) > 0 {
return os.Create(filepath.Join(tf.Path, name))
}

return os.CreateTemp(tf.Path, "temp")
}
func (tf *TmpFolder) createFile(name string) (*os.File, *TmpFile) {
var (
f *os.File
err error
)

func (tf *TmpFolder) newErrorTmpFile(err error) *TmpFile {
return &TmpFile{
Path: "",
Error: err,
Folder: tf,
if len(name) > 0 {
f, err = os.Create(filepath.Join(tf.Path, name))
} else {
f, err = os.CreateTemp(tf.Path, "temp")
}
}

func (tf *TmpFolder) checkState(file *os.File, ierr error) *TmpFile {
err := ierr
filepath := ""

if file != nil {
filepath = file.Name()

file.Close()

_, err = os.Stat(filepath)
}
Expect(err).Should(Succeed())

return &TmpFile{
Path: filepath,
Error: err,
return f, &TmpFile{
Path: f.Name(),
Folder: tf,
}
}

func (tf *TmpFile) Stat() error {
if tf.Error != nil {
return tf.Error
}

_, res := os.Stat(tf.Path)

return res
}
7 changes: 0 additions & 7 deletions lists/list_cache_test.go
Expand Up @@ -61,20 +61,13 @@ var _ = Describe("ListCache", func() {
server3 = TestServer("blocked3.com\nblocked1a.com")

tmpDir = NewTmpFolder("ListCache")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)

emptyFile = tmpDir.CreateStringFile("empty", "#empty file")
Expect(emptyFile.Error).Should(Succeed())

emptyFile = tmpDir.CreateStringFile("empty", "#empty file")
Expect(emptyFile.Error).Should(Succeed())
file1 = tmpDir.CreateStringFile("file1", "blocked1.com", "blocked1a.com")
Expect(file1.Error).Should(Succeed())
file2 = tmpDir.CreateStringFile("file2", "blocked2.com")
Expect(file2.Error).Should(Succeed())
file3 = tmpDir.CreateStringFile("file3", "blocked3.com", "blocked1a.com")
Expect(file3.Error).Should(Succeed())
})

JustBeforeEach(func() {
Expand Down
17 changes: 3 additions & 14 deletions querylog/file_writer_test.go
Expand Up @@ -25,7 +25,6 @@ var _ = Describe("FileWriter", func() {

JustBeforeEach(func() {
tmpDir = helpertest.NewTmpFolder("fileWriter")
Expect(tmpDir.Error).Should(Succeed())
})

Describe("CSV writer", func() {
Expand Down Expand Up @@ -116,21 +115,11 @@ var _ = Describe("FileWriter", func() {
})
})

Eventually(func(g Gomega) int {
filesCount, err := tmpDir.CountFiles()
g.Expect(err).Should(Succeed())

return filesCount
}, "20s", "1s").Should(Equal(2))
Expect(tmpDir.ReadDir()).Should(HaveLen(2))

go writer.CleanUp()

Eventually(func(g Gomega) int {
filesCount, err := tmpDir.CountFiles()
g.Expect(err).Should(Succeed())
writer.CleanUp()

return filesCount
}, "20s", "1s").Should(Equal(1))
Expect(tmpDir.ReadDir()).Should(HaveLen(1))
})
})
})
Expand Down

0 comments on commit 8c7b89c

Please sign in to comment.