Documentation ¶
Overview ¶
Package tsfio provides a simple API for file input output based on the standard library.
The tsfio package is a supplement to the standard library and supplies additional functions for file input output operations, e.g., appending one file to another file. Also, file input output operations on Linux and Windows system directories or files are blocked (see inval_unix.go and inval_win.go) and an error is returned. All operations expect a directory or a regular file, return an error otherwise. Default flags and file mode is used when opening files, creating files or directories and when writing to files (with exceptions documented in the function descriptions)
- Files are opened read-write (os.O_RDWR).
- Data is appended when writing to file (os.O_APPEND).
- A file is created if it does not exist (os.O_CREATE).
- File mode and permission bits are 0644.
- Directory mode and permissions bits are 0755.
If an API call is not successful, a tserr error in JSON format is returned.
With Printable functions, non-printable runes can be removed from strings and runes. With golden file functions, golden files can be created and test cases evaluated. Golden files can be used in unit tests. The expected output is stored in a golden file. The actual output data will be compared with the golden file. The test fails if there is a difference in actual output and golden file. With normalization functions, new lines in byte slices or strings are normalized to the Unix representation of a new line as line feed LF (0x0A). Therefore, Windows new lines CR LF (0x0D 0x0A) are replaced by Unix new lines LF (0x0A). Also, Mac new lines CR (0x0D) are replaced by Unix new lines LF (0x0A).
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public Licence v3.0 that can be found in the LICENSE file.
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public Licence v3.0 that can be found in the LICENSE file.
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public Licence v3.0 that can be found in the LICENSE file.
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public Licence v3.0 that can be found in the LICENSE file.
Copyright (c) 2023 thorstenrie. All Rights Reserved. Use is governed with GNU Affero General Public Licence v3.0 that can be found in the LICENSE file.
Index ¶
- func AppendFile(a *Append) error
- func CheckDir(d Directory) error
- func CheckFile(f Filename) error
- func CloseFile(f *os.File) error
- func CreateDir(d Directory) error
- func CreateGoldenFile(tc *Testcase) error
- func EvalGoldenFile(tc *Testcase) error
- func ExistsDir(dn Directory) (bool, error)
- func ExistsFile(fn Filename) (bool, error)
- func FileSize(fn Filename) (int64, error)
- func IsPrintable(a []string) (bool, error)
- func NormNewlinesBytes(i []byte) ([]byte, error)
- func NormNewlinesStr(i string) string
- func OpenFile(fn Filename) (*os.File, error)
- func Printable(a string) string
- func ReadFile(f Filename) ([]byte, error)
- func RemoveFile(f Filename) error
- func ResetFile(fn Filename) error
- func RuneToPrintable(r rune) string
- func Sprintf[T Fio](f string, a ...any) T
- func TouchFile(fn Filename) error
- func WriteSingleStr(fn Filename, s string) error
- func WriteStr(fn Filename, s string) error
- type Append
- type Directory
- type Filename
- type Fio
- type Testcase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendFile ¶
AppendFile appends a file to another file. It receives a pointer to struct Append which holds fileA, the file to be extended by contents of fileI and fileI, the input file, holding the contents to be appended to fileA. As result, fileA holds its original content extended by the contents of fileI, and fileI remains as before. If fileA does not exist, it is created as empty file and as result will hold the contents of fileI. If fileI does not exist, it returns an error. AppendFile returns an error, if any.
func CheckDir ¶
CheckDir performs checks on directory d and returns an error if
- d is an empty string
- d contains a blocked directory or filename
- d is an existing file, not a directory
- os.Stat returns an error when retrieving FileInfo
Otherwise it returns nil.
func CheckFile ¶
CheckFile performs checks on file f and returns an error if
- f is an empty string
- f contains a blocked directory or filename
- f is an existing directory, not a file
- os.Stat returns an error when retrieving FileInfo
Otherwise it returns nil.
func CloseFile ¶
CloseFile closes f and f is unusable for file input output. An error is returned if f has already been closed.
func CreateDir ¶
CreateDir creates a directory named d with any necessary parents. If d already exists as directory, it does nothing and returns nil. It returns an error, if there is any.
func CreateGoldenFile ¶ added in v1.3.0
CreateGoldenFile creates a golden file provided by the testcase name. The data in the testcase is written to the golden file. The golden file is stored in the default golden files directory testdata/ and has the default golden file type .golden.
func EvalGoldenFile ¶ added in v1.3.0
EvalGoldenFile evaluates the testcase if it equals the test data from the golden file provided by the testcase name. It returns an error if the testcase data does not equal the contents of the golden file. The golden file must reside in the default golden files directory testdata/ with the default golden file type .golden.
func ExistsDir ¶ added in v1.5.0
ExistsDir returns true if directory dn exists, returns false otherwise. It returns false and an error if there is any. If dn is a file, ExistsDir returns false and an error.
func ExistsFile ¶
ExistsFile returns true if file fn exists, returns false otherwise. It returns false and an error if there is any. If fn is a directory, ExistsFile returns false and an error.
func FileSize ¶ added in v1.1.0
FileSize returns the length in bytes for the regular file fn. If fn is a blocked filename, a directory or if FileInfo for fn cannot be retrieved, it returns 0 and an error.
func IsPrintable ¶ added in v1.3.0
IsPrintable returns true, if slice of strings a only consists of printable strings. If one or more strings of slice contain a non-printable character, it returns false. If a is nil or has zero length, it returns false and an error.
func NormNewlinesBytes ¶ added in v1.4.0
NormNewlinesBytes normalizes new lines in the byte slice i. New lines are normalized to the Unix representation of a new line as line feed LF (0x0A). Therefore, Windows new lines CR LF (0x0D 0x0A) are replaced by Unix new lines LF (0x0A). Also, Mac new lines CR (0x0D) are replaced by Unix new lines LF (0x0A). It returns a normalized copy of i. If i is nil, it returns an error.
func NormNewlinesStr ¶ added in v1.4.0
NormNewlinesStr normalizes new lines in the string i. New lines are normalized to the Unix representation of a new line as line feed LF (0x0A). Therefore, Windows new lines CR LF (0x0D 0x0A) are replaced by Unix new lines LF (0x0A). Also, Mac new lines CR (0x0D) are replaced by Unix new lines LF (0x0A). It returns a normalized copy of i.
func OpenFile ¶
OpenFile opens the named file fn with default flags and permission bits. If the file does not exist, it is created. If the directory to the file does not exist, the file will not be created and OpenFile returns an error. If opened successfully, the file is returned and can be used for file input output and error is nil.
func Printable ¶ added in v1.2.0
Printable returns all printable runes of string a as defined by Go. All non-printable runes of string a are dropped.
func ReadFile ¶
ReadFile reads f and and returns it contents. It returns an error, if any. If a file does not exist, it returns an error. If successful, error will be nil.
func RemoveFile ¶
RemoveFile removes file f. It returns an error, if there is any. If f is a directory it returns an error. If f does not exist, it also returns an error.
func ResetFile ¶
ResetFile truncates fn to size zero. If fn does not exist, it is created as empty file. It returns an error, if there is any.
func RuneToPrintable ¶ added in v1.3.0
RuneToPrintable returns rune r as a string, if it is printable. Otherwise, it returns an empty string.
func Sprintf ¶
Sprintf formats according to the format specifier and returns the resulting Filename or Directory
func TouchFile ¶
TouchFile updates the access and modification times of filename fn to the current time. If fn does not exist, it is created as an empty file. It returns an error, if any.
func WriteSingleStr ¶
WriteSingleStr writes a single string s to file fn. If fn exists, it is truncated to size zero first. If it does not exist, it is created. It returns an error, if any.
Types ¶
type Append ¶
type Append struct { FileA Filename // fileA to be extended FileI Filename // fileI holds the contents to be appended to fileA }
Append holds filename fileA, the file to be extended by contents from file I, and filename fileI, which is the input file and will be appended to fileA.
type Directory ¶
type Directory string
A Directory is the name of a directory and may contain its path
type Filename ¶
type Filename string
A Filename is the name of a regular file and may contain its path
func GoldenFilePath ¶ added in v1.3.0
GoldenFilePath returns the path of the test data golden file for the provided name of a testcase. The golden files are stored in the default golden files directory testdata/ and have the default golden file type .golden.
func InvalFile ¶ added in v1.3.0
func InvalFile() [14]Filename
InvalFile returns the array of blocked filenames. If a Filename matches InvalFile, tsfio functions will return an error.