attribution_migration_test.go

 1package config
 2
 3import (
 4	"testing"
 5
 6	"github.com/stretchr/testify/require"
 7)
 8
 9func TestAttributionMigration(t *testing.T) {
10	t.Parallel()
11
12	tests := []struct {
13		name             string
14		configJSON       string
15		expectedTrailer  TrailerStyle
16		expectedGenerate bool
17	}{
18		{
19			name: "old setting co_authored_by=true migrates to co-authored-by",
20			configJSON: `{
21				"options": {
22					"attribution": {
23						"co_authored_by": true,
24						"generated_with": false
25					}
26				}
27			}`,
28			expectedTrailer:  TrailerStyleCoAuthoredBy,
29			expectedGenerate: false,
30		},
31		{
32			name: "old setting co_authored_by=false migrates to none",
33			configJSON: `{
34				"options": {
35					"attribution": {
36						"co_authored_by": false,
37						"generated_with": true
38					}
39				}
40			}`,
41			expectedTrailer:  TrailerStyleNone,
42			expectedGenerate: true,
43		},
44		{
45			name: "new setting takes precedence over old setting",
46			configJSON: `{
47				"options": {
48					"attribution": {
49						"trailer_style": "assisted-by",
50						"co_authored_by": true,
51						"generated_with": false
52					}
53				}
54			}`,
55			expectedTrailer:  TrailerStyleAssistedBy,
56			expectedGenerate: false,
57		},
58		{
59			name: "default when neither setting present",
60			configJSON: `{
61				"options": {
62					"attribution": {
63						"generated_with": true
64					}
65				}
66			}`,
67			expectedTrailer:  TrailerStyleAssistedBy,
68			expectedGenerate: true,
69		},
70		{
71			name: "default when attribution is null",
72			configJSON: `{
73				"options": {}
74			}`,
75			expectedTrailer:  TrailerStyleAssistedBy,
76			expectedGenerate: true,
77		},
78	}
79
80	for _, tt := range tests {
81		t.Run(tt.name, func(t *testing.T) {
82			t.Parallel()
83
84			cfg, err := loadFromBytes([][]byte{[]byte(tt.configJSON)})
85			require.NoError(t, err)
86
87			cfg.setDefaults(t.TempDir(), "")
88
89			require.Equal(t, tt.expectedTrailer, cfg.Options.Attribution.TrailerStyle)
90			require.Equal(t, tt.expectedGenerate, cfg.Options.Attribution.GeneratedWith)
91		})
92	}
93}