From 1a28e5e0d4e0752a75afb82664c9af31d0a779a0 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 14 Sep 2019 17:54:41 -0300 Subject: [PATCH] Few code improvements on #216 --- docs/usage.md | 8 ++-- internal/status/checksum.go | 2 +- internal/taskfile/var.go | 4 +- internal/templater/templater.go | 4 +- task.go | 4 +- task_test.go | 64 +++++++++++++++++++++---------- testdata/checksum/Taskfile.yml | 14 ------- testdata/status_vars/.gitignore | 1 + testdata/status_vars/Taskfile.yml | 17 ++++++++ testdata/status_vars/source.txt | 1 + variables.go | 2 +- 11 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 testdata/status_vars/.gitignore create mode 100644 testdata/status_vars/Taskfile.yml create mode 100644 testdata/status_vars/source.txt diff --git a/docs/usage.md b/docs/usage.md index 02a8db7e..a9c12a7a 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -325,7 +325,6 @@ tasks: ### Using programmatic checks to indicate a task is up to date. - Alternatively, you can inform a sequence of tests as `status`. If no error is returned (exit status 0), the task is considered up-to-date: @@ -345,7 +344,6 @@ tasks: - test -f directory/file2.txt ``` - Normally, you would use `sources` in combination with `generates` - but for tasks that generate remote artifacts (Docker images, deploys, CD releases) the checksum source and timestamps require either @@ -354,10 +352,10 @@ fingerprint file. Two special variables `{{.CHECKSUM}}` and `{{.TIMESTAMP}}` are available for interpolation within `status` commands, depending on the method assigned -to fingerprint the sources. Only `source` globs are fingerprinted. +to fingerprint the sources. Only `source` globs are fingerprinted. -Note that the `{{.TIMESTAMP}}` variable is a "live" Go time struct, and can be -formatted using any of the methods that `Time` responds to. +Note that the `{{.TIMESTAMP}}` variable is a "live" Go `time.Time` struct, and +can be formatted using any of the methods that `time.Time` responds to. See [the Go Time documentation](https://golang.org/pkg/time/) for more information. diff --git a/internal/status/checksum.go b/internal/status/checksum.go index c7137ea2..68dcd699 100644 --- a/internal/status/checksum.go +++ b/internal/status/checksum.go @@ -99,7 +99,7 @@ func (c *Checksum) OnError() error { } // Kind implements the Checker Interface -func (t *Checksum) Kind() string { +func (*Checksum) Kind() string { return "checksum" } diff --git a/internal/taskfile/var.go b/internal/taskfile/var.go index ad0f5e90..225aed1e 100644 --- a/internal/taskfile/var.go +++ b/internal/taskfile/var.go @@ -15,8 +15,8 @@ type Vars map[string]Var // ToCacheMap converts Vars to a map containing only the static // variables -func (vs Vars) ToCacheMap() (m map[string](interface{})) { - m = make(map[string](interface{}), len(vs)) +func (vs Vars) ToCacheMap() (m map[string]interface{}) { + m = make(map[string]interface{}, len(vs)) for k, v := range vs { if v.Sh != "" { // Dynamic variable is not yet resolved; trigger diff --git a/internal/templater/templater.go b/internal/templater/templater.go index d43117e0..b395070a 100644 --- a/internal/templater/templater.go +++ b/internal/templater/templater.go @@ -14,11 +14,11 @@ import ( type Templater struct { Vars taskfile.Vars - cacheMap map[string](interface{}) + cacheMap map[string]interface{} err error } -func (r *Templater) RefreshCacheMap() { +func (r *Templater) ResetCache() { r.cacheMap = r.Vars.ToCacheMap() } diff --git a/task.go b/task.go index 08b72f95..c6513498 100644 --- a/task.go +++ b/task.go @@ -357,7 +357,9 @@ func getEnviron(t *taskfile.Task) []string { environ := os.Environ() for k, v := range t.Env.ToCacheMap() { - environ = append(environ, fmt.Sprintf("%s=%s", k, v)) + if s, ok := v.(string); ok { + environ = append(environ, fmt.Sprintf("%s=%s", k, s)) + } } return environ } diff --git a/task_test.go b/task_test.go index 41eb5a8c..b329763d 100644 --- a/task_test.go +++ b/task_test.go @@ -12,7 +12,6 @@ import ( "testing" "github.com/go-task/task/v2" - "github.com/go-task/task/v2/internal/logger" "github.com/go-task/task/v2/internal/taskfile" "github.com/stretchr/testify/assert" @@ -352,20 +351,12 @@ func TestStatusChecksum(t *testing.T) { } var buff bytes.Buffer - - logCapturer := logger.Logger{ - Stdout: &buff, - Stderr: &buff, - Verbose: true, - } - e := task.Executor{ Dir: dir, Stdout: &buff, Stderr: &buff, } assert.NoError(t, e.Setup()) - e.Logger = &logCapturer assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"})) for _, f := range files { @@ -376,20 +367,51 @@ func TestStatusChecksum(t *testing.T) { buff.Reset() assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"})) assert.Equal(t, `task: Task "build" is up to date`+"\n", buff.String()) +} - buff.Reset() - e.Silent = false - e.Verbose = true - assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-checksum"})) - assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e") +func TestStatusVariables(t *testing.T) { + t.Run("Checksum", func(t *testing.T) { + const dir = "testdata/status_vars" - buff.Reset() - inf, _ := os.Stat(filepath.Join(dir, "source.txt")) - ts := fmt.Sprintf("%d", inf.ModTime().Unix()) - tf := fmt.Sprintf("%s", inf.ModTime()) - assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-timestamp"})) - assert.Contains(t, buff.String(), ts) - assert.Contains(t, buff.String(), tf) + _ = os.RemoveAll(filepath.Join(dir, ".task")) + _ = os.Remove(filepath.Join(dir, "generated.txt")) + + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: false, + Verbose: true, + } + assert.NoError(t, e.Setup()) + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-checksum"})) + assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e") + }) + + t.Run("Timestamp", func(t *testing.T) { + const dir = "testdata/status_vars" + + _ = os.Remove(filepath.Join(dir, "generated.txt")) + + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: false, + Verbose: true, + } + assert.NoError(t, e.Setup()) + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-timestamp"})) + + inf, err := os.Stat(filepath.Join(dir, "source.txt")) + assert.NoError(t, err) + ts := fmt.Sprintf("%d", inf.ModTime().Unix()) + tf := fmt.Sprintf("%s", inf.ModTime()) + assert.Contains(t, buff.String(), ts) + assert.Contains(t, buff.String(), tf) + }) } func TestInit(t *testing.T) { diff --git a/testdata/checksum/Taskfile.yml b/testdata/checksum/Taskfile.yml index 2becc5f0..1c620a71 100644 --- a/testdata/checksum/Taskfile.yml +++ b/testdata/checksum/Taskfile.yml @@ -10,17 +10,3 @@ tasks: generates: - ./generated.txt method: checksum - - build-with-checksum: - sources: - - ./source.txt - method: checksum - status: - - echo "{{.CHECKSUM}}" - - build-with-timestamp: - sources: - - ./source.txt - status: - - echo '{{.TIMESTAMP.Unix }}' - - echo '{{.TIMESTAMP}}' diff --git a/testdata/status_vars/.gitignore b/testdata/status_vars/.gitignore new file mode 100644 index 00000000..56796b08 --- /dev/null +++ b/testdata/status_vars/.gitignore @@ -0,0 +1 @@ +generated.txt diff --git a/testdata/status_vars/Taskfile.yml b/testdata/status_vars/Taskfile.yml new file mode 100644 index 00000000..2e758b09 --- /dev/null +++ b/testdata/status_vars/Taskfile.yml @@ -0,0 +1,17 @@ +version: '3' + +tasks: + build-with-checksum: + sources: + - ./source.txt + method: checksum + status: + - echo "{{.CHECKSUM}}" + + build-with-timestamp: + sources: + - ./source.txt + method: timestamp + status: + - echo '{{.TIMESTAMP.Unix}}' + - echo '{{.TIMESTAMP}}' diff --git a/testdata/status_vars/source.txt b/testdata/status_vars/source.txt new file mode 100644 index 00000000..8ab686ea --- /dev/null +++ b/testdata/status_vars/source.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/variables.go b/variables.go index 8307fc4d..a55213a5 100644 --- a/variables.go +++ b/variables.go @@ -109,7 +109,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) { vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value} // Adding new variables, requires us to refresh the templaters // cache of the the values manually - r.RefreshCacheMap() + r.ResetCache() new.Status = r.ReplaceSlice(origTask.Status) }