mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-23 12:18:51 +02:00
If a `t.FileSystem().FileContent("file.txt", Equals("bla"))` assertion fails because the file doesn't exist, the error would say Expected path 'file.txt' to not exist, but it does which is very confusing.
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package components
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type FileSystem struct {
|
|
*assertionHelper
|
|
}
|
|
|
|
// This does _not_ check the files panel, it actually checks the filesystem
|
|
func (self *FileSystem) PathPresent(path string) {
|
|
self.assertWithRetries(func() (bool, string) {
|
|
_, err := os.Stat(path)
|
|
return err == nil, fmt.Sprintf("Expected path '%s' to exist, but it does not", path)
|
|
})
|
|
}
|
|
|
|
// This does _not_ check the files panel, it actually checks the filesystem
|
|
func (self *FileSystem) PathNotPresent(path string) {
|
|
self.assertWithRetries(func() (bool, string) {
|
|
_, err := os.Stat(path)
|
|
return os.IsNotExist(err), fmt.Sprintf("Expected path '%s' to not exist, but it does", path)
|
|
})
|
|
}
|
|
|
|
// Asserts that the file at the given path has the given content
|
|
func (self *FileSystem) FileContent(path string, matcher *TextMatcher) {
|
|
self.assertWithRetries(func() (bool, string) {
|
|
_, err := os.Stat(path)
|
|
if os.IsNotExist(err) {
|
|
return false, fmt.Sprintf("Expected path '%s' to exist, but it does not", path)
|
|
}
|
|
|
|
output, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return false, fmt.Sprintf("Expected error when reading file content at path '%s': %s", path, err.Error())
|
|
}
|
|
|
|
strOutput := string(output)
|
|
|
|
if ok, errMsg := matcher.context("").test(strOutput); !ok {
|
|
return false, fmt.Sprintf("Unexpected content in file %s: %s", path, errMsg)
|
|
}
|
|
|
|
return true, ""
|
|
})
|
|
}
|