This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package archiver | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// IllegalPathError is an error returned when an illegal | ||
// path is detected during the archival process. | ||
// | ||
// By default, only the Filename is showed on error, but you might | ||
// also get the absolute value of the invalid path on the AbsolutePath | ||
// field. | ||
type IllegalPathError struct { | ||
AbsolutePath string | ||
Filename string | ||
} | ||
|
||
func (err *IllegalPathError) Error() string { | ||
return fmt.Sprintf("illegal file path: %s", err.Filename) | ||
} | ||
|
||
// IsIllegalPathError returns true if the provided error is of | ||
// the type IllegalPathError. | ||
func IsIllegalPathError(err error) bool { | ||
return err != nil && strings.Contains(err.Error(), "illegal file path: ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package archiver_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/mholt/archiver/v3" | ||
) | ||
|
||
func TestIllegalPathErrorString(t *testing.T) { | ||
tests := []struct { | ||
instance *archiver.IllegalPathError | ||
expected string | ||
}{ | ||
{instance: &archiver.IllegalPathError{Filename: "foo.txt"}, expected: "illegal file path: foo.txt"}, | ||
{instance: &archiver.IllegalPathError{AbsolutePath: "/tmp/bar.txt", Filename: "bar.txt"}, expected: "illegal file path: bar.txt"}, | ||
} | ||
|
||
for i, test := range tests { | ||
test := test | ||
|
||
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) { | ||
if test.expected != test.instance.Error() { | ||
t.Fatalf("Excepected '%s', but got '%s'", test.expected, test.instance.Error()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsIllegalPathError(t *testing.T) { | ||
tests := []struct { | ||
instance error | ||
expected bool | ||
}{ | ||
{instance: nil, expected: false}, | ||
{instance: os.ErrNotExist, expected: false}, | ||
{instance: fmt.Errorf("some error"), expected: false}, | ||
{instance: errors.New("another error"), expected: false}, | ||
{instance: &archiver.IllegalPathError{Filename: "foo.txt"}, expected: true}, | ||
} | ||
|
||
for i, test := range tests { | ||
test := test | ||
|
||
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) { | ||
actual := archiver.IsIllegalPathError(test.instance) | ||
if actual != test.expected { | ||
t.Fatalf("Excepected '%v', but got '%v'", test.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters