Add basename

feature/pwd
dtluna 5 years ago
parent 289851b20f
commit dbce61c760
  1. 1
      README.md
  2. 35
      basename/basename.go
  3. 28
      basename/basename_test.go
  4. 2
      go.mod
  5. 7
      go.sum

@ -12,3 +12,4 @@ Implemeted utilities:
* `false`
* `yes`
* `dirname`
* `basename`

@ -0,0 +1,35 @@
package main
import (
"fmt"
"os"
p "path"
"strings"
)
func usage() {
fmt.Printf("usage: %v path [suffix]\n", os.Args[0])
os.Exit(1)
}
func basename(path, suffix string) string {
result := p.Base(path)
if suffix == "" {
return result
}
return strings.SplitN(result, suffix, 2)[0]
}
func main() {
if len(os.Args) == 1 || len(os.Args) > 3 {
usage()
}
path := os.Args[1]
suffix := ""
if len(os.Args) == 3 {
suffix = os.Args[2]
}
fmt.Println(basename(path, suffix))
}

@ -0,0 +1,28 @@
package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"path"
"testing"
)
type BasenameTestSuite struct {
suite.Suite
}
func (suite *BasenameTestSuite) TestEmptySuffix() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ""))
}
func (suite *BasenameTestSuite) TestSuffixNotInPath() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ".git"))
}
func (suite *BasenameTestSuite) TestSuffixInPath() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ".git"))
}
func TestBasenameTestSuite(t *testing.T) {
suite.Run(t, new(BasenameTestSuite))
}

@ -1 +1,3 @@
module source.heropunch.io/tomo/go-coreutils
require github.com/stretchr/testify v1.3.0

@ -0,0 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Loading…
Cancel
Save