temp_dir_fixture.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "os"
  16. "path/filepath"
  17. "regexp"
  18. "runtime"
  19. "strings"
  20. "testing"
  21. )
  22. type TempDirFixture struct {
  23. t testing.TB
  24. dir *TempDir
  25. oldDir string
  26. }
  27. // everything not listed in this character class will get replaced by _, so that it's a safe filename
  28. var sanitizeForFilenameRe = regexp.MustCompile("[^a-zA-Z0-9.]")
  29. func SanitizeFileName(name string) string {
  30. return sanitizeForFilenameRe.ReplaceAllString(name, "_")
  31. }
  32. func NewTempDirFixture(t testing.TB) *TempDirFixture {
  33. dir, err := NewDir(SanitizeFileName(t.Name()))
  34. if err != nil {
  35. t.Fatalf("Error making temp dir: %v", err)
  36. }
  37. ret := &TempDirFixture{
  38. t: t,
  39. dir: dir,
  40. }
  41. t.Cleanup(ret.tearDown)
  42. return ret
  43. }
  44. func (f *TempDirFixture) T() testing.TB {
  45. return f.t
  46. }
  47. func (f *TempDirFixture) Path() string {
  48. return f.dir.Path()
  49. }
  50. func (f *TempDirFixture) Chdir() {
  51. cwd, err := os.Getwd()
  52. if err != nil {
  53. f.t.Fatal(err)
  54. }
  55. f.oldDir = cwd
  56. err = os.Chdir(f.Path())
  57. if err != nil {
  58. f.t.Fatal(err)
  59. }
  60. }
  61. func (f *TempDirFixture) JoinPath(path ...string) string {
  62. p := []string{}
  63. isAbs := len(path) > 0 && filepath.IsAbs(path[0])
  64. if isAbs {
  65. if !strings.HasPrefix(path[0], f.Path()) {
  66. f.t.Fatalf("Path outside fixture tempdir are forbidden: %s", path[0])
  67. }
  68. } else {
  69. p = append(p, f.Path())
  70. }
  71. p = append(p, path...)
  72. return filepath.Join(p...)
  73. }
  74. func (f *TempDirFixture) JoinPaths(paths []string) []string {
  75. joined := make([]string, len(paths))
  76. for i, p := range paths {
  77. joined[i] = f.JoinPath(p)
  78. }
  79. return joined
  80. }
  81. // Returns the full path to the file written.
  82. func (f *TempDirFixture) WriteFile(path string, contents string) string {
  83. fullPath := f.JoinPath(path)
  84. base := filepath.Dir(fullPath)
  85. err := os.MkdirAll(base, os.FileMode(0o777))
  86. if err != nil {
  87. f.t.Fatal(err)
  88. }
  89. err = os.WriteFile(fullPath, []byte(contents), os.FileMode(0o777))
  90. if err != nil {
  91. f.t.Fatal(err)
  92. }
  93. return fullPath
  94. }
  95. // Returns the full path to the file written.
  96. func (f *TempDirFixture) CopyFile(originalPath, newPath string) {
  97. contents, err := os.ReadFile(originalPath)
  98. if err != nil {
  99. f.t.Fatal(err)
  100. }
  101. f.WriteFile(newPath, string(contents))
  102. }
  103. // Read the file.
  104. func (f *TempDirFixture) ReadFile(path string) string {
  105. fullPath := f.JoinPath(path)
  106. contents, err := os.ReadFile(fullPath)
  107. if err != nil {
  108. f.t.Fatal(err)
  109. }
  110. return string(contents)
  111. }
  112. func (f *TempDirFixture) WriteSymlink(linkContents, destPath string) {
  113. fullDestPath := f.JoinPath(destPath)
  114. err := os.MkdirAll(filepath.Dir(fullDestPath), os.FileMode(0o777))
  115. if err != nil {
  116. f.t.Fatal(err)
  117. }
  118. err = os.Symlink(linkContents, fullDestPath)
  119. if err != nil {
  120. f.t.Fatal(err)
  121. }
  122. }
  123. func (f *TempDirFixture) MkdirAll(path string) {
  124. fullPath := f.JoinPath(path)
  125. err := os.MkdirAll(fullPath, os.FileMode(0o777))
  126. if err != nil {
  127. f.t.Fatal(err)
  128. }
  129. }
  130. func (f *TempDirFixture) TouchFiles(paths []string) {
  131. for _, p := range paths {
  132. f.WriteFile(p, "")
  133. }
  134. }
  135. func (f *TempDirFixture) Rm(pathInRepo string) {
  136. fullPath := f.JoinPath(pathInRepo)
  137. err := os.RemoveAll(fullPath)
  138. if err != nil {
  139. f.t.Fatal(err)
  140. }
  141. }
  142. func (f *TempDirFixture) NewFile(prefix string) (*os.File, error) {
  143. return os.CreateTemp(f.dir.Path(), prefix)
  144. }
  145. func (f *TempDirFixture) TempDir(prefix string) string {
  146. name, err := os.MkdirTemp(f.dir.Path(), prefix)
  147. if err != nil {
  148. f.t.Fatal(err)
  149. }
  150. return name
  151. }
  152. func (f *TempDirFixture) tearDown() {
  153. if f.oldDir != "" {
  154. err := os.Chdir(f.oldDir)
  155. if err != nil {
  156. f.t.Fatal(err)
  157. }
  158. }
  159. err := f.dir.TearDown()
  160. if err != nil && runtime.GOOS == "windows" &&
  161. (strings.Contains(err.Error(), "The process cannot access the file") ||
  162. strings.Contains(err.Error(), "Access is denied")) {
  163. // NOTE(nick): I'm not convinced that this is a real problem.
  164. // I think it might just be clean up of file notification I/O.
  165. } else if err != nil {
  166. f.t.Fatal(err)
  167. }
  168. }