@@ -22,6 +22,9 @@ package main
2222import (
2323 "context"
2424 "fmt"
25+ "io"
26+ "os"
27+ "path/filepath"
2528 "testing"
2629 "time"
2730
@@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
6568 argsTpl string
6669 pushFile string
6770 diffFile string
71+ diffName string
6872 assert assertFunc
6973 }{
7074 {
@@ -75,14 +79,50 @@ func TestDiffArtifact(t *testing.T) {
7579 diffFile : "./testdata/diff-artifact/deployment.yaml" ,
7680 assert : assertGoldenFile ("testdata/diff-artifact/success.golden" ),
7781 },
82+ {
83+ name : "create unified diff output by default" ,
84+ url : "oci://%s/podinfo:2.0.0" ,
85+ argsTpl : "diff artifact %s --path=%s" ,
86+ pushFile : "./testdata/diff-artifact/deployment.yaml" ,
87+ diffFile : "./testdata/diff-artifact/deployment-diff.yaml" ,
88+ diffName : "deployment.yaml" ,
89+ assert : assert (
90+ assertErrorIs (ErrDiffArtifactChanged ),
91+ assertRegexp (`(?m)^- cpu: 1000m$` ),
92+ assertRegexp (`(?m)^\+ cpu: 2000m$` ),
93+ ),
94+ },
7895 {
7996 name : "should fail if there is a diff" ,
8097 url : "oci://%s/podinfo:2.0.0" ,
8198 argsTpl : "diff artifact %s --path=%s" ,
8299 pushFile : "./testdata/diff-artifact/deployment.yaml" ,
83100 diffFile : "./testdata/diff-artifact/deployment-diff.yaml" ,
84- assert : assertErrorIs (ErrDiffArtifactChanged ),
101+ diffName : "only-local.yaml" ,
102+ assert : assert (
103+ assertErrorIs (ErrDiffArtifactChanged ),
104+ assertRegexp (`(?m)^Only in [^:]+: deployment.yaml$` ),
105+ assertRegexp (`(?m)^Only in [^:]+: only-local.yaml$` ),
106+ ),
85107 },
108+ {
109+ name : "semantic diff using dyff" ,
110+ url : "oci://%s/podinfo:2.0.0" ,
111+ argsTpl : "diff artifact %s --path=%s --differ=dyff" ,
112+ pushFile : "./testdata/diff-artifact/deployment.yaml" ,
113+ diffFile : "./testdata/diff-artifact/deployment-diff.yaml" ,
114+ diffName : "deployment.yaml" ,
115+ assert : assert (
116+ assertErrorIs (ErrDiffArtifactChanged ),
117+ assertRegexp (`(?m)^spec.template.spec.containers.podinfod.resources.limits.cpu$` ),
118+ assertRegexp (`(?m)^ ± value change$` ),
119+ assertRegexp (`(?m)^ - 1000m$` ),
120+ assertRegexp (`(?m)^ \+ 2000m$` ),
121+ ),
122+ },
123+ // Attention: tests do not spawn a new process when executing commands.
124+ // That means that the --differ flag remains set to "dyff" for
125+ // subsequent tests.
86126 }
87127
88128 ctx := ctrl .SetupSignalHandler ()
@@ -99,11 +139,38 @@ func TestDiffArtifact(t *testing.T) {
99139 t .Fatalf (fmt .Errorf ("failed to push image: %w" , err ).Error ())
100140 }
101141
142+ diffFile := tt .diffFile
143+ if tt .diffName != "" {
144+ diffFile = makeTempFile (t , tt .diffFile , tt .diffName )
145+ }
146+
102147 cmd := cmdTestCase {
103- args : fmt .Sprintf (tt .argsTpl , tt .url , tt . diffFile ),
148+ args : fmt .Sprintf (tt .argsTpl , tt .url , diffFile ),
104149 assert : tt .assert ,
105150 }
106151 cmd .runTestCmd (t )
107152 })
108153 }
109154}
155+
156+ func makeTempFile (t * testing.T , source , basename string ) string {
157+ path := filepath .Join (t .TempDir (), basename )
158+ out , err := os .Create (path )
159+ if err != nil {
160+ t .Fatal (err )
161+ }
162+ defer out .Close ()
163+
164+ in , err := os .Open (source )
165+ if err != nil {
166+ t .Fatal (err )
167+ }
168+ defer in .Close ()
169+
170+ _ , err = io .Copy (out , in )
171+ if err != nil {
172+ t .Fatal (err )
173+ }
174+
175+ return path
176+ }
0 commit comments