|
| 1 | +package goutil_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/nao1215/gup/internal/goutil" |
| 13 | +) |
| 14 | + |
| 15 | +// ============================================================================ |
| 16 | +// Tests for public functions |
| 17 | +// ============================================================================ |
| 18 | + |
| 19 | +func ExampleBinaryPathList() { |
| 20 | + // Get list of files in the current directory |
| 21 | + got, err := goutil.BinaryPathList(".") |
| 22 | + if err != nil { |
| 23 | + log.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + want := []string{ |
| 27 | + "examples_test.go", |
| 28 | + "goutil.go", |
| 29 | + "goutil_test.go", |
| 30 | + } |
| 31 | + |
| 32 | + if cmp.Equal(got, want) { |
| 33 | + fmt.Println("Example BinaryPathList: OK") |
| 34 | + } |
| 35 | + // Output: Example BinaryPathList: OK |
| 36 | +} |
| 37 | + |
| 38 | +func ExampleCanUseGoCmd() { |
| 39 | + // If `go` command is available, CanUseGoCmd returns no error |
| 40 | + if err := goutil.CanUseGoCmd(); err != nil { |
| 41 | + log.Fatal(err) // no go command found |
| 42 | + } |
| 43 | + |
| 44 | + fmt.Println("Example CanUseGoCmd: OK") |
| 45 | + // Output: Example CanUseGoCmd: OK |
| 46 | +} |
| 47 | + |
| 48 | +func ExampleGetLatestVer() { |
| 49 | + // Get the latest version of a package |
| 50 | + verLatest, err := goutil.GetLatestVer("github.com/mattn/go-colorable") |
| 51 | + if err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + // As of 2022/09/17, the latest version of go-colorable is v0.1.13 |
| 56 | + expectMin := "v0.1.13" |
| 57 | + |
| 58 | + if strings.Compare(expectMin, verLatest) <= 0 { |
| 59 | + fmt.Println("Example GetLatestVer: OK") |
| 60 | + } else { |
| 61 | + log.Fatalf("latest version is older than expected. expect: %s, latest: %s", |
| 62 | + expectMin, verLatest) |
| 63 | + } |
| 64 | + // Output: Example GetLatestVer: OK |
| 65 | +} |
| 66 | + |
| 67 | +func ExampleGetPackageInformation() { |
| 68 | + pkgInfo := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"}) |
| 69 | + if pkgInfo == nil { |
| 70 | + log.Fatal("example GetPackageInformation failed. The returned package information is nil") |
| 71 | + } |
| 72 | + |
| 73 | + // Expected package information on Linux and macOS |
| 74 | + want := []string{ |
| 75 | + "gal", |
| 76 | + "github.com/nao1215/gal/cmd/gal", |
| 77 | + "github.com/nao1215/gal", |
| 78 | + } |
| 79 | + |
| 80 | + // On Windows, paths are missing |
| 81 | + if runtime.GOOS == "windows" { |
| 82 | + want = []string{ |
| 83 | + "gal", "", "", |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Actual package information |
| 88 | + got := []string{ |
| 89 | + pkgInfo[0].Name, |
| 90 | + pkgInfo[0].ImportPath, |
| 91 | + pkgInfo[0].ModulePath, |
| 92 | + } |
| 93 | + |
| 94 | + if cmp.Equal(got, want) { |
| 95 | + fmt.Println("Example GetPackageInformation: OK") |
| 96 | + } else { |
| 97 | + log.Fatalf("example GetPackageInformation failed. got: %#v, want: %#v", got, want) |
| 98 | + } |
| 99 | + // Output: Example GetPackageInformation: OK |
| 100 | +} |
| 101 | + |
| 102 | +func ExampleGetPackageVersion() { |
| 103 | + // GetPackageVersion returns the version of the package installed via `go install`. |
| 104 | + got := goutil.GetPackageVersion("gup_dummy") |
| 105 | + |
| 106 | + // Non existing binary returns "unknown" |
| 107 | + want := "unknown" |
| 108 | + |
| 109 | + if got == want { |
| 110 | + fmt.Println("Example GetPackageVersion: OK") |
| 111 | + } else { |
| 112 | + log.Fatalf( |
| 113 | + "example GetPackageVersion failed. unexpected return. got: %s, want: %s", |
| 114 | + got, want, |
| 115 | + ) |
| 116 | + } |
| 117 | + // Output: Example GetPackageVersion: OK |
| 118 | +} |
| 119 | + |
| 120 | +func ExampleGoBin() { |
| 121 | + pathDirGoBin, err := goutil.GoBin() |
| 122 | + if err != nil { |
| 123 | + log.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + // By default, GoBin returns the value of GOBIN or GOPATH environment variable. |
| 127 | + // But note that on race condition `os.Getenv()` may return a temporary |
| 128 | + // directory. Such as `/bin` on U*ix environments. |
| 129 | + if pathDirGoBin == "" { |
| 130 | + log.Fatal("example GoBin failed. path to go binary is empty") |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Println("Example GoBin: OK") |
| 134 | + // Output: Example GoBin: OK |
| 135 | +} |
| 136 | + |
| 137 | +func ExampleGoVersionWithOptionM() { |
| 138 | + // GoVersionWithOptionM returns the embedded module version information of |
| 139 | + // the executable. `gal` in this case. |
| 140 | + pathFileBin := filepath.Join("..", "..", "cmd", "testdata", "check_success", "gal") |
| 141 | + if runtime.GOOS == "windows" { |
| 142 | + pathFileBin = filepath.Join("..", "..", "cmd", "testdata", "check_success_for_windows", "gal.exe") |
| 143 | + } |
| 144 | + |
| 145 | + modInfo, err := goutil.GoVersionWithOptionM(pathFileBin) |
| 146 | + if err != nil { |
| 147 | + log.Fatal(err) |
| 148 | + } |
| 149 | + |
| 150 | + for _, info := range modInfo { |
| 151 | + expectContains := "github.com/nao1215/gal" |
| 152 | + if strings.Contains(info, expectContains) { |
| 153 | + fmt.Println("Example GoVersionWithOptionM: OK") |
| 154 | + |
| 155 | + break |
| 156 | + } |
| 157 | + } |
| 158 | + // Output: Example GoVersionWithOptionM: OK |
| 159 | +} |
| 160 | + |
| 161 | +func ExampleInstall() { |
| 162 | + // Install installs an executable from a Go package. |
| 163 | + err := goutil.Install("example.com/unknown_user/unknown_package") |
| 164 | + |
| 165 | + // If the package is not found or invalid, Install returns an error. |
| 166 | + // In this case it should be an error. |
| 167 | + if err == nil { |
| 168 | + log.Fatal("example Install failed. non existing/invalid package should return error") |
| 169 | + } |
| 170 | + |
| 171 | + // Error message should contain the package path |
| 172 | + expectMsg := "can't install example.com/unknown_user/unknown_package" |
| 173 | + |
| 174 | + if strings.Contains(err.Error(), expectMsg) { |
| 175 | + fmt.Println("Example Install: OK") |
| 176 | + } else { |
| 177 | + fmt.Println(err.Error()) |
| 178 | + } |
| 179 | + // Output: Example Install: OK |
| 180 | +} |
| 181 | + |
| 182 | +func ExampleIsAlreadyUpToDate() { |
| 183 | + // Create Version object with Current and Latest package version |
| 184 | + ver := goutil.Version{ |
| 185 | + Current: "v1.9.0", |
| 186 | + Latest: "v1.9.1", |
| 187 | + } |
| 188 | + |
| 189 | + // Check if Current is already up to date (expected: false) |
| 190 | + if goutil.IsAlreadyUpToDate(ver) { |
| 191 | + fmt.Println("Example IsAlreadyUpToDate: already up to date.") |
| 192 | + } else { |
| 193 | + fmt.Println("Example IsAlreadyUpToDate: outdated. Newer latest version exists.") |
| 194 | + } |
| 195 | + |
| 196 | + // Output: Example IsAlreadyUpToDate: outdated. Newer latest version exists. |
| 197 | +} |
| 198 | + |
| 199 | +func ExampleNewGoPaths() { |
| 200 | + // Instantiate GoPaths object |
| 201 | + gp := goutil.NewGoPaths() |
| 202 | + |
| 203 | + // By default, NewGoPaths returns a GoPaths object with the value of GOBIN |
| 204 | + // or GOPATH environment variable of Go. But note that on race condition |
| 205 | + // `os.Getenv()` may return a temporary directory. |
| 206 | + if gp.GOBIN == "" && gp.GOPATH == "" { |
| 207 | + log.Fatal("example NewGoPaths failed. both GOBIN and GOPATH are empty") |
| 208 | + } |
| 209 | + |
| 210 | + fmt.Println("Example NewGoPaths: OK") |
| 211 | + // Output: Example NewGoPaths: OK |
| 212 | +} |
| 213 | + |
| 214 | +func ExampleNewVersion() { |
| 215 | + // Instantiate Version object |
| 216 | + ver := goutil.NewVersion() |
| 217 | + |
| 218 | + // By default, Current and Latest fields are empty |
| 219 | + if ver.Current != "" { |
| 220 | + log.Fatal("example NewVersion failed. the field Current is not empty") |
| 221 | + } |
| 222 | + |
| 223 | + if ver.Latest != "" { |
| 224 | + log.Fatal("example NewVersion failed. the field Latest is not empty") |
| 225 | + } |
| 226 | + |
| 227 | + fmt.Println("Example NewVersion: OK") |
| 228 | + // Output: Example NewVersion: OK |
| 229 | +} |
| 230 | + |
| 231 | +// ============================================================================ |
| 232 | +// Tests for public methods |
| 233 | +// ============================================================================ |
| 234 | + |
| 235 | +// ---------------------------------------------------------------------------- |
| 236 | +// Type: GoPaths |
| 237 | +// ---------------------------------------------------------------------------- |
| 238 | + |
| 239 | +func ExampleGoPaths_StartDryRunMode() { |
| 240 | + gh := goutil.NewGoPaths() |
| 241 | + |
| 242 | + // StartDryRunMode starts dry run mode. In dry run mode, GoPaths will temporarily |
| 243 | + // change the OS env variables of GOBIN or GOPATH. The original values will be |
| 244 | + // restored when the `EndDryRunMode` method is called. |
| 245 | + if err := gh.StartDryRunMode(); err != nil { |
| 246 | + log.Fatalf("example GoPaths.StartDryRunMode failed to start dry mode: %s", err.Error()) |
| 247 | + } |
| 248 | + |
| 249 | + onDryRunMode := []string{ |
| 250 | + os.Getenv("GOBIN"), |
| 251 | + os.Getenv("GOPATH"), |
| 252 | + } |
| 253 | + |
| 254 | + // End dry run mode. |
| 255 | + if err := gh.EndDryRunMode(); err != nil { |
| 256 | + log.Fatalf("example GoPaths.StartDryRunMode failed to end dry mode: %s", err.Error()) |
| 257 | + } |
| 258 | + |
| 259 | + offDryRunMode := []string{ |
| 260 | + os.Getenv("GOBIN"), |
| 261 | + os.Getenv("GOPATH"), |
| 262 | + } |
| 263 | + |
| 264 | + if cmp.Equal(onDryRunMode, offDryRunMode) { |
| 265 | + log.Fatal("example GoPaths.StartDryRunMode failed. dry run mode did not change to temp dir") |
| 266 | + } |
| 267 | + |
| 268 | + fmt.Println("Example GoPaths.StartDryRunMode: OK") |
| 269 | + // Output: Example GoPaths.StartDryRunMode: OK |
| 270 | +} |
| 271 | + |
| 272 | +// ---------------------------------------------------------------------------- |
| 273 | +// Type: Package |
| 274 | +// ---------------------------------------------------------------------------- |
| 275 | + |
| 276 | +func ExamplePackage_CurrentToLatestStr() { |
| 277 | + // Set the paths of the target binary |
| 278 | + packages := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"}) |
| 279 | + if len(packages) == 0 { |
| 280 | + log.Fatal("example GetPackageInformation failed. The returned package information is nil") |
| 281 | + } |
| 282 | + |
| 283 | + // test with the first package found |
| 284 | + pkgInfo := packages[0] |
| 285 | + |
| 286 | + wantContain := "Already up-to-date" |
| 287 | + got := pkgInfo.CurrentToLatestStr() |
| 288 | + |
| 289 | + if !strings.Contains(got, wantContain) { |
| 290 | + log.Fatalf( |
| 291 | + "example Package.CurrentToLatestStr failed. \nwant contain: %s\n got: %s", |
| 292 | + wantContain, got, |
| 293 | + ) |
| 294 | + } |
| 295 | + |
| 296 | + fmt.Println("Example Package.CurrentToLatestStr: OK") |
| 297 | + // Output: Example Package.CurrentToLatestStr: OK |
| 298 | +} |
| 299 | + |
| 300 | +func ExamplePackage_SetLatestVer() { |
| 301 | + packages := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"}) |
| 302 | + if len(packages) == 0 { |
| 303 | + log.Fatal("example GetPackageInformation failed. The returned package information is nil") |
| 304 | + } |
| 305 | + |
| 306 | + // test with the first package found |
| 307 | + pkgInfo := packages[0] |
| 308 | + |
| 309 | + // By default, the Latest field of Package object is empty |
| 310 | + before := pkgInfo.Version.Latest |
| 311 | + |
| 312 | + // Execute method and update the Version.Latest field |
| 313 | + pkgInfo.SetLatestVer() |
| 314 | + |
| 315 | + // After calling SetLatestVer, the Latest field should be updated with the latest |
| 316 | + // version or `unknown` if the latest version is not found. |
| 317 | + after := pkgInfo.Version.Latest |
| 318 | + |
| 319 | + // Require the field to be updated |
| 320 | + if before == after { |
| 321 | + log.Fatalf( |
| 322 | + "example Package.SetLatestVer failed. The latest version is not updated. before: %s, after: %s", |
| 323 | + before, after, |
| 324 | + ) |
| 325 | + } |
| 326 | + |
| 327 | + fmt.Println("Example Package.SetLatestVer: OK") |
| 328 | + // Output: Example Package.SetLatestVer: OK |
| 329 | +} |
| 330 | + |
| 331 | +func ExamplePackage_VersionCheckResultStr() { |
| 332 | + packages := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"}) |
| 333 | + if len(packages) == 0 { |
| 334 | + log.Fatal("example GetPackageInformation failed. The returned package information is nil") |
| 335 | + } |
| 336 | + |
| 337 | + // test with the first package found |
| 338 | + pkgInfo := packages[0] |
| 339 | + |
| 340 | + wantContain := "Already up-to-date" |
| 341 | + got := pkgInfo.VersionCheckResultStr() |
| 342 | + |
| 343 | + if !strings.Contains(got, wantContain) { |
| 344 | + log.Fatalf( |
| 345 | + "example Package.VersionCheckResultStr failed. \nwant contain: %s\n got: %s", |
| 346 | + wantContain, got, |
| 347 | + ) |
| 348 | + } |
| 349 | + |
| 350 | + fmt.Println("Example Package.VersionCheckResultStr: OK") |
| 351 | + // Output: Example Package.VersionCheckResultStr: OK |
| 352 | +} |
0 commit comments