1
0
mirror of https://github.com/go-task/task.git synced 2025-01-04 03:48:02 +02:00

Allow absolute path in generates section

Fixes issue #47 by allowing absolute paths in a task's generates and
sources sections. Tests are added for the generates section only at this
time.
This commit is contained in:
Sindre Røkenes Myren 2017-07-12 11:30:28 +02:00 committed by Sindre Røkenes Myren
parent a586fef414
commit 86a23849e0
5 changed files with 98 additions and 2 deletions

View File

@ -23,7 +23,9 @@ func maxTime(a, b time.Time) time.Time {
func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error) {
for _, p := range patterns {
p = filepath.Join(dir, p)
if !filepath.IsAbs(p) {
p = filepath.Join(dir, p)
}
mp, err := getPatternMinTime(p)
if err != nil {
return time.Time{}, err
@ -34,7 +36,9 @@ func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error)
}
func getPatternsMaxTime(dir string, patterns []string) (m time.Time, err error) {
for _, p := range patterns {
p = filepath.Join(dir, p)
if !filepath.IsAbs(p) {
p = filepath.Join(dir, p)
}
mp, err := getPatternMaxTime(p)
if err != nil {
return time.Time{}, err

View File

@ -2,6 +2,7 @@ package task_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -149,6 +150,61 @@ func TestStatus(t *testing.T) {
}
}
func TestGenerates(t *testing.T) {
var srcTask = "sub/src.txt"
var relTask = "rel.txt"
var absTask = "abs.txt"
// This test does not work with a relative dir.
dir, err := filepath.Abs("testdata/generates")
assert.NoError(t, err)
var srcFile = filepath.Join(dir, srcTask)
for _, task := range []string{srcTask, relTask, absTask} {
path := filepath.Join(dir, task)
_ = os.Remove(path)
if _, err := os.Stat(path); err == nil {
t.Errorf("File should not exists: %v", err)
}
}
buff := bytes.NewBuffer(nil)
e := &task.Executor{
Dir: dir,
Stdout: buff,
Stderr: buff,
}
assert.NoError(t, e.ReadTaskfile())
for _, task := range []string{relTask, absTask} {
var destFile = filepath.Join(dir, task)
var upToDate = fmt.Sprintf("task: Task \"%s\" is up to date\n", srcTask) +
fmt.Sprintf("task: Task \"%s\" is up to date\n", task)
// Run task for the first time.
assert.NoError(t, e.Run(task))
if _, err := os.Stat(srcFile); err != nil {
t.Errorf("File should exists: %v", err)
}
if _, err := os.Stat(destFile); err != nil {
t.Errorf("File should exists: %v", err)
}
// Ensure task was not incorrectly found to be up-to-date on first run.
if buff.String() == upToDate {
t.Errorf("Wrong output message: %s", buff.String())
}
buff.Reset()
// Re-run task to ensure it's now found to be up-to-date.
assert.NoError(t, e.Run(task))
if buff.String() != upToDate {
t.Errorf("Wrong output message: %s", buff.String())
}
buff.Reset()
}
}
func TestInit(t *testing.T) {
const dir = "testdata/init"
var file = filepath.Join(dir, "Taskfile.yml")

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

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

34
testdata/generates/Taskfile.yml vendored Normal file
View File

@ -0,0 +1,34 @@
abs.txt:
desc: generates dest file based on absolute paths
deps:
- sub/src.txt
dir: sub
cmds:
- cat src.txt > '{{.BUILD_DIR}}/abs.txt'
sources:
- src.txt
generates:
- "{{.BUILD_DIR}}/abs.txt"
rel.txt:
desc: generates dest file based on relative paths
deps:
- sub/src.txt
dir: sub
cmds:
- cat src.txt > '../rel.txt'
sources:
- src.txt
generates:
- "../rel.txt"
sub/src.txt:
desc: generate source file
cmds:
- mkdir -p sub
- echo "hello world" > sub/src.txt
# Sleep to avoid time-stamp issue. Some filesystems, E.g. HFS+ for Mac OS,
# only support one second precision on file timestamps.
- sleep 1
status:
- test -f sub/src.txt

1
testdata/generates/Taskvars.yml vendored Normal file
View File

@ -0,0 +1 @@
BUILD_DIR: $pwd