Skip to content

Commit

Permalink
add Public setter, update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: 2rigor <[email protected]>
  • Loading branch information
2rigor committed Oct 30, 2024
1 parent 6ee4035 commit 2b6e3ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
17 changes: 11 additions & 6 deletions pkg/file/tarutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

const perFileReadLimitDefault = 2 * GB

var perFileReadLimit int64 = perFileReadLimitDefault
var perFileReadLimitStr = "0" // allows configuring the above value during build time

Expand Down Expand Up @@ -43,15 +44,19 @@ type ErrFileNotFound struct {
}

func init() {
setPerFileReadLimit(perFileReadLimitStr)
setPerFileReadLimitStr(perFileReadLimitStr)
}

func setPerFileReadLimitStr(val string) {
if parsedInt64, err := strconv.ParseInt(val, 10, 64); err == nil {
SetPerFileReadLimit(parsedInt64)
}
}

func setPerFileReadLimit(val string) {
valInt64, err := strconv.ParseInt(val, 10, 64)
if err != nil || valInt64 <= 0 {
return
func SetPerFileReadLimit(val int64) {
if val > 0 {
perFileReadLimit = val
}
perFileReadLimit = valInt64
}

func (e *ErrFileNotFound) Error() string {
Expand Down
52 changes: 31 additions & 21 deletions pkg/file/tarutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,31 +432,41 @@ func Test_tarVisitor_visit(t *testing.T) {
}

func TestSetPerFileReadLimit_Valid(t *testing.T) {
// Test with a valid limit string
perFileReadLimit = perFileReadLimitDefault
setPerFileReadLimit("12345")
if perFileReadLimit != 12345 {
t.Errorf("Expected perFileReadLimit to be 12345, but got %d", perFileReadLimit)
}
// Test with a valid limit string
SetPerFileReadLimit(perFileReadLimitDefault)
setPerFileReadLimitStr("12345")

if perFileReadLimit != 12345 {
t.Errorf("Expected perFileReadLimit to be 12345, but got %d", perFileReadLimit)
}
}

func TestSetPerFileReadLimit_Invalid(t *testing.T) {
// Test with a invalid limit string
perFileReadLimit = perFileReadLimitDefault
setPerFileReadLimit("invalid")

if perFileReadLimit != perFileReadLimitDefault {
t.Errorf("Expected perFileReadLimit to be %d, but got %d", perFileReadLimitDefault, perFileReadLimit)
}
// Test with an invalid limit string
SetPerFileReadLimit(perFileReadLimitDefault)
setPerFileReadLimitStr("invalid")

if perFileReadLimit != perFileReadLimitDefault {
t.Errorf("Expected perFileReadLimit to be %d, but got %d", perFileReadLimitDefault, perFileReadLimit)
}
}

func TestSetPerFileReadLimit_NegativeValue(t *testing.T) {
// Test with an invalid limit string
SetPerFileReadLimit(perFileReadLimitDefault)
setPerFileReadLimitStr("-12345")

if perFileReadLimit != perFileReadLimitDefault {
t.Errorf("Expected perFileReadLimit to be %d, but got %d", perFileReadLimitDefault, perFileReadLimit)
}
}

func TestSetPerFileReadLimit_Empty(t *testing.T) {
// Test with an empty limit string (default)
perFileReadLimit = perFileReadLimitDefault
setPerFileReadLimit("")
if perFileReadLimit != perFileReadLimitDefault {
t.Errorf("Expected perFileReadLimit to be %d, but got %d", perFileReadLimitDefault, perFileReadLimit)
}
// Test with an empty limit string (default)
SetPerFileReadLimit(perFileReadLimitDefault)
setPerFileReadLimitStr("")

if perFileReadLimit != perFileReadLimitDefault {
t.Errorf("Expected perFileReadLimit to be %d, but got %d", perFileReadLimitDefault, perFileReadLimit)
}
}

0 comments on commit 2b6e3ca

Please sign in to comment.