feat: writer test full coverage
This commit is contained in:
parent
25526ac993
commit
7660cc57fd
|
@ -2,6 +2,7 @@ package io
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gitlab.com/urkob/go-cert-gen/pkg/io"
|
||||
|
@ -17,18 +18,19 @@ func (w writer) WriteFile(filename string, data []byte) (string, error) {
|
|||
if filename == "" {
|
||||
return "", fmt.Errorf("filename cannot be empty")
|
||||
}
|
||||
|
||||
if w.dirPath == "" {
|
||||
return "", fmt.Errorf("export directory cannot be empty")
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(w.dirPath, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Println("to write file")
|
||||
outputPath := w.dirPath + "/" + filename
|
||||
if err := os.WriteFile(outputPath, data, 0o600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Println("file written")
|
||||
return outputPath, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -13,7 +13,7 @@ import (
|
|||
var (
|
||||
testWriterPath = "testPath"
|
||||
testWriterPathError = "test Path, @:ººººº\\/.Ç*⁺´+"
|
||||
testFileContent = "test data"
|
||||
testFileContent = []byte("test data")
|
||||
testFileName = "test-file.txt"
|
||||
workingDir = ""
|
||||
)
|
||||
|
@ -53,13 +53,13 @@ func Test_writer_WriteFile(t *testing.T) {
|
|||
}(t)
|
||||
|
||||
w := newWriter(testWriterPath)
|
||||
wgot, err := w.WriteFile(testFileName, []byte(testFileContent))
|
||||
wgot, err := w.WriteFile(testFileName, testFileContent)
|
||||
require.NoError(t, err)
|
||||
|
||||
btsReaded, err := os.ReadFile(wgot)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, btsReaded)
|
||||
require.Equal(t, string(btsReaded), testFileContent)
|
||||
require.Equal(t, string(btsReaded), string(testFileContent))
|
||||
}
|
||||
|
||||
func Test_writer_WriteFileError(t *testing.T) {
|
||||
|
@ -72,39 +72,23 @@ func Test_writer_WriteFileError(t *testing.T) {
|
|||
}(t)
|
||||
|
||||
w := newWriter("")
|
||||
_, err = w.WriteFile(testFileName, []byte(testFileContent))
|
||||
_, err = w.WriteFile(testFileName, testFileContent)
|
||||
assert.Error(t, err)
|
||||
|
||||
w = newWriter(testWriterPath)
|
||||
_, err = w.WriteFile("", []byte(testFileContent))
|
||||
_, err = w.WriteFile("", testFileContent)
|
||||
require.Error(t, err)
|
||||
|
||||
go func(t *testing.T) {
|
||||
err := deleteAllDirs()
|
||||
go func(err error) {
|
||||
require.NoError(t, err)
|
||||
}(err)
|
||||
err = os.MkdirAll(testWriterPath, fs.ModeDir)
|
||||
require.NoError(t, err, "mkdir should not throw error")
|
||||
|
||||
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
|
||||
}
|
||||
}(t)
|
||||
err = os.MkdirAll(testWriterPath+"/"+testFileName, fs.ModeDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = w.WriteFile(testFileName, nil)
|
||||
assert.Error(t, err)
|
||||
|
||||
w = newWriter(testWriterPathError)
|
||||
_, err = w.WriteFile(testFileContent, []byte(testFileContent))
|
||||
_, err = w.WriteFile(testFileName, testFileContent)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue