path_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package common
  2. import (
  3. "os"
  4. "testing"
  5. checker "gopkg.in/check.v1"
  6. )
  7. func TestGoodFile(t *testing.T) {
  8. goodpath := "~/goethereumtest.pass"
  9. path := ExpandHomePath(goodpath)
  10. contentstring := "3.14159265358979323846"
  11. err := WriteFile(path, []byte(contentstring))
  12. if err != nil {
  13. t.Error("Could not write file")
  14. }
  15. if !FileExist(path) {
  16. t.Error("File not found at", path)
  17. }
  18. v, err := ReadAllFile(path)
  19. if err != nil {
  20. t.Error("Could not read file", path)
  21. }
  22. if v != contentstring {
  23. t.Error("Expected", contentstring, "Got", v)
  24. }
  25. }
  26. func TestBadFile(t *testing.T) {
  27. badpath := "/this/path/should/not/exist/goethereumtest.fail"
  28. path := ExpandHomePath(badpath)
  29. contentstring := "3.14159265358979323846"
  30. err := WriteFile(path, []byte(contentstring))
  31. if err == nil {
  32. t.Error("Wrote file, but should not be able to", path)
  33. }
  34. if FileExist(path) {
  35. t.Error("Found file, but should not be able to", path)
  36. }
  37. v, err := ReadAllFile(path)
  38. if err == nil {
  39. t.Error("Read file, but should not be able to", v)
  40. }
  41. }
  42. type CommonSuite struct{}
  43. var _ = checker.Suite(&CommonSuite{})
  44. func (s *CommonSuite) TestOS(c *checker.C) {
  45. expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';')
  46. res := IsWindows()
  47. if !expwin {
  48. c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
  49. } else {
  50. c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
  51. }
  52. }
  53. func (s *CommonSuite) TestWindonziePath(c *checker.C) {
  54. iswindowspath := os.PathSeparator == '\\'
  55. path := "/opt/eth/test/file.ext"
  56. res := WindonizePath(path)
  57. ressep := string(res[0])
  58. if !iswindowspath {
  59. c.Assert(ressep, checker.Equals, "/")
  60. } else {
  61. c.Assert(ressep, checker.Not(checker.Equals), "/")
  62. }
  63. }