attribution_migration_test.go

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