agones.dev/agones@v1.54.0/build/scripts/sdk-update-version/main_test.go (about) 1 // Copyright 2025 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "os" 19 "path/filepath" 20 "testing" 21 ) 22 23 func TestIncrementVersionAfterRelease(t *testing.T) { 24 testCases := []struct { 25 name string 26 input string 27 expected string 28 }{ 29 {"Standard case", "1.2.3", "1.3.3"}, 30 {"Zero case", "1.0.0", "1.1.0"}, 31 {"Double digit", "1.10.10", "1.11.10"}, 32 } 33 34 for _, tc := range testCases { 35 t.Run(tc.name, func(t *testing.T) { 36 result := incrementVersionAfterRelease(tc.input) 37 if result != tc.expected { 38 t.Errorf("expected %s, but got %s", tc.expected, result) 39 } 40 }) 41 } 42 } 43 44 func TestIncrementPatchVersion(t *testing.T) { 45 testCases := []struct { 46 name string 47 input string 48 expected string 49 }{ 50 {"Standard case", "1.2.3", "1.2.4"}, 51 {"Zero case", "1.0.0", "1.0.1"}, 52 {"Double digit", "1.10.9", "1.10.10"}, 53 } 54 55 for _, tc := range testCases { 56 t.Run(tc.name, func(t *testing.T) { 57 result := incrementPatchVersion(tc.input) 58 if result != tc.expected { 59 t.Errorf("expected %s, but got %s", tc.expected, result) 60 } 61 }) 62 } 63 } 64 65 func TestUpdateFile(t *testing.T) { 66 // Create a temporary directory for our test files 67 tmpDir, err := os.MkdirTemp("", "test-update-file") 68 if err != nil { 69 t.Fatalf("Failed to create temp dir: %v", err) 70 } 71 defer func() { 72 // Log error to make linter happy. CI / CD env will clean up, so no need to handle err. 73 if err := os.RemoveAll(tmpDir); err != nil { 74 t.Log(err) 75 } 76 }() 77 78 // Test cases 79 testCases := []struct { 80 name string 81 releaseStage string 82 initialFile string 83 initialContent string 84 version string 85 expectedContent string 86 }{ 87 { 88 "before-json", 89 "before", 90 "test.json", 91 `{"version": "1.2.3-dev"}`, 92 "1.2.3", 93 `{"version": "1.2.3"}`, 94 }, 95 { 96 "before-yaml", 97 "before", 98 "test.yaml", 99 `version: 1.2.3-dev`, 100 "1.2.3", 101 `version: 1.2.3`, 102 }, 103 { 104 "after-makefile", 105 "after", 106 "build/Makefile", 107 `version = 1.2.3`, 108 "1.2.3", 109 `version = 1.3.3`, 110 }, 111 { 112 "after-json", 113 "after", 114 "test.json", 115 `{"version": "1.2.3"}`, 116 "1.2.3", 117 `{"version": "1.3.3-dev"}`, 118 }, 119 { 120 "patch-yaml", 121 "patch", 122 "test.yaml", 123 `version: 1.2.3`, 124 "1.2.3", 125 `version: 1.2.4`, 126 }, 127 { 128 "patch-json", 129 "patch", 130 "test.json", 131 `{"version": "1.2.3"}`, 132 "1.2.3", 133 `{"version": "1.2.4"}`, 134 }, 135 } 136 137 for _, tc := range testCases { 138 t.Run(tc.name, func(t *testing.T) { 139 // Set the global release stage for this test 140 releaseStage = tc.releaseStage 141 142 // Create the test file 143 filePath := filepath.Join(tmpDir, tc.initialFile) 144 err := os.MkdirAll(filepath.Dir(filePath), 0o755) 145 if err != nil { 146 t.Fatalf("Failed to create dir for test file: %v", err) 147 } 148 err = os.WriteFile(filePath, []byte(tc.initialContent), 0o644) 149 if err != nil { 150 t.Fatalf("Failed to write initial test file: %v", err) 151 } 152 153 // Run the function 154 err = updateFile(filePath, tc.version) 155 if err != nil { 156 t.Errorf("UpdateFile returned an error: %v", err) 157 } 158 159 // Read the file and check the content 160 updatedContent, err := os.ReadFile(filePath) 161 if err != nil { 162 t.Fatalf("Failed to read updated test file: %v", err) 163 } 164 165 if string(updatedContent) != tc.expectedContent { 166 t.Errorf("expected content %q, but got %q", tc.expectedContent, string(updatedContent)) 167 } 168 }) 169 } 170 }