2023-03-05 00:05:00 +01:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
2023-03-05 15:00:53 +01:00
|
|
|
"io/fs"
|
2023-03-05 00:05:00 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testWriterPath = "testPath"
|
|
|
|
testWriterPathError = "test Path, @:ººººº\\/.Ç*⁺´+"
|
2023-03-05 15:00:53 +01:00
|
|
|
testFileContent = []byte("test data")
|
2023-03-05 00:05:00 +01:00
|
|
|
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) {
|
2023-03-05 00:07:48 +01:00
|
|
|
err := deleteAllDirs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer func(t *testing.T) {
|
|
|
|
err := deleteAllDirs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}(t)
|
|
|
|
|
2023-03-05 00:05:00 +01:00
|
|
|
w := newWriter(testWriterPath)
|
2023-03-05 15:00:53 +01:00
|
|
|
wgot, err := w.WriteFile(testFileName, testFileContent)
|
2023-03-05 00:05:00 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
btsReaded, err := os.ReadFile(wgot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, btsReaded)
|
2023-03-05 15:00:53 +01:00
|
|
|
require.Equal(t, string(btsReaded), string(testFileContent))
|
2023-03-05 00:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_writer_WriteFileError(t *testing.T) {
|
2023-03-05 00:07:48 +01:00
|
|
|
err := deleteAllDirs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer func(t *testing.T) {
|
|
|
|
err := deleteAllDirs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}(t)
|
|
|
|
|
2023-03-05 00:05:00 +01:00
|
|
|
w := newWriter("")
|
2023-03-05 15:00:53 +01:00
|
|
|
_, err = w.WriteFile(testFileName, testFileContent)
|
2023-03-05 00:05:00 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
w = newWriter(testWriterPath)
|
2023-03-05 15:00:53 +01:00
|
|
|
_, err = w.WriteFile("", testFileContent)
|
2023-03-05 00:05:00 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2023-03-05 15:00:53 +01:00
|
|
|
err = os.MkdirAll(testWriterPath, fs.ModeDir)
|
|
|
|
require.NoError(t, err, "mkdir should not throw error")
|
|
|
|
|
|
|
|
err = os.MkdirAll(testWriterPath+"/"+testFileName, fs.ModeDir)
|
|
|
|
require.NoError(t, err)
|
2023-03-05 00:05:00 +01:00
|
|
|
|
|
|
|
_, err = w.WriteFile(testFileName, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
w = newWriter(testWriterPathError)
|
2023-03-05 15:00:53 +01:00
|
|
|
_, err = w.WriteFile(testFileName, testFileContent)
|
2023-03-05 00:05:00 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|