mirror of
https://github.com/go-task/task.git
synced 2025-01-06 03:53:54 +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:
parent
a586fef414
commit
86a23849e0
8
file.go
8
file.go
@ -23,7 +23,9 @@ func maxTime(a, b time.Time) time.Time {
|
|||||||
|
|
||||||
func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error) {
|
func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error) {
|
||||||
for _, p := range patterns {
|
for _, p := range patterns {
|
||||||
p = filepath.Join(dir, p)
|
if !filepath.IsAbs(p) {
|
||||||
|
p = filepath.Join(dir, p)
|
||||||
|
}
|
||||||
mp, err := getPatternMinTime(p)
|
mp, err := getPatternMinTime(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
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) {
|
func getPatternsMaxTime(dir string, patterns []string) (m time.Time, err error) {
|
||||||
for _, p := range patterns {
|
for _, p := range patterns {
|
||||||
p = filepath.Join(dir, p)
|
if !filepath.IsAbs(p) {
|
||||||
|
p = filepath.Join(dir, p)
|
||||||
|
}
|
||||||
mp, err := getPatternMaxTime(p)
|
mp, err := getPatternMaxTime(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
return time.Time{}, err
|
||||||
|
56
task_test.go
56
task_test.go
@ -2,6 +2,7 @@ package task_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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) {
|
func TestInit(t *testing.T) {
|
||||||
const dir = "testdata/init"
|
const dir = "testdata/init"
|
||||||
var file = filepath.Join(dir, "Taskfile.yml")
|
var file = filepath.Join(dir, "Taskfile.yml")
|
||||||
|
1
testdata/generates/.gitignore
vendored
Normal file
1
testdata/generates/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.txt
|
34
testdata/generates/Taskfile.yml
vendored
Normal file
34
testdata/generates/Taskfile.yml
vendored
Normal 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
1
testdata/generates/Taskvars.yml
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
BUILD_DIR: $pwd
|
Loading…
Reference in New Issue
Block a user