testos_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "os"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. )
  12. // fatal is the required common interface between *testing.B and *testing.T
  13. type fatal interface {
  14. Fatal(...interface{})
  15. Helper()
  16. }
  17. type fatalOs struct {
  18. fatal
  19. }
  20. func must(f fatal, err error) {
  21. f.Helper()
  22. if err != nil {
  23. f.Fatal(err)
  24. }
  25. }
  26. func mustRemove(f fatal, err error) {
  27. f.Helper()
  28. if err != nil && !fs.IsNotExist(err) {
  29. f.Fatal(err)
  30. }
  31. }
  32. func (f *fatalOs) Chmod(name string, mode os.FileMode) {
  33. f.Helper()
  34. must(f, os.Chmod(name, mode))
  35. }
  36. func (f *fatalOs) Chtimes(name string, atime time.Time, mtime time.Time) {
  37. f.Helper()
  38. must(f, os.Chtimes(name, atime, mtime))
  39. }
  40. func (f *fatalOs) Create(name string) *os.File {
  41. f.Helper()
  42. file, err := os.Create(name)
  43. must(f, err)
  44. return file
  45. }
  46. func (f *fatalOs) Mkdir(name string, perm os.FileMode) {
  47. f.Helper()
  48. must(f, os.Mkdir(name, perm))
  49. }
  50. func (f *fatalOs) MkdirAll(name string, perm os.FileMode) {
  51. f.Helper()
  52. must(f, os.MkdirAll(name, perm))
  53. }
  54. func (f *fatalOs) Remove(name string) {
  55. f.Helper()
  56. if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
  57. f.Fatal(err)
  58. }
  59. }
  60. func (f *fatalOs) RemoveAll(name string) {
  61. f.Helper()
  62. if err := os.RemoveAll(name); err != nil && !os.IsNotExist(err) {
  63. f.Fatal(err)
  64. }
  65. }
  66. func (f *fatalOs) Rename(oldname, newname string) {
  67. f.Helper()
  68. must(f, os.Rename(oldname, newname))
  69. }
  70. func (f *fatalOs) Stat(name string) os.FileInfo {
  71. f.Helper()
  72. info, err := os.Stat(name)
  73. must(f, err)
  74. return info
  75. }