Add basename
This commit is contained in:
1 parent
289851b20f
commit
dbce61c760
5 files changed
+73
No files matched your search
@@ -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))
|
||||
}
|
||||
Reference in new issue
Block a user