1
0
mirror of https://github.com/go-task/task.git synced 2025-01-12 04:34:11 +02:00

checksum: add tests for filename

This commit is contained in:
Andrey Nering 2017-10-01 15:05:09 -03:00
parent 7977e6fb16
commit 309bc4ee4c
3 changed files with 32 additions and 6 deletions

View File

@ -35,6 +35,7 @@ test:
deps: [install]
cmds:
- go test ./args
- go test ./status
- go test
# https://github.com/goreleaser/goreleaser

View File

@ -21,7 +21,7 @@ type Checksum struct {
// IsUpToDate implements the Checker interface
func (c *Checksum) IsUpToDate() (bool, error) {
checksumFile := filepath.Join(c.Dir, ".task", c.normalizeFilename(c.Task))
checksumFile := c.checksumFilePath()
data, _ := ioutil.ReadFile(checksumFile)
oldMd5 := strings.TrimSpace(string(data))
@ -66,14 +66,18 @@ func (c *Checksum) checksum(files ...string) (string, error) {
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// OnError implements the Checker interface
func (c *Checksum) OnError() error {
return os.Remove(c.checksumFilePath())
}
func (c *Checksum) checksumFilePath() string {
return filepath.Join(c.Dir, ".task", c.normalizeFilename(c.Task))
}
var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]")
// replaces invalid caracters on filenames with "-"
func (*Checksum) normalizeFilename(f string) string {
return checksumFilenameRegexp.ReplaceAllString(f, "-")
}
// OnError implements the Checker interface
func (c *Checksum) OnError() error {
return os.Remove(filepath.Join(c.Dir, ".task", c.Task))
}

21
status/checksum_test.go Normal file
View File

@ -0,0 +1,21 @@
package status
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNormalizeFilename(t *testing.T) {
tests := []struct {
In, Out string
}{
{"foobarbaz", "foobarbaz"},
{"foo/bar/baz", "foo-bar-baz"},
{"foo@bar/baz", "foo-bar-baz"},
{"foo1bar2baz3", "foo1bar2baz3"},
}
for _, test := range tests {
assert.Equal(t, test.Out, (&Checksum{}).normalizeFilename(test.In))
}
}