diff --git a/Taskfile.yml b/Taskfile.yml index e446aa28..f6727fdb 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -35,6 +35,7 @@ test: deps: [install] cmds: - go test ./args + - go test ./status - go test # https://github.com/goreleaser/goreleaser diff --git a/status/checksum.go b/status/checksum.go index 477f75e5..df344edd 100644 --- a/status/checksum.go +++ b/status/checksum.go @@ -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)) -} diff --git a/status/checksum_test.go b/status/checksum_test.go new file mode 100644 index 00000000..2181ec96 --- /dev/null +++ b/status/checksum_test.go @@ -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)) + } +}