Skip to content

Commit

Permalink
Using require instead of assert
Browse files Browse the repository at this point in the history
and `*testing.T.Cleanup` instead of `defer` plus constifying `1024`
  • Loading branch information
meatballhat authored and tempusfrangit committed Dec 3, 2024
1 parent de6d436 commit f56ac41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
4 changes: 4 additions & 0 deletions pkg/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"math/rand"
)

const (
kB int64 = 1024
)

// generateTestContent generates a byte slice of a random size > 1KiB
func generateTestContent(size int64) []byte {
content := make([]byte, size)
Expand Down
11 changes: 5 additions & 6 deletions pkg/consumer/null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/replicate/pget/pkg/consumer"
)

func TestNullWriter_Consume(t *testing.T) {
buf := generateTestContent(1024)
r := require.New(t)
buf := generateTestContent(kB)
reader := bytes.NewReader(buf)

nullConsumer := consumer.NullWriter{}
err := nullConsumer.Consume(reader, "", 1024)
assert.NoError(t, err)
r.NoError(nullConsumer.Consume(reader, "", kB))

_, _ = reader.Seek(0, 0)
err = nullConsumer.Consume(reader, "", 100)
assert.Error(t, err)
r.Error(nullConsumer.Consume(reader, "", kB-100))
}
41 changes: 20 additions & 21 deletions pkg/consumer/tar_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/replicate/pget/pkg/consumer"
)
Expand Down Expand Up @@ -104,62 +104,61 @@ func createTarFileBytesBuffer() ([]byte, error) {
}

func TestTarExtractor_Consume(t *testing.T) {
r := require.New(t)

tarFileBytes, err := createTarFileBytesBuffer()
if err != nil {
t.Fatal(err)
}
r.NoError(err)

// Create a reader from the tar file bytes
reader := bytes.NewReader(tarFileBytes)

// Create a temporary directory to extract the tar file
tmpDir, err := os.MkdirTemp("", "tarExtractorTest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
r.NoError(err)

t.Cleanup(func() { os.RemoveAll(tmpDir) })

tarConsumer := consumer.TarExtractor{}
targetDir := path.Join(tmpDir, "extract")
err = tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)))
assert.NoError(t, err)
r.NoError(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes))))

// Check if the extraction was successful
checkTarExtraction(t, targetDir)

// Test with incorrect expectedBytes
_, _ = reader.Seek(0, 0)
targetDir = path.Join(tmpDir, "extract-fail")
err = tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)-1))
assert.Error(t, err)
r.Error(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)-1)))
}

func checkTarExtraction(t *testing.T, targetDir string) {
r := require.New(t)

// Verify that file1.txt is correctly extracted
fqFile1Path := path.Join(targetDir, file1Path)
content, err := os.ReadFile(fqFile1Path)
assert.NoError(t, err)
assert.Equal(t, file1Content, string(content))
r.NoError(err)
r.Equal(file1Content, string(content))

// Verify that file2.txt is correctly extracted
fqFile2Path := path.Join(targetDir, file2Path)
content, err = os.ReadFile(fqFile2Path)
assert.NoError(t, err)
assert.Equal(t, file2Content, string(content))
r.NoError(err)
r.Equal(file2Content, string(content))

// Verify that link_to_file1.txt is a symlink pointing to file1.txt
linkToFile1Path := path.Join(targetDir, fileSymLinkPath)
linkTarget, err := os.Readlink(linkToFile1Path)
assert.NoError(t, err)
assert.Equal(t, file1Path, linkTarget)
assert.Equal(t, os.ModeSymlink, os.ModeSymlink&os.ModeType)
r.NoError(err)
r.Equal(file1Path, linkTarget)
r.Equal(os.ModeSymlink, os.ModeSymlink&os.ModeType)

// Verify that subdir/hard_link_to_file2.txt is a hard link to file2.txt
hardLinkToFile2Path := path.Join(targetDir, fileHardLinkPath)
hardLinkStat, err := os.Stat(hardLinkToFile2Path)
assert.NoError(t, err)
r.NoError(err)
file2Stat, err := os.Stat(fqFile2Path)
assert.NoError(t, err)
r.NoError(err)

if !os.SameFile(hardLinkStat, file2Stat) {
t.Errorf("hard link does not match file2.txt")
Expand Down
26 changes: 13 additions & 13 deletions pkg/consumer/write_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/replicate/pget/pkg/consumer"
)

func TestFileWriter_Consume(t *testing.T) {
r := require.New(t)

buf := generateTestContent(1024)
buf := generateTestContent(kB)
reader := bytes.NewReader(buf)

writeFileConsumer := consumer.FileWriter{}
tmpFile, _ := os.CreateTemp("", "fileWriterTest-")

defer tmpFile.Close()
defer os.Remove(tmpFile.Name())
t.Cleanup(func() {
tmpFile.Close()
os.Remove(tmpFile.Name())
})

err := writeFileConsumer.Consume(reader, tmpFile.Name(), 1024)
assert.NoError(t, err)
r.NoError(writeFileConsumer.Consume(reader, tmpFile.Name(), kB))

// Check the file content is correct
fileContent, _ := os.ReadFile(tmpFile.Name())
assert.Equal(t, buf, fileContent)
r.Equal(buf, fileContent)

_, _ = reader.Seek(0, 0)
err = writeFileConsumer.Consume(reader, "", 100)
assert.Error(t, err)
r.Error(writeFileConsumer.Consume(reader, "", kB-100))

// test overwrite
// overwrite the file
f, err := os.OpenFile(tmpFile.Name(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
assert.NoError(t, err)
r.NoError(err)
_, _ = f.Write([]byte("different content"))
f.Close()

// consume the reader
_, _ = reader.Seek(0, 0)
writeFileConsumer.Overwrite = true
err = writeFileConsumer.Consume(reader, tmpFile.Name(), 1024)
assert.NoError(t, err)
r.NoError(writeFileConsumer.Consume(reader, tmpFile.Name(), kB))

// check the file content is correct
fileContent, _ = os.ReadFile(tmpFile.Name())
assert.Equal(t, buf, fileContent)
r.Equal(buf, fileContent)
}

0 comments on commit f56ac41

Please sign in to comment.