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

Few code improvements on #216

This commit is contained in:
Andrey Nering 2019-09-14 17:54:41 -03:00
parent 884cd0d636
commit 1a28e5e0d4
11 changed files with 74 additions and 47 deletions

View File

@ -325,7 +325,6 @@ tasks:
### Using programmatic checks to indicate a task is up to date. ### Using programmatic checks to indicate a task is up to date.
Alternatively, you can inform a sequence of tests as `status`. If no error 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: is returned (exit status 0), the task is considered up-to-date:
@ -345,7 +344,6 @@ tasks:
- test -f directory/file2.txt - test -f directory/file2.txt
``` ```
Normally, you would use `sources` in combination with Normally, you would use `sources` in combination with
`generates` - but for tasks that generate remote artifacts (Docker images, `generates` - but for tasks that generate remote artifacts (Docker images,
deploys, CD releases) the checksum source and timestamps require either deploys, CD releases) the checksum source and timestamps require either
@ -354,10 +352,10 @@ fingerprint file.
Two special variables `{{.CHECKSUM}}` and `{{.TIMESTAMP}}` are available Two special variables `{{.CHECKSUM}}` and `{{.TIMESTAMP}}` are available
for interpolation within `status` commands, depending on the method assigned 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 Note that the `{{.TIMESTAMP}}` variable is a "live" Go `time.Time` struct, and
formatted using any of the methods that `Time` responds to. 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. See [the Go Time documentation](https://golang.org/pkg/time/) for more information.

View File

@ -99,7 +99,7 @@ func (c *Checksum) OnError() error {
} }
// Kind implements the Checker Interface // Kind implements the Checker Interface
func (t *Checksum) Kind() string { func (*Checksum) Kind() string {
return "checksum" return "checksum"
} }

View File

@ -15,8 +15,8 @@ type Vars map[string]Var
// ToCacheMap converts Vars to a map containing only the static // ToCacheMap converts Vars to a map containing only the static
// variables // variables
func (vs Vars) ToCacheMap() (m map[string](interface{})) { func (vs Vars) ToCacheMap() (m map[string]interface{}) {
m = make(map[string](interface{}), len(vs)) m = make(map[string]interface{}, len(vs))
for k, v := range vs { for k, v := range vs {
if v.Sh != "" { if v.Sh != "" {
// Dynamic variable is not yet resolved; trigger // Dynamic variable is not yet resolved; trigger

View File

@ -14,11 +14,11 @@ import (
type Templater struct { type Templater struct {
Vars taskfile.Vars Vars taskfile.Vars
cacheMap map[string](interface{}) cacheMap map[string]interface{}
err error err error
} }
func (r *Templater) RefreshCacheMap() { func (r *Templater) ResetCache() {
r.cacheMap = r.Vars.ToCacheMap() r.cacheMap = r.Vars.ToCacheMap()
} }

View File

@ -357,7 +357,9 @@ func getEnviron(t *taskfile.Task) []string {
environ := os.Environ() environ := os.Environ()
for k, v := range t.Env.ToCacheMap() { 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 return environ
} }

View File

@ -12,7 +12,6 @@ import (
"testing" "testing"
"github.com/go-task/task/v2" "github.com/go-task/task/v2"
"github.com/go-task/task/v2/internal/logger"
"github.com/go-task/task/v2/internal/taskfile" "github.com/go-task/task/v2/internal/taskfile"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -352,20 +351,12 @@ func TestStatusChecksum(t *testing.T) {
} }
var buff bytes.Buffer var buff bytes.Buffer
logCapturer := logger.Logger{
Stdout: &buff,
Stderr: &buff,
Verbose: true,
}
e := task.Executor{ e := task.Executor{
Dir: dir, Dir: dir,
Stdout: &buff, Stdout: &buff,
Stderr: &buff, Stderr: &buff,
} }
assert.NoError(t, e.Setup()) assert.NoError(t, e.Setup())
e.Logger = &logCapturer
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"})) assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
for _, f := range files { for _, f := range files {
@ -376,20 +367,51 @@ func TestStatusChecksum(t *testing.T) {
buff.Reset() buff.Reset()
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"})) assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
assert.Equal(t, `task: Task "build" is up to date`+"\n", buff.String()) assert.Equal(t, `task: Task "build" is up to date`+"\n", buff.String())
}
buff.Reset() func TestStatusVariables(t *testing.T) {
e.Silent = false t.Run("Checksum", func(t *testing.T) {
e.Verbose = true const dir = "testdata/status_vars"
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-checksum"}))
assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e")
buff.Reset() _ = os.RemoveAll(filepath.Join(dir, ".task"))
inf, _ := os.Stat(filepath.Join(dir, "source.txt")) _ = os.Remove(filepath.Join(dir, "generated.txt"))
ts := fmt.Sprintf("%d", inf.ModTime().Unix())
tf := fmt.Sprintf("%s", inf.ModTime()) var buff bytes.Buffer
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-timestamp"})) e := task.Executor{
assert.Contains(t, buff.String(), ts) Dir: dir,
assert.Contains(t, buff.String(), tf) 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) { func TestInit(t *testing.T) {

View File

@ -10,17 +10,3 @@ tasks:
generates: generates:
- ./generated.txt - ./generated.txt
method: checksum 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}}'

1
testdata/status_vars/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
generated.txt

17
testdata/status_vars/Taskfile.yml vendored Normal file
View File

@ -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}}'

1
testdata/status_vars/source.txt vendored Normal file
View File

@ -0,0 +1 @@
Hello, World!

View File

@ -109,7 +109,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value} vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value}
// Adding new variables, requires us to refresh the templaters // Adding new variables, requires us to refresh the templaters
// cache of the the values manually // cache of the the values manually
r.RefreshCacheMap() r.ResetCache()
new.Status = r.ReplaceSlice(origTask.Status) new.Status = r.ReplaceSlice(origTask.Status)
} }