@@ -22,6 +22,9 @@ package main
22
22
import (
23
23
"context"
24
24
"fmt"
25
+ "io"
26
+ "os"
27
+ "path/filepath"
25
28
"testing"
26
29
"time"
27
30
@@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
65
68
argsTpl string
66
69
pushFile string
67
70
diffFile string
71
+ diffName string
68
72
assert assertFunc
69
73
}{
70
74
{
@@ -75,14 +79,50 @@ func TestDiffArtifact(t *testing.T) {
75
79
diffFile : "./testdata/diff-artifact/deployment.yaml" ,
76
80
assert : assertGoldenFile ("testdata/diff-artifact/success.golden" ),
77
81
},
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
+ },
78
95
{
79
96
name : "should fail if there is a diff" ,
80
97
url : "oci://%s/podinfo:2.0.0" ,
81
98
argsTpl : "diff artifact %s --path=%s" ,
82
99
pushFile : "./testdata/diff-artifact/deployment.yaml" ,
83
100
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
+ ),
85
107
},
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.
86
126
}
87
127
88
128
ctx := ctrl .SetupSignalHandler ()
@@ -99,11 +139,38 @@ func TestDiffArtifact(t *testing.T) {
99
139
t .Fatalf (fmt .Errorf ("failed to push image: %w" , err ).Error ())
100
140
}
101
141
142
+ diffFile := tt .diffFile
143
+ if tt .diffName != "" {
144
+ diffFile = makeTempFile (t , tt .diffFile , tt .diffName )
145
+ }
146
+
102
147
cmd := cmdTestCase {
103
- args : fmt .Sprintf (tt .argsTpl , tt .url , tt . diffFile ),
148
+ args : fmt .Sprintf (tt .argsTpl , tt .url , diffFile ),
104
149
assert : tt .assert ,
105
150
}
106
151
cmd .runTestCmd (t )
107
152
})
108
153
}
109
154
}
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