1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +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.
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.

View File

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

View File

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

View File

@ -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()
}

View File

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

View File

@ -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) {

View File

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

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}
// 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)
}