1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/piperutils/ioUtils_test.go
Eng Zer Jun 0f4e30e9db
test: use T.TempDir to create temporary test directory (#3721)
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-07-12 15:19:12 +02:00

133 lines
3.5 KiB
Go

package piperutils
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCopyData(t *testing.T) {
runInTempDir(t, "copying file succeeds small", func(t *testing.T) {
srcName := "testFileSrc"
src, err := os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to create src file")
}
data := make([]byte, 3)
data[0] = byte(32)
data[1] = byte(42)
data[2] = byte(52)
_, err = src.Write(data)
if err != nil {
t.Fatalf("Failed to write data to src file: %v", err)
}
src.Close()
src, err = os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to reopen src file")
}
dstName := "testFileTgt"
dst, err := os.OpenFile(dstName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to create dst file")
}
result, err := CopyData(dst, src)
src.Close()
dst.Close()
dst, err = os.OpenFile(dstName, os.O_CREATE|os.O_RDWR, 0700)
dataRead := make([]byte, 3)
dst.Read(dataRead)
dst.Close()
assert.NoError(t, err, "Didn't expect error but got one")
assert.Equal(t, int64(3), result, "Expected true but got false")
assert.Equal(t, data, dataRead, "data written %v is different to data read %v")
})
runInTempDir(t, "copying file succeeds larger", func(t *testing.T) {
srcName := "testFile"
src, err := os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to create src file")
}
data := make([]byte, 300)
for i := 0; i < 300; i++ {
data[i] = byte(i)
}
_, err = src.Write(data)
src.Close()
src, err = os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
dstName := "testFile2"
dst, err := os.OpenFile(dstName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to create dst file")
}
result, err := CopyData(dst, src)
src.Close()
dst.Close()
assert.NoError(t, err, "Didn't expect error but got one")
assert.Equal(t, int64(300), result, "Expected true but got false")
})
runInTempDir(t, "copying file fails on read", func(t *testing.T) {
srcName := "testFileExcl"
src, err := os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatalf("Failed to create src file %v", err)
}
data := make([]byte, 300)
for i := 0; i < 300; i++ {
data[i] = byte(i)
}
_, err = src.Write(data)
src.Close()
src, err = os.OpenFile(srcName, os.O_WRONLY, 0700)
dstName := "testFile2"
dst, err := os.OpenFile(dstName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatal("Failed to create dst file")
}
result, err := CopyData(dst, src)
src.Close()
dst.Close()
assert.Error(t, err, "Expected error but got none")
assert.Equal(t, int64(0), result, "Expected true but got false")
})
runInTempDir(t, "copying file fails on write", func(t *testing.T) {
srcName := "testFileExcl"
src, err := os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatalf("Failed to create src file %v", err)
}
data := make([]byte, 300)
for i := 0; i < 300; i++ {
data[i] = byte(i)
}
_, err = src.Write(data)
src.Close()
src, err = os.OpenFile(srcName, os.O_CREATE|os.O_RDWR, 0700)
dstName := "testFileExclus"
dst, err := os.OpenFile(dstName, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
t.Fatalf("Failed to create dst file: %v", err)
}
dst.Close()
dst, err = os.OpenFile(dstName, os.O_RDONLY, 0700)
result, err := CopyData(dst, src)
src.Close()
dst.Close()
assert.Error(t, err, "Expected error but got none")
assert.Equal(t, int64(0), result, "Expected true but got false")
})
}