@@ -212,3 +212,79 @@ func TestWriteToFileAndRoundTrip(t *testing.T) {
212212 t .Fatalf ("expected commit message preserved" )
213213 }
214214}
215+
216+ func TestLoadFromFileLegacyChartsKey (t * testing.T ) {
217+ tmp := filepath .Join (t .TempDir (), "legacy.json" )
218+ legacy := []byte (`{
219+ "last_commit": "legacy123",
220+ "charts": {
221+ "myapp": {
222+ "versions": {
223+ "2.0.0": {
224+ "version": "2.0.0",
225+ "train": "stable",
226+ "commits": {
227+ "h1": {
228+ "commit_hash": "h1",
229+ "parent_hash": "p1",
230+ "author": {"name": "dev", "date": "2024-06-01"},
231+ "kind": "fix",
232+ "message": "legacy commit"
233+ }
234+ }
235+ }
236+ }
237+ }
238+ }
239+ }` )
240+ if err := os .WriteFile (tmp , legacy , 0644 ); err != nil {
241+ t .Fatalf ("write legacy file: %v" , err )
242+ }
243+
244+ var cd ChangedData
245+ cd .mu = & sync.RWMutex {}
246+ if err := cd .LoadFromFile (tmp ); err != nil {
247+ t .Fatalf ("LoadFromFile legacy: %v" , err )
248+ }
249+ if cd .LastCommit != "legacy123" {
250+ t .Fatalf ("expected LastCommit legacy123, got %s" , cd .LastCommit )
251+ }
252+ if cd .Apps ["myapp" ] == nil {
253+ t .Fatalf ("expected myapp to be loaded from legacy charts key" )
254+ }
255+ if cd .Apps ["myapp" ].Versions ["2.0.0" ].Commits ["h1" ].Message != "legacy commit" {
256+ t .Fatalf ("expected legacy commit message preserved" )
257+ }
258+ }
259+
260+ func TestLoadFromFileWritesAppsKey (t * testing.T ) {
261+ tmp := filepath .Join (t .TempDir (), "roundtrip.json" )
262+ cd := ChangedData {
263+ mu : & sync.RWMutex {},
264+ LastCommit : "rt1" ,
265+ Apps : map [string ]* App {"testapp" : {Versions : map [string ]* Version {}}},
266+ }
267+ if err := cd .WriteToFile (tmp ); err != nil {
268+ t .Fatalf ("WriteToFile: %v" , err )
269+ }
270+ raw , _ := os .ReadFile (tmp )
271+ if ! contains (string (raw ), `"apps"` ) {
272+ t .Fatalf ("expected written JSON to use \" apps\" key, got: %s" , string (raw ))
273+ }
274+ if contains (string (raw ), `"charts"` ) {
275+ t .Fatalf ("written JSON must not contain legacy \" charts\" key" )
276+ }
277+ }
278+
279+ func contains (s , substr string ) bool {
280+ return len (s ) >= len (substr ) && (s == substr || len (s ) > 0 && containsHelper (s , substr ))
281+ }
282+
283+ func containsHelper (s , substr string ) bool {
284+ for i := 0 ; i <= len (s )- len (substr ); i ++ {
285+ if s [i :i + len (substr )] == substr {
286+ return true
287+ }
288+ }
289+ return false
290+ }
0 commit comments