mirror of
https://github.com/go-task/task.git
synced 2026-09-01 19:50:16 +02:00
fix: report no timestamp when a task's sources match no file
TimestampChecker.Value returned a sentinel for "no source found": first
the string "0", then time.Unix(0, 0) once the value became a time.Time
exposed to templates. Rendered, that sentinel reads as a genuine date,
1970-01-01 01:00:00 +0100 CET, whose zone depends on the machine — so a
command interpolating {{.TIMESTAMP}} gets a plausible but meaningless
multi-word value instead of nothing.
Return an empty string, like NoneChecker already does for a checker with
nothing to report.
This commit is contained in:
@@ -122,8 +122,12 @@ func (checker *TimestampChecker) Value(t *ast.Task) (any, error) {
|
||||
return time.Now(), err
|
||||
}
|
||||
|
||||
// No source matched, so there is no modification time to report. Returning
|
||||
// the Unix epoch here would render as a real date in templates, e.g.
|
||||
// "1970-01-01 01:00:00 +0100 CET", which reads as a genuine timestamp and
|
||||
// carries a machine-dependent zone.
|
||||
if sourcesMaxTime.IsZero() {
|
||||
return time.Unix(0, 0), nil
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return sourcesMaxTime, nil
|
||||
|
||||
44
internal/fingerprint/sources_timestamp_test.go
Normal file
44
internal/fingerprint/sources_timestamp_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package fingerprint
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/go-task/task/v3/taskfile/ast"
|
||||
)
|
||||
|
||||
func TestTimestampCheckerValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "source.txt"), []byte("content"), 0o644))
|
||||
checker := NewTimestampChecker(t.TempDir(), true)
|
||||
|
||||
t.Run("reports the newest source modification time", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
value, err := checker.Value(&ast.Task{
|
||||
Dir: dir,
|
||||
Sources: []*ast.Glob{{Glob: "source.txt"}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.IsType(t, time.Time{}, value)
|
||||
assert.False(t, value.(time.Time).IsZero())
|
||||
})
|
||||
|
||||
t.Run("reports no value when no source matches", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
value, err := checker.Value(&ast.Task{
|
||||
Dir: dir,
|
||||
Sources: []*ast.Glob{{Glob: "gen/**/*.go"}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", value)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user