1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00

Don't persist new checksum on the disk if dry mode is enabled

Fixes #166
This commit is contained in:
Andrey Nering 2019-02-09 10:41:19 -02:00
parent 713ecd35f6
commit 902f0d3ac4
7 changed files with 57 additions and 14 deletions

View File

@ -6,7 +6,10 @@
by prefixing it with `:`
([#161](https://github.com/go-task/task/issues/161), [#172](https://github.com/go-task/task/issues/172)),
- Add flag to override the `output` option
([#173](https://github.com/go-task/task/pull/173)).
([#173](https://github.com/go-task/task/pull/173));
- Fix bug where Task was persisting the new checksum on the disk when the Dry
Mode is enabled
([#166](https://github.com/go-task/task/issues/166)).
## v2.3.0 - 2019-01-02

View File

@ -17,6 +17,7 @@ type Checksum struct {
Dir string
Task string
Sources []string
Dry bool
}
// IsUpToDate implements the Checker interface
@ -36,9 +37,11 @@ func (c *Checksum) IsUpToDate() (bool, error) {
return false, nil
}
_ = os.MkdirAll(filepath.Join(c.Dir, ".task", "checksum"), 0755)
if err = ioutil.WriteFile(checksumFile, []byte(newMd5+"\n"), 0644); err != nil {
return false, err
if !c.Dry {
_ = os.MkdirAll(filepath.Join(c.Dir, ".task", "checksum"), 0755)
if err = ioutil.WriteFile(checksumFile, []byte(newMd5+"\n"), 0644); err != nil {
return false, err
}
}
return oldMd5 == newMd5, nil
}

View File

@ -16,7 +16,7 @@ func (e *Executor) Status(ctx context.Context, calls ...taskfile.Call) error {
if err != nil {
return err
}
isUpToDate, err := isTaskUpToDate(ctx, t)
isUpToDate, err := e.isTaskUpToDate(ctx, t)
if err != nil {
return err
}
@ -27,12 +27,12 @@ func (e *Executor) Status(ctx context.Context, calls ...taskfile.Call) error {
return nil
}
func isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
if len(t.Status) > 0 {
return isTaskUpToDateStatus(ctx, t)
return e.isTaskUpToDateStatus(ctx, t)
}
checker, err := getStatusChecker(t)
checker, err := e.getStatusChecker(t)
if err != nil {
return false, err
}
@ -40,15 +40,15 @@ func isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
return checker.IsUpToDate()
}
func statusOnError(t *taskfile.Task) error {
checker, err := getStatusChecker(t)
func (e *Executor) statusOnError(t *taskfile.Task) error {
checker, err := e.getStatusChecker(t)
if err != nil {
return err
}
return checker.OnError()
}
func getStatusChecker(t *taskfile.Task) (status.Checker, error) {
func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) {
switch t.Method {
case "", "timestamp":
return &status.Timestamp{
@ -61,6 +61,7 @@ func getStatusChecker(t *taskfile.Task) (status.Checker, error) {
Dir: t.Dir,
Task: t.Task,
Sources: t.Sources,
Dry: e.Dry,
}, nil
case "none":
return status.None{}, nil
@ -69,7 +70,7 @@ func getStatusChecker(t *taskfile.Task) (status.Checker, error) {
}
}
func isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) {
func (e *Executor) isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) {
for _, s := range t.Status {
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: s,

View File

@ -181,7 +181,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
}
if !e.Force {
upToDate, err := isTaskUpToDate(ctx, t)
upToDate, err := e.isTaskUpToDate(ctx, t)
if err != nil {
return err
}
@ -195,7 +195,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
for i := range t.Cmds {
if err := e.runCommand(ctx, t, call, i); err != nil {
if err2 := statusOnError(t); err2 != nil {
if err2 := e.statusOnError(t); err2 != nil {
e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
}

View File

@ -473,6 +473,32 @@ func TestDry(t *testing.T) {
}
}
// TestDryChecksum tests if the checksum file is not being written to disk
// if the dry mode is enabled.
func TestDryChecksum(t *testing.T) {
const dir = "testdata/dry_checksum"
checksumFile := filepath.Join(dir, ".task/checksum/default")
_ = os.Remove(checksumFile)
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
Dry: true,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
_, err := os.Stat(checksumFile)
assert.Error(t, err, "checksum file should not exist")
e.Dry = false
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "default"}))
_, err = os.Stat(checksumFile)
assert.NoError(t, err, "checksum file should exist")
}
func TestIncludes(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/includes",

9
testdata/dry_checksum/Taskfile.yml vendored Normal file
View File

@ -0,0 +1,9 @@
version: '2'
tasks:
default:
cmds:
- echo "Working..."
sources:
- source.txt
method: checksum

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

@ -0,0 +1 @@
Something...