gdebug_testdata.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gdebug
  7. import (
  8. "io/ioutil"
  9. "path/filepath"
  10. )
  11. // TestDataPath retrieves and returns the testdata path of current package,
  12. // which is used for unit testing cases only.
  13. // The optional parameter `names` specifies the sub-folders/sub-files,
  14. // which will be joined with current system separator and returned with the path.
  15. func TestDataPath(names ...string) string {
  16. path := CallerDirectory() + string(filepath.Separator) + "testdata"
  17. for _, name := range names {
  18. path += string(filepath.Separator) + name
  19. }
  20. return path
  21. }
  22. // TestDataContent retrieves and returns the file content for specified testdata path of current package
  23. func TestDataContent(names ...string) string {
  24. path := TestDataPath(names...)
  25. if path != "" {
  26. data, err := ioutil.ReadFile(path)
  27. if err == nil {
  28. return string(data)
  29. }
  30. }
  31. return ""
  32. }