github.com/speakeasy-api/sdk-gen-config@v1.14.2/upgrades_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_upgrade_Success(t *testing.T) {
    10  	getUUID = func() string {
    11  		return "123"
    12  	}
    13  
    14  	type args struct {
    15  		currentVersion string
    16  		cfg            map[string]any
    17  		lockFile       map[string]any
    18  	}
    19  	tests := []struct {
    20  		name         string
    21  		args         args
    22  		wantCfg      map[string]any
    23  		wantLockFile map[string]any
    24  	}{
    25  		{
    26  			name: "upgrades all fields from the original version through v1.0.0 to v2.0.0",
    27  			args: args{
    28  				currentVersion: "",
    29  				cfg: map[string]any{
    30  					"management": map[string]any{
    31  						"openapi-checksum":  "123",
    32  						"openapi-version":   "1.0.0",
    33  						"speakeasy-version": "1.0.0",
    34  					},
    35  					"comments": map[string]any{
    36  						"disabled":                        true,
    37  						"omitdescriptionifsummarypresent": true,
    38  					},
    39  					"baseserverurl":          "http://localhost:8080",
    40  					"sdkclassname":           "MySDK",
    41  					"tagnamespacingdisabled": true,
    42  					"go": map[string]any{
    43  						"version":     "0.0.1",
    44  						"packagename": "openapi",
    45  					},
    46  				},
    47  			},
    48  			wantCfg: map[string]any{
    49  				"configVersion": v2,
    50  				"generation": map[string]any{
    51  					"baseServerUrl": "http://localhost:8080",
    52  					"sdkClassName":  "MySDK",
    53  				},
    54  				"go": map[string]any{
    55  					"version":     "0.0.1",
    56  					"packageName": "openapi",
    57  				},
    58  			},
    59  			wantLockFile: map[string]any{
    60  				"lockVersion": v2,
    61  				"id":          "123",
    62  				"management": map[string]any{
    63  					"docChecksum":      "123",
    64  					"docVersion":       "1.0.0",
    65  					"speakeasyVersion": "1.0.0",
    66  					"releaseVersion":   "0.0.1",
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name: "upgrade only some fields from the original version through v1.0.0 to v2.0.0",
    72  			args: args{
    73  				currentVersion: "",
    74  				cfg: map[string]any{
    75  					"sdkclassname": "MySDK",
    76  					"go": map[string]any{
    77  						"version":     "0.0.1",
    78  						"packagename": "openapi",
    79  					},
    80  				},
    81  			},
    82  			wantCfg: map[string]any{
    83  				"configVersion": v2,
    84  				"generation": map[string]any{
    85  					"sdkClassName": "MySDK",
    86  				},
    87  				"go": map[string]any{
    88  					"version":     "0.0.1",
    89  					"packageName": "openapi",
    90  				},
    91  			},
    92  			wantLockFile: map[string]any{
    93  				"lockVersion": v2,
    94  				"id":          "123",
    95  				"management": map[string]any{
    96  					"releaseVersion": "0.0.1",
    97  				},
    98  			},
    99  		},
   100  		{
   101  			name: "upgrades from v1.0.0 to v2.0.0",
   102  			args: args{
   103  				currentVersion: v1,
   104  				cfg: map[string]any{
   105  					"configVersion": v1,
   106  					"management": map[string]any{
   107  						"docChecksum":      "123",
   108  						"docVersion":       "1.0.0",
   109  						"speakeasyVersion": "1.0.0",
   110  					},
   111  					"generation": map[string]any{
   112  						"baseServerUrl":          "http://localhost:8080",
   113  						"sdkClassName":           "MySDK",
   114  						"tagNamespacingDisabled": true,
   115  						"singleTagPerOp":         true,
   116  						"comments": map[string]any{
   117  							"disableComments":                 true,
   118  							"omitDescriptionIfSummaryPresent": true,
   119  						},
   120  						"repoURL": "http://localhost:8080",
   121  					},
   122  					"features": map[string]map[string]string{
   123  						"go": {
   124  							"core":   "1.0.0",
   125  							"errors": "1.0.0",
   126  						},
   127  					},
   128  					"go": map[string]any{
   129  						"version":          "0.0.1",
   130  						"packageName":      "openapi",
   131  						"published":        true,
   132  						"installationURL":  "https://github.com/speakeasy-api/sdk",
   133  						"repoSubDirectory": "./go",
   134  					},
   135  				},
   136  			},
   137  			wantCfg: map[string]any{
   138  				"configVersion": v2,
   139  				"generation": map[string]any{
   140  					"baseServerUrl": "http://localhost:8080",
   141  					"sdkClassName":  "MySDK",
   142  				},
   143  				"go": map[string]any{
   144  					"version":     "0.0.1",
   145  					"packageName": "openapi",
   146  				},
   147  			},
   148  			wantLockFile: map[string]any{
   149  				"lockVersion": v2,
   150  				"id":          "123",
   151  				"management": map[string]any{
   152  					"docChecksum":      "123",
   153  					"docVersion":       "1.0.0",
   154  					"speakeasyVersion": "1.0.0",
   155  					"repoURL":          "http://localhost:8080",
   156  					"published":        true,
   157  					"installationURL":  "https://github.com/speakeasy-api/sdk",
   158  					"repoSubDirectory": "./go",
   159  					"releaseVersion":   "0.0.1",
   160  				},
   161  				"features": map[string]map[string]string{
   162  					"go": {
   163  						"core":   "1.0.0",
   164  						"errors": "1.0.0",
   165  					},
   166  				},
   167  			},
   168  		},
   169  	}
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			upgraded, lockFile, err := upgrade(tt.args.currentVersion, tt.args.cfg, tt.args.lockFile, testUpdateLang)
   173  			assert.NoError(t, err)
   174  			assert.Equal(t, tt.wantCfg, upgraded)
   175  			assert.Equal(t, tt.wantLockFile, lockFile)
   176  		})
   177  	}
   178  }
   179  
   180  func testUpdateLang(lang, template, oldVersion, newVersion string, cfg map[string]any) (map[string]any, error) {
   181  	if oldVersion == "" {
   182  		switch lang {
   183  		case "go":
   184  			upgraded := map[string]any{
   185  				"version":     "0.0.1",
   186  				"packageName": "openapi",
   187  			}
   188  
   189  			version, ok := cfg["version"]
   190  			if ok {
   191  				upgraded["version"] = version
   192  			}
   193  
   194  			packageName, ok := cfg["packagename"]
   195  			if ok {
   196  				upgraded["packageName"] = packageName
   197  			}
   198  
   199  			return upgraded, nil
   200  		}
   201  	}
   202  
   203  	return cfg, nil
   204  }