feat: test coverage more than 85%

This commit is contained in:
Urko 2023-03-05 00:05:00 +01:00
parent 89136cae59
commit 0bf7b63286
3 changed files with 106 additions and 0 deletions

View File

@ -11,7 +11,13 @@ type writer struct {
dirPath string dirPath string
} }
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
func (w writer) WriteFile(filename string, data []byte) (string, error) { func (w writer) WriteFile(filename string, data []byte) (string, error) {
if filename == "" {
return "", fmt.Errorf("filename cannot be empty")
}
if w.dirPath == "" { if w.dirPath == "" {
return "", fmt.Errorf("export directory cannot be empty") return "", fmt.Errorf("export directory cannot be empty")
} }
@ -27,6 +33,10 @@ func (w writer) WriteFile(filename string, data []byte) (string, error) {
} }
func NewWriter(dirPath string) io.WriterIface { func NewWriter(dirPath string) io.WriterIface {
return newWriter(dirPath)
}
func newWriter(dirPath string) *writer {
return &writer{ return &writer{
dirPath: dirPath, dirPath: dirPath,
} }

View File

@ -0,0 +1,94 @@
package io
import (
"log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testWriterPath = "testPath"
testWriterPathError = "test Path, @:ººººº\\/.Ç*⁺´+"
testFileContent = "test data"
testFileName = "test-file.txt"
workingDir = ""
)
func deleteAllDirs() error {
return os.RemoveAll(testWriterPath)
}
func init() {
err := deleteAllDirs()
if err != nil {
log.Fatalln("deleteAllDirs: ", err)
}
workingDir, err = os.Getwd()
if err != nil {
log.Fatalln("os.Getwd: ", err)
}
}
func Test_newWriter(t *testing.T) {
w := newWriter(testWriterPath)
require.NotNil(t, w)
}
func TestNewWriter(t *testing.T) {
w := NewWriter(testWriterPath)
require.NotNil(t, w)
}
func Test_writer_WriteFile(t *testing.T) {
deleteAllDirs()
defer deleteAllDirs()
w := newWriter(testWriterPath)
wgot, err := w.WriteFile(testFileName, []byte(testFileContent))
require.NoError(t, err)
btsReaded, err := os.ReadFile(wgot)
require.NoError(t, err)
require.NotEmpty(t, btsReaded)
require.Equal(t, string(btsReaded), testFileContent)
}
func Test_writer_WriteFileError(t *testing.T) {
deleteAllDirs()
defer deleteAllDirs()
w := newWriter("")
_, err := w.WriteFile(testFileName, []byte(testFileContent))
assert.Error(t, err)
w = newWriter(testWriterPath)
_, err = w.WriteFile("", []byte(testFileContent))
require.Error(t, err)
go func() {
deleteAllDirs()
for {
f, err := os.Stat(testWriterPath)
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
continue
}
t.Errorf("os.Stat error: %s", err)
}
err = os.RemoveAll(testWriterPath)
require.NotNil(t, f)
log.Println("exists deleting dir")
require.NoError(t, err, "should not throw error because dir exists")
return
}
}()
_, err = w.WriteFile(testFileName, nil)
assert.Error(t, err)
w = newWriter(testWriterPathError)
_, err = w.WriteFile(testFileContent, []byte(testFileContent))
require.Error(t, err)
}

View File

@ -1,5 +1,7 @@
package io package io
type WriterIface interface { type WriterIface interface {
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
WriteFile(filename string, data []byte) (string, error) WriteFile(filename string, data []byte) (string, error)
} }