You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
904 B

  1. package common
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. func TestOSGoPath(t *testing.T) {
  7. // restore original gopath upon exit
  8. path := os.Getenv("GOPATH")
  9. defer func() {
  10. _ = os.Setenv("GOPATH", path)
  11. }()
  12. err := os.Setenv("GOPATH", "~/testgopath")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. path = GoPath()
  17. if path != "~/testgopath" {
  18. t.Fatalf("should get GOPATH env var value, got %v", path)
  19. }
  20. os.Unsetenv("GOPATH")
  21. path = GoPath()
  22. if path != "~/testgopath" {
  23. t.Fatalf("subsequent calls should return the same value, got %v", path)
  24. }
  25. }
  26. func TestOSGoPathWithoutEnvVar(t *testing.T) {
  27. // restore original gopath upon exit
  28. path := os.Getenv("GOPATH")
  29. defer func() {
  30. _ = os.Setenv("GOPATH", path)
  31. }()
  32. os.Unsetenv("GOPATH")
  33. // reset cache
  34. gopath = ""
  35. path = GoPath()
  36. if path == "" || path == "~/testgopath" {
  37. t.Fatalf("should get nonempty result of calling go env GOPATH, got %v", path)
  38. }
  39. }