Skip to content

Commit

Permalink
Fix pointer diffs always triggering (#155)
Browse files Browse the repository at this point in the history
* Fix the mktemp diff

* More better diffs
  • Loading branch information
UnstoppableMango authored Aug 19, 2024
1 parent 27c9bd8 commit fba2524
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
12 changes: 12 additions & 0 deletions provider/pkg/provider/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ func (s *State[T]) Diff(ctx context.Context, inputs CommandArgs[T]) (map[string]

return diff, nil
}

func Changed[T comparable](a *T, b *T) bool {
if a == b {
return false
}

if a == nil || b == nil {
return true
}

return *a != *b
}
20 changes: 8 additions & 12 deletions provider/pkg/provider/coreutils/mktemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,34 @@ func (Mktemp) Diff(ctx context.Context, id string, olds MktempState, news cmd.Co
return provider.DiffResponse{}, fmt.Errorf("mv: %w", err)
}

if news.Args.Directory != olds.Args.Directory {
if cmd.Changed(news.Args.Directory, olds.Args.Directory) {
diff["args.directory"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.DryRun != olds.Args.DryRun {
diff["args.destination"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
if cmd.Changed(news.Args.DryRun, olds.Args.DryRun) {
diff["args.dryRun"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Quiet != olds.Args.Quiet {
if cmd.Changed(news.Args.Quiet, olds.Args.Quiet) {
diff["args.quiet"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Suffix != olds.Args.Suffix {
if cmd.Changed(news.Args.Suffix, olds.Args.Suffix) {
diff["args.suffix"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.T != olds.Args.T {
if cmd.Changed(news.Args.T, olds.Args.T) {
diff["args.t"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Template != olds.Args.Template {
if cmd.Changed(news.Args.Template, olds.Args.Template) {
diff["args.template"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.TmpDir != olds.Args.TmpDir {
if cmd.Changed(news.Args.TmpDir, olds.Args.TmpDir) {
diff["args.tmpDir"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Directory != olds.Args.Directory {
diff["args.suffix"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

return provider.DiffResponse{
DeleteBeforeReplace: true,
HasChanges: len(diff) > 0,
Expand Down
20 changes: 10 additions & 10 deletions provider/pkg/provider/coreutils/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,47 +101,47 @@ func (Mv) Diff(ctx context.Context, id string, olds MvState, news cmd.CommandArg

defaultKind := news.UpdateKind()

if news.Args.Backup != olds.Args.Backup {
if cmd.Changed(news.Args.Backup, olds.Args.Backup) {
diff["args.backup"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.Destination != olds.Args.Destination {
if cmd.Changed(news.Args.Destination, olds.Args.Destination) {
diff["args.destination"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.Directory != olds.Args.Directory {
if cmd.Changed(news.Args.Directory, olds.Args.Directory) {
diff["args.directory"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.Force != olds.Args.Force {
if cmd.Changed(news.Args.Force, olds.Args.Force) {
diff["args.force"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.NoClobber != olds.Args.NoClobber {
if cmd.Changed(news.Args.NoClobber, olds.Args.NoClobber) {
diff["args.noClobber"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.NoTargetDirectory != olds.Args.NoTargetDirectory {
if cmd.Changed(news.Args.NoTargetDirectory, olds.Args.NoTargetDirectory) {
diff["args.noTargetDirectory"] = provider.PropertyDiff{Kind: defaultKind}
}

if !slices.Equal(news.Args.Source, olds.Args.Source) {
diff["args.source"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.StripTrailingSlashes != olds.Args.StripTrailingSlashes {
if cmd.Changed(news.Args.StripTrailingSlashes, olds.Args.StripTrailingSlashes) {
diff["args.stripTrailingSlashes"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.Suffix != olds.Args.Suffix {
if cmd.Changed(news.Args.Suffix, olds.Args.Suffix) {
diff["args.suffix"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.TargetDirectory != olds.Args.TargetDirectory {
if cmd.Changed(news.Args.TargetDirectory, olds.Args.TargetDirectory) {
diff["args.targetDirectory"] = provider.PropertyDiff{Kind: defaultKind}
}

if news.Args.Update != olds.Args.Update {
if cmd.Changed(news.Args.Update, olds.Args.Update) {
diff["args.update"] = provider.PropertyDiff{Kind: defaultKind}
}

Expand Down
8 changes: 4 additions & 4 deletions provider/pkg/provider/coreutils/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ func (Rm) Diff(ctx context.Context, id string, olds RmState, news cmd.CommandArg
return provider.DiffResponse{}, fmt.Errorf("rm: %w", err)
}

if news.Args.Dir != olds.Args.Dir {
if cmd.Changed(news.Args.Dir, olds.Args.Dir) {
diff["args.dir"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if !slices.Equal(news.Args.Files, olds.Args.Files) {
diff["args.files"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Force != olds.Args.Force {
if cmd.Changed(news.Args.Force, olds.Args.Force) {
diff["args.force"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.OneFileSystem != olds.Args.OneFileSystem {
if cmd.Changed(news.Args.OneFileSystem, olds.Args.OneFileSystem) {
diff["args.oneFileSystem"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Recursive != olds.Args.Recursive {
if cmd.Changed(news.Args.Recursive, olds.Args.Recursive) {
diff["args.recursive"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func (Tee) Diff(ctx context.Context, id string, olds TeeState, news cmd.CommandA
return provider.DiffResponse{}, fmt.Errorf("tee: %w", err)
}

if news.Args.Append != olds.Args.Append {
if cmd.Changed(news.Args.Append, olds.Args.Append) {
diff["args.append"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

if news.Args.Stdin != olds.Args.Stdin {
if cmd.Changed(news.Args.Stdin, olds.Args.Stdin) {
diff["args.stdin"] = provider.PropertyDiff{Kind: provider.UpdateReplace}
}

Expand Down
43 changes: 41 additions & 2 deletions tests/lifecycle/mktemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,48 @@ var _ = Describe("Mktemp", func() {
},
},
})
})

It("should not execute when unchanged", func(ctx context.Context) {
var firstDir string

_, err := provisioner.Exec(ctx, "touch", "blah")
Expect(err).NotTo(HaveOccurred())
run(server, integration.LifeCycleTest{
Resource: resource,
Create: integration.Operation{
Inputs: pr.NewPropertyMapFromMap(map[string]interface{}{
"args": map[string]interface{}{
"tmpdir": true,
},
}),
Hook: func(inputs, output pr.PropertyMap) {
Expect(output["stderr"]).To(HavePropertyValue(""))
Expect(output["stdout"].V).NotTo(BeEmpty())
Expect(output["exitCode"].V).To(BeEquivalentTo(0))
Expect(output["createdFiles"].V).To(BeEmpty())
Expect(output["movedFiles"].V).To(BeEmpty())
firstDir = output["stdout"].V.(string)
},
},
Updates: []integration.Operation{
{
Inputs: pr.NewPropertyMapFromMap(map[string]interface{}{
"args": map[string]interface{}{
"tmpdir": true,
},
}),
Hook: func(inputs, output pr.PropertyMap) {
Expect(output["stderr"]).To(HavePropertyValue(""))
Expect(firstDir).NotTo(BeEmpty())
Expect(output["stdout"]).To(HavePropertyValue(firstDir))
Expect(output["exitCode"].V).To(BeEquivalentTo(0))
Expect(output["triggers"]).To(Equal(pr.NewArrayProperty([]pr.PropertyValue{
pr.NewProperty("a trigger"),
})))
Expect(inputs["args"]).To(Equal(output["args"]))
},
},
},
})
})

It("should fail when template is invalid", func() {
Expand Down

0 comments on commit fba2524

Please sign in to comment.