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