From 9dd7ee329f9a4e01f1165bc63401f21dee471394 Mon Sep 17 00:00:00 2001 From: kerelape Date: Sat, 7 Oct 2023 01:16:19 +0500 Subject: [PATCH] Add `Path` --- go.mod | 8 ++++++++ go.sum | 9 +++++++++ path.go | 23 +++++++++++++++++++++++ path_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 go.sum create mode 100644 path.go create mode 100644 path_test.go diff --git a/go.mod b/go.mod index aeda6b8..0ce5e6b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/kerelape/filesystem go 1.20 + +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8cf6655 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/path.go b/path.go new file mode 100644 index 0000000..c5b6043 --- /dev/null +++ b/path.go @@ -0,0 +1,23 @@ +package filesystem + +import "path" + +// Path is a path to a file-system object. +type Path string + +// IsAbsolute returns true if the path is absolute. +func (p Path) IsAbsolute() bool { + return path.IsAbs((string)(p)) +} + +// IsRelative is oppasite to IsAbsolute, it returns +// true if the path is relative, and false if it's +// absolute. +func (p Path) IsRelative() bool { + return !p.IsAbsolute() +} + +// String returns this path as a string. +func (p Path) String() string { + return (string)(p) +} diff --git a/path_test.go b/path_test.go new file mode 100644 index 0000000..863674a --- /dev/null +++ b/path_test.go @@ -0,0 +1,39 @@ +package filesystem + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPath_IsAbsolute(t *testing.T) { + absolutes := []Path{ + "/", + "/smth", + "/smth/", + } + for _, absolute := range absolutes { + t.Run(absolute.String(), func(t *testing.T) { + assert.Truef(t, absolute.IsAbsolute(), `"%s" was expected to be absolute`, absolute) + assert.Falsef(t, absolute.IsRelative(), `"%s" is both, relative and absolute`, absolute) + }) + } +} + +func TestPath_IsRelative(t *testing.T) { + relatives := []Path{ + "smth", + "./smth", + "../smth/", + } + for _, relative := range relatives { + t.Run(relative.String(), func(t *testing.T) { + assert.Truef(t, relative.IsRelative(), `"%s" was expected to be relative`, relative) + assert.Falsef(t, relative.IsAbsolute(), `"%s" is both, relative and absolute`, relative) + }) + } +} + +func TestPath_String(t *testing.T) { + assert.Equal(t, (Path)("/").String(), "/") +}