https://github.com/sheepla/tdloader
A go module for easily loading test data placed in the Git repository
https://github.com/sheepla/tdloader
Last synced: 12 months ago
JSON representation
A go module for easily loading test data placed in the Git repository
- Host: GitHub
- URL: https://github.com/sheepla/tdloader
- Owner: sheepla
- License: mit
- Created: 2023-01-21T17:41:17.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-21T18:30:01.000Z (over 3 years ago)
- Last Synced: 2025-01-10T21:30:09.111Z (over 1 year ago)
- Language: Go
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tdloader
A go module for easily loading test data placed in the Git repository
## About
This module makes it easy to loading the test data placed in the Go repository.
You can load test data simply by specifying a relative path from the Git repository root e.g. `testdata := tdloader.GetFile("_testdata/data1.json")`
The following functions can be used to load test data.
- `GetPath` - Resolve full path of test data
- `GetFile` - Open test data file and get `*os.File`. Since the `os.File` type implements the `func (f *File) Read(b []byte) (n int, err error)` method, it can also be treated as the `io.Reder` type.
- `GetBytes` - Get the contents of the test data file as a bytes.
- `GetString` - Get the contents of the test data file as a string
Function names prefixed with `Must` will panic when an error occurs.
## Usage
```go
// mypkg.go
package mypkg
func DoSomethingWithFile(f *os.File) error {
// -- snip --
}
```
```go
// mypkg_test.go
package mypkg
import (
"testing"
"github.com/sheepla/tdloader"
)
func TestDoSomething(t *testing.T) {
testdata := tdloader.GetFile("_testdata/data1.json")
if err := DoSomethingWithFile(testdata); err != nil {
t.Fatal(err)
}
// -- snip --
}
```
## Installation
```sh
go get github.com/sheepla/tdloader
```
## Notice
This module runs a `git` command to find the project root directory. When using it, it is necessary that Git is installed and that the repository is managed by Git.
## License
[MIT](https://github.com/sheepla/tdloader/blob/master/LICENSE)
## Author
[sheepla](https://github.com/sheepla)